Qplot线条颜色和传奇美学

时间:2016-03-04 23:17:13

标签: r ggplot2

我想更改此qplot中的图例和线条颜色。enter image description here

这是我的数据

   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)更改线条颜色,但这不起作用。我该如何解决?

很抱歉这么多问题,谢谢你的帮助。

1 个答案:

答案 0 :(得分: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

产生

plot with title and red and yellow lines

实际上,由于您无论如何都要添加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的部分内容,并将你的色彩审美移动到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的顺序决定哪个位于最前面。