ggplot 中的多线图,具有不同颜色的点和线和点的图例

Multiple Line Plots in ggplot with different colors of points and legend for line and points

我有以下数据集

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")

如您所见,我有六个不同的案例。我的问题是,我怎样才能让点(个案)有不同的颜色,并且还有另一个图例?

提前致谢

您可以使用 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")

它是否回答了您的问题?


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