0

Hi I have managed to do this plot:

p3 <- ggplot() + 
geom_point(data = dv,aes(x = year,y = X1st.Qu.,group = 1),color = "yellow",shape = 95,size = 8) + 
geom_point(data = dv,aes(x = year,y = Mean,group = 1),color = "blue", shape = 21,size = 2) + 
geom_point(data = dv,aes(x = year,y = X3rd.Qu.,group = 1),color = "red",shape = 95,size = 8) +
geom_line(data = dv,aes(x = year,y = Mean,group = 1),color = "blue", shape = 21,size = .5)+
theme(axis.text.x = element_text(angle = 90,size = 7))+
theme(plot.title = element_text(hjust = 0.5))+
ggtitle("Statistical description per year") + 
labs(y="Statistics", x = "Year")

and would like to add a legend for the colours...how is it possible? I tried with scale_color_manual but didn't show up

As per last recommendation to move the color in the aes() arguments this is the result.

The colours are no longer mapped with color although the legend is shown (incorrectly)

enter image description here

user9737581
  • 41
  • 1
  • 8
  • @ user9737581 .. Can u add an image of what have you got, so that we can help you on what you want and what is missing? – user1142937 May 11 '18 at 10:43
  • just included it now – user9737581 May 11 '18 at 11:19
  • @ user9737581 Try `scale_shape_manual(values = c(1, 1,1), breaks = c("lower Quartile", "mean",Upper Quartile"))` – user1142937 May 11 '18 at 11:26
  • Although no complaints from R it does not show the legend still. – user9737581 May 11 '18 at 11:42
  • @ user9737581 :Just watched ur code closely, move the `color` argument inside the `aes` for all the `geometry` as this : `geom_point(data = dv,aes(x = year,y = X1st.Qu.,color = "yellow"),shape = 95,size = 8)` – user1142937 May 11 '18 at 12:03
  • Hi I edited my initial post to be able to see the result of the suggestion – user9737581 May 11 '18 at 12:11
  • The best long-term solution is to reshape your data (see linked question, some excellent answers there). Alternatively, use manual coloring: use `aes(...., color = 'my_label1')` for each colored geom. Then add `+ scale_color_manual(values = c('my_label1' = 'yellow', etc...))`. Then do the same for the shapes. It's a lot of work, which is why reshaping is easier! – Axeman May 11 '18 at 12:43
  • @ user9737581: As pointed already by me and @Axeman kindly use `scale_shape_manual` for correcting the right colors in the legend... – user1142937 May 11 '18 at 16:02
  • @ user9737581 This solves ur problem.. add this line to have control on colors in legend.. scale_color_manual(values = c('yellow' = 'yellow', "blue" = 'blue','red' = 'red','blue' = 'blue')) – user1142937 May 11 '18 at 16:23

0 Answers0