0

I wanted to change the legend and line colors in this qplot.enter image description here

Here is my data

   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

And here is the code I used

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

The legend title code line labs(variable = "Standard deviation(sd)") didn't work and R didn't report any error. How do I fix it?

I can color the dot on the line in black but that didn't change the legend. How do I make the legend change?

I tried to change the line color with geom_line(colour = c("red","yellow"), size=1) but that didn't work. How do I fix it?

Sorry for so many questions and thanks for any help.

1 Answer 1

1

You only need to fix a couple things; first, the title is called title, not variable; and second, you need to add a color scale for your lines. All together,

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

produces

plot with title and red and yellow lines

Really, since you're adding on geom_line and geom_point anyway, it's simpler to use ggplot notation instead of qplot. It also makes it a little clearer how aes gets inherited.

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

Alternately, chop out parts of qplot that you're overriding and move your color aesthetic to the proper place in geom_line (which simplifies your point color, too):

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

Note that the order of geom_line and geom_point determines which is on top.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.