this is from googling ggplot 2 line chart: http://www.sthda.com/english/wiki/ggplot2-line-plot-quick-start-guide-r-software-and-data-visualization
library(ggplot2)
df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
dose=rep(c("D0.5", "D1", "D2"),2),
len=c(6.8, 15, 33, 4.2, 10, 29.5))
head(df2)
## supp dose len
## 1 VC D0.5 6.8
## 2 VC D1 15.0
## 3 VC D2 33.0
## 4 OJ D0.5 4.2
## 5 OJ D1 10.0
## 6 OJ D2 29.5
len : Tooth length dose : Dose in milligrams (0.5, 1, 2) supp : Supplement type (VC or OJ)
# Line plot with multiple groups
ggplot(data=df2, aes(x=dose, y=len, group=supp)) +
geom_line()+
geom_point()
# Change line types
ggplot(data=df2, aes(x=dose, y=len, group=supp)) +
geom_line(linetype="dashed", color="blue", size=1.2)+
geom_point(color="red", size=3)
Change line types by groups In the graphs below, line types and point shapes are controlled automatically by the levels of the variable supp :
# Change line types by groups (supp)
ggplot(df2, aes(x=dose, y=len, group=supp)) +
geom_line(aes(linetype=supp))+
geom_point()
# Change line types and point shapes
ggplot(df2, aes(x=dose, y=len, group=supp)) +
geom_line(aes(linetype=supp))+
geom_point(aes(shape=supp))
Change line colors by groups Line colors are controlled automatically by the levels of the variable supp :
p<-ggplot(df2, aes(x=dose, y=len, group=supp)) +
geom_line(aes(color=supp))+
geom_point(aes(color=supp))
p