美文网首页
R绘图_ggplot2绘制Line Plot

R绘图_ggplot2绘制Line Plot

作者: 谢俊飞 | 来源:发表于2020-02-14 18:26 被阅读0次

ggplot2在线学习:STHDA :Statistical tools for high-throughput data analysis.
ggplot2使用说明:https://ggplot2.tidyverse.org/reference/

  • 火狐截图_2020-02-11T08-36-22.554Z.png

Line Plot

英文:http://www.sthda.com/english/wiki/ggplot2-line-types-how-to-change-line-types-of-a-graph-in-r-software

根据说明文档,运行代码……

#generate some data
df <- data.frame(time = c("Breakfeast","Lunch","Dinner"),
                 bill = c(10, 30, 15))
head(df)
#creat line plots and change line types
library(ggplot2)
#basic line plot with points
ggplot(df, aes(x = time, y=bill,group=1)) +
  geom_line()+
  geom_point()
#change the line type
ggplot(df, aes(x=time, y=bill, group=1)) +
  geom_line(linetype = "dashed") +
  geom_point()
#creat some datas
df2 <- data.frame(sex = rep(c("F","M"), each =3),
                  time = c("Breakfeast","Lunch","Dinner"),
                  bill = c(10, 30, 15, 13, 40, 17))
head(df2)
#change the globally appearance of lines
library(ggplot2)
#line plot with muliple group
ggplot(df2, aes(x=time, y=bill, group=sex)) +
  geom_line() +
  geom_point()

#change the line type
ggplot(df2, aes(x=time, y=bill, group=sex)) +
  geom_line(linetype="dashed") +
  geom_point()
#change the colors and sizes
ggplot(df2, aes(x=time, y=bill, group=sex)) +
  geom_line(linetype="dashed", color="red", size=2) +
  geom_point(color="blue",size=3)
#change the line typs + colors
ggplot(df2, aes(x=time, y=bill, group=sex)) +
  geom_line(aes(linetype=sex,color=sex)) +
  geom_point(aes(color=sex)) +
  theme(legend.position = "top")

#set the line type manually
ggplot(df2, aes(x=time, y=bill, group=sex)) +
  geom_line(aes(linetype=sex,color=sex)) +
  geom_point() +
  scale_linetype_manual(values=c("twodash", "dotted")) +
  theme(legend.position = "top")

# Change line colors and sizes
ggplot(df2, aes(x=time, y=bill, group=sex)) +
  geom_line(aes(linetype=sex, color=sex, size=sex))+
  geom_point()+
  scale_linetype_manual(values=c("twodash", "dotted"))+
  scale_color_manual(values=c('#999999','#E69F00'))+
  scale_size_manual(values=c(1, 1.5))+
  theme(legend.position="top")

相关文章

网友评论

      本文标题:R绘图_ggplot2绘制Line Plot

      本文链接:https://www.haomeiwen.com/subject/hfsjfhtx.html