Reputation: 23
Im trying to create a scatterplot where the points are connected by a line. However, Id like to have the points coloured while the line remains black. If it try it I either dont get a legend. Or I get a legend that is partly or completly missing:
structure(list(Strain = c("Wildtype", "Wildtype", "Wildtype",
"Wildtype", "Wildtype", "Wildtype"), Time = c(0L, 24L, 48L, 72L,
120L, 144L), m = c(0, 1.2, 3.4, 6.5,
11.1, 14.2), sd = c(0, 1, 1,
1, 1, 1)), row.names = c(NA, -6L
), class = c("tbl_df", "tbl", "data.frame"))
ggplot(data=Wildtype, aes(x=Time,y=m))+
geom_errorbar(aes(ymin=m-sd, ymax=m+sd), width=1,
position=position_dodge(.9))+
geom_line(aes(color=Strain))+
scale_color_manual(values=c((Strain="black")))+
geom_point(aes(color=Strain),fill="red",color="black", shape =22, size=2.5,stroke=0)+
xlab("Time [h]")+
ylab("Hydrogen [µmol]")+
scale_x_continuous(breaks=seq(0,150,20))+
scale_y_continuous(breaks=seq(0,25,5),limits=c(0,25))+
ggtitle("Title")+
theme(plot.title = element_text(hjust = 0.5))
The df consists basically out of a "Strain" that is a character, time and a measurement for a specific time. This code leads me to the type of graph I want but its missing the red boxes in the legend. I only have a black line giving me the Strain. If i remove the scale_color_manual line I get a red line and also a red line in the legend (without a point crossed by that line). Is there any chance to color all the lines of the different "Strains" in black and only the data points in color. But have both the line and the point shown in the legend? Thank you very much for your help
Upvotes: 2
Views: 1533
Reputation: 23787
Easiest is to create a new aesthetic for geom_line
, e.g. linetype
- given that you want the line in black, you don't even need to specify it's color (black by default)
library(ggplot2)
ggplot(data=df, aes(x=Time,y=m))+
geom_point(aes(color=Strain))+
geom_line(aes(linetype = Strain)) +
scale_color_manual(values = "red")
Upvotes: 2