使用geom_point()和geom_line()进行多个系列的ggplot中的图例错误

佩德罗·坎波斯

我正在尝试从以下数据集中绘制点和线。

dados = structure(list(
Vertices = c(0.5, 1, 1.5, 2, 2.5, 3), 
AAA = c(1.8156, 2.2355, 2.4784, 2.6283, 2.7266, 2.7947), 
BBB = c(1.9603, 2.3802, 2.6231, 2.773, 2.8713, 2.9394), 
CCC = c(2.1559, 2.5758, 2.8188, 2.9686, 3.0669, 3.135)), 
row.names = c(NA, 6L), class = "data.frame")

ggplot(data = dados, aes(x = Vertices)) +

  #lower points
  geom_point(aes(y = AAA, color = "purple")) +
  geom_line(aes(y = AAA, color = "purple", linetype ="longdash")) +

  #intermediate points
  geom_point(aes(y = BBB, color = "blue")) +
  geom_line(aes(y = BBB, color="blue", linetype = "dotted")) + 

  #higher points
  geom_point(aes(y = CCC, color = "green")) +
  geom_line(aes(y = CCC, color="green", linetype = "dashed")) +

  scale_color_manual(values = c("purple", "blue", "green"),
                     labels = c("AAA","BBB","CCC")) +

  ggtitle("Curvas Spread Debentures") +
  xlab("Years") + ylab("% points")

#helps visualize numerically
dados 

总结:美学颜色未遵循代码顺序

“ AAA”是最低值,应该是紫色,但是以绿色绘制,并且在图例中被指定为“ CCC”。

“ BBB”是中间值,应该是蓝色,但以紫色绘制,并在图例中指定为“ AAA”

“ CCC”是最大值,应该是绿色,但以蓝色绘制,并在图例中指定为“ BBB”

我还尝试仅绘制点或仅绘制线以简化,但是颜色和图例也弄乱了。这是一个非常简单的代码,完全出错了,我看不到错误,有什么想法吗?

罗纳克·沙

你是说要做这样的事吗?

library(ggplot2)

tidyr::pivot_longer(dados, cols = -Vertices) %>%
    ggplot() + aes(Vertices, value, color = name) +
    geom_point() + geom_line(aes(linetype=name)) + 
    ggtitle("Curvas Spread Debentures") +
    xlab("Years") + ylab("% points") +
    scale_color_manual(values = c('purple', 'blue', 'green'))

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ggplot2:合并geom_line,geom_point和geom_bar的图例

如何使geom_line和geom_point抖动相同的幅度?

从图例中删除ggplot2的geom_point图标

使用facet_grid和多个数据点(R ggplot2)时,将geom_point和geom_line组合

ggplot2中带有geom_line和geom_point的图例

如何为我的ggplot添加图例?我正在使用geom_line和geom_ribbon

如何在ggplot2中获取堆叠的geom_bar和geom_point的通用图例?

ggplot2-结合了geom_point和geom_line的图例

ggplot图例(geom_point)中的空白条目?

动画geom_line()崩溃为geom_point()

绘制geom_line()+ geom_point()R

将gganimate与geom_point和geom_line一起使用

使用geom_bar和geom_point控制ggplot中的图例

抖动geom_line和geom_point

添加geom_point和geom_line进行绘图

ggplot:两组的geom_line和geom_point重叠顺序?

ggplot:为什么为geom_ribbon和geom_line都打印图例?

ggplot2:将geom_line和geom_point组合在一起的图形中的坐标图例

分别调整geom_point()和geom_line()

结合ggplot geom_point和geom_line图产生的冗余图例

ggplot2:如何将geom_line和geom_point的图层与错误栏组合在一起

带有ggplot2和geom_point的图例

绘制 geom_line() 和 geom_point() - 不同长度的数据

无法在 r 中使用 ggplot2 将组合 geom_point 和 _line 图的颜色和线型图例分开

Geom_point 和 ggboxplot 以及 ggpair 在 ggplot 中错误地对所有绘图点进行了配对

如何在 ggplot 混合 geom_rect 和 geom_line 中对区域进行着色?

将 geom_line 和 geom_point 与 ggplotly 一起使用时的奇怪图例和工具提示文本

ggplot 将 geom_point 与 geom_line 分开的方法?

带有 geom_line 和 geom_ribbon 和 geom_point 的图例