3

I am trying to plot my data with legends for the black line as well but I don't know how it can be done so that "MYLINE" will appear on the right side under MY2? and how to control the colour and properties of this line e.g colour, thickness,...?

MY1_DIS_REF$METHOD <- "MY1"
MY1_plot<-MY1_DIS_REF[,-c(5,6,7)]
MY2_DIS_REF$METHOD <- "MY2"
MY2_plot<-MY2_DIS_REF[,-c(5,6,7)]
MY1andMY2_PLOT <- rbind(MY1_plot,MY2_plot)

ggplot()+
  geom_point(data=MY1andMY2_PLOT,aes(X,Y,color=METHOD),alpha=0.5)+
  geom_path(data=line,aes(V1,V2,group=B1,),size=1)+
  xlab("X")+
  ylab("Y")+
  facet_wrap(~B1,scales = "free")+
  theme_bw()+coord_flip()

enter image description here

4
  • Try adding aes(color = "MYLINE"") into the geom_path() call. You can edit other attributes like that too, e.g. size = 3 Commented Nov 16, 2020 at 18:38
  • @G_T Thank you but now the line is drawn on the points in the legend as shown here: dropbox.com/s/bu9wc95ns8h7dpw/… Also how to set the colours of each? Is it random that now it became red green and blue? Commented Nov 16, 2020 at 19:11
  • can you share a piece of your data so we can help you easier? i cannot reproduce your plot.. please, copy and paste the output of dput(MY1_DIS_REF) and dput(MY2_DIS_REF) into you question. Commented Nov 16, 2020 at 19:12
  • @rodolfoksveiga You can find the MY1andMY2_PLOT and line in this link dropbox.com/sh/cybaqgbctgo55co/AADwTkKXZdSpY-tZafDgJ7U7a?dl=0 Commented Nov 16, 2020 at 19:24

2 Answers 2

4

You can add a separate legend for the line by using a different aesthetic to the one the current legend is using:

ggplot()+
  geom_point(data=MY1andMY2_PLOT,aes(X,Y,color=METHOD),alpha=0.5)+
  geom_path(data=line,aes(V1,V2, group=B1, linetype = "Myline"), size=1)+
  xlab("X")+
  ylab("Y")+
  facet_wrap(~B1,scales = "free")+
  theme_bw()+coord_flip()

enter image description here

You can change the other aesthetics with arguments outside of aesthetics, e.g. thickness is controlled by size:

ggplot()+
  geom_point(data=MY1andMY2_PLOT,aes(X,Y,color=METHOD),alpha=0.5)+
  geom_path(data=line,aes(V1,V2, group=B1, linetype = "Myline"), size=3)+
  xlab("X")+
  ylab("Y")+
  facet_wrap(~B1,scales = "free")+
  theme_bw()+coord_flip()

enter image description here

You can change colors by editing the color scale (red and blue are default):

ggplot()+
  geom_point(data=MY1andMY2_PLOT,aes(X,Y,color=METHOD),alpha=0.5)+
  geom_path(data=line,aes(V1,V2, group=B1, linetype = "Myline"), size=1)+
  xlab("X")+
  ylab("Y")+
  facet_wrap(~B1,scales = "free")+
  theme_bw()+coord_flip() +
  scale_color_manual(values = c("orange", "pink"))

enter image description here

Edit: Change line colour:

ggplot()+
  geom_point(data=MY1andMY2_PLOT,aes(X,Y,color=METHOD),alpha=0.5)+
  geom_path(data=line,aes(V1,V2, group=B1, linetype = "Myline"), size=3, colour = "blue") +
  xlab("X")+
  ylab("Y")+
  facet_wrap(~B1,scales = "free")+
  theme_bw()+coord_flip()

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. Is it possible to change the colour of the line? and change the legend name from "linetype" to something else?
i guess setting geom_path(data=line,aes(V1,V2, group=B1, DESIRED_NAME = "Myline", colour = "DESIRED_COLOUR") will do the trick
@rodolfoksveiga When I do this "Myline" legend never appears. Any advice?
`"Myline" is whatever you want to call your line in the legend. check the legend in the plot and you'll see the name of the black line is "Myline"
@rodolfoksveiga I understand but when I did this geom_path(data=line,aes(V1,V2, group=B1, DESIRED_NAME = "Myline", colour = "DESIRED_COLOUR") nothing appeared in the legend
|
1

If you also want to rename the legend, you can do the following:

ggplot()+
  geom_point(data=MY1andMY2_PLOT,aes(X,Y,color=METHOD),alpha=0.5)+
  geom_path(data=line,aes(V1,V2, group = B1, linetype = "MyLine"), size=3, colour = "blue") +
  labs(x = "X", y = "Y", linetype = "DESIRED_LINETYPE_NAME", color = "DESIRED_COLOR_NAME")+
  facet_wrap(~B1,scales = "free")+
  theme_bw()+coord_flip()

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.