繁体   English   中英

Qplot线条颜色和图例美学

[英]Qplot line color and legend aesthetic

我想在此qplot中更改图例和线条颜色。 在此处输入图片说明

这是我的数据

  n.clusters mean.cluster mean.bucket variable value 1 3 21.64790 21.49858 sd.cluster 5.643380 2 5 21.63516 21.54975 sd.cluster 4.369756 3 7 21.55446 21.49889 sd.cluster 3.643280 4 9 21.59585 21.57022 sd.cluster 3.237870 5 11 21.63110 21.58452 sd.cluster 3.012060 6 13 21.55224 21.56104 sd.cluster 2.643777 7 3 21.64790 21.49858 sd.bucket 5.648886 8 5 21.63516 21.54975 sd.bucket 4.397690 9 7 21.55446 21.49889 sd.bucket 3.654752 10 9 21.59585 21.57022 sd.bucket 3.262954 11 11 21.63110 21.58452 sd.bucket 3.023834 12 13 21.55224 21.56104 sd.bucket 2.716441 

这是我使用的代码

 qplot(n.clusters, value, data = mu.est.summary.long,colour = variable, geom = c("point", "line"))+ theme_bw() + scale_x_continuous(breaks = seq(1,13,2)) + geom_point(aes(n.clusters, value), colour = "black", size=3.5) + geom_line(size=1)+ labs(x = "Number of cluster", y = "Value", variable = "Standard deviation(sd)") 

图例标题代码行labs(variable = "Standard deviation(sd)")不起作用,R没有报告任何错误。 我如何解决它?

我可以将行上的点涂成黑色,但这并没有改变图例。 如何更改图例?

我试图用geom_line(colour = c("red","yellow"), size=1)更改线条颜色,但这没有用。 我如何解决它?

很抱歉有这么多问题,感谢您的帮助。

您只需要修复几件事; 首先,标题称为title ,而不是variable 其次,您需要为线条添加一个色标。 全部一起,

qplot(n.clusters, value, data = df, colour = variable, geom = c("point", "line"))+
  theme_bw() +
  scale_x_continuous(breaks = seq(1,13,2)) +
  geom_point(aes(n.clusters, value), colour = "black", size=3.5) + 
  geom_line(size=1)+
  scale_color_manual(values = c('red', 'yellow')) +   # added
  labs(x = "Number of cluster",
       y = "Value",
       title = "Standard deviation(sd)")   # changed

产生

标有标题和红线和黄线的情节

确实,由于无论如何都要添加geom_linegeom_point ,因此使用ggplot表示法而不是qplot更为简单。 这也使aes是如何继承的更加清晰。

ggplot(data = df, aes(n.clusters, value)) + 
  geom_line(aes(colour = variable), size = 1) +
  geom_point(size = 3.5) +
  scale_x_continuous(breaks = seq(1, 13, 2)) +
  scale_color_manual(values = c('red', 'yellow')) + 
  theme_bw() +
  labs(x = "Number of cluster",
       y = "Value",
       title = "Standard deviation (sd)")

或者, qplot您要覆盖的qplot部分, qplot颜色美感移至geom_line的适当位置(这也简化了点颜色):

qplot(n.clusters, value, data = df)+
  geom_line(aes(colour = variable), size = 1) +
  geom_point(size = 3.5) + 
  scale_x_continuous(breaks = seq(1, 13, 2)) +
  scale_color_manual(values = c('red', 'yellow')) + 
  theme_bw() +
  labs(x = "Number of cluster",
       y = "Value",
       title = "Standard deviation(sd)")

请注意, geom_linegeom_point的顺序确定哪个在最上面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM