Tak
Tak

Reputation: 3616

Plotting data using ggplot

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

Upvotes: 3

Views: 73

Answers (2)

rodolfoksveiga
rodolfoksveiga

Reputation: 1261

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

Upvotes: 1

G_T
G_T

Reputation: 1587

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

Upvotes: 4

Related Questions