我有以下数据集

data = data.frame(tmu = c(0.1164966,0.01649658, 0.605479878,0.073743729, 0.21649659,0.543409994,0.1164966,0.01649658,0.605479878,
                  0.073743729,0.21649659,0.543409994,0.1164966,0.01649658,0.605479878, 0.073743729, 0.21649659,0.543409994)
                  , Method = c( rep('M1', 6), rep('M2', 6), rep('M3', 6)),
                  Value = c(1.140242, 1.016913, 2.211742,1.07824, 1.312171, 1.872045,1.131858,1.016773, 1.982265,1.077372, 1.276319,
                            1.771913,1.131858, 1.016773,1.932845, 1.077338,  1.276319, 1.756129),
                  cases = rep(c('A', 'B', 'C', 'D', 'E', 'F'),3))

我使用以下代码生成下面的图表。
pd <- position_dodge(width = 0.4)

ggplot(data, aes(x=tmu, y=Value, color=Method)) +
  geom_line(size = .3, position = pd) +
  geom_point(size = 2, shape = 18, position = pd) +
  labs(fill = "") +
  theme(legend.position="bottom")

r - ggplot 中的多线图具有不同颜色的点和线和点的图例-LMLPHP
如您所见,我有六个不同的案例。我的问题是,如何为点(案例)设置不同的颜色并且还有另一个图例?

先感谢您

最佳答案

例如,您可以使用 fillcolor 参数来区分点和线。在选择允许填充的点形状(例如 shape = 21 )时,为线条着色并为点填充:

ggplot(data, aes(x=tmu, y=Value, color = Method, group = Method)) +
  geom_line(size = .3, position = pd) +
  geom_point(aes(fill = cases),color = "black", shape = 21,size = 2, position = pd) +
  labs(fill = "") +
  theme(legend.position="bottom")

或者您可以将不同的 color 参数传递给 geom_linegeom_point :

ggplot(data, aes(x=tmu, y=Value, group = Method)) +
  geom_line(aes(color = Method), size = .3, position = pd) +
  geom_point(aes(color = cases),size = 2, shape = 18, position = pd) +
  labs(fill = "") +
  theme(legend.position="bottom")

它回答你的问题吗?

注意:由于某些原因,我无法上传输出图的图像......抱歉

关于r - ggplot 中的多线图具有不同颜色的点和线和点的图例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60625153/

10-12 17:59
查看更多