R语言 ggplot绘图中添加直线、箭头、注释、矩形等

 

001、基础绘图

library(gcookbook)  # For the data set
p <- ggplot(heightweight, aes(x = ageYear, y = heightIn, colour = sex)) + geom_point()
p                              ## 基础绘图

 

002、添加直线

p + geom_hline(yintercept = 60) + geom_vline(xintercept = 14)      ## 添加直线

 

003、调整颜色及粗细

p + geom_hline(yintercept = 60, colour = "red", lwd = 3)      ## 调整颜色、粗细

 

003_02、

p + geom_hline(yintercept = 60, colour = "red", lwd = 3, 
               linetype = "dashed")                ## 调整线条类型

 

 

004、增加斜线,控制截距和斜率

p + geom_abline(intercept = 37.4, slope = 1.75)        ## 增加斜线,控制截距和斜率

 

005、  增加箭头

p <- ggplot(subset(climate, Source == "Berkeley"), 
            aes(x = Year, y = Anomaly10y)) + 
  geom_line()
p + annotate("segment", x = 1850, xend = 1820, y = -0.8, 
yend = -0.95, colour = "blue", size = 1, arrow = arrow())   ## 增加箭头

 

006、 增加箭头

p + annotate("segment", x = 1950, xend = 1980, y = -0.25, 
yend = -0.25, arrow = arrow(ends = "both", angle = 70, 
length = unit(0.2,  "cm")))         ## 增加箭头

 

007、 添加矩形

library(gcookbook)

p <- ggplot(subset(climate, Source == "Berkeley"), aes(x = Year, y = Anomaly10y)) + 
  geom_line()                      ## 基础绘图
p
p + annotate("rect", xmin = 1950, xmax = 1980, ymin = -1, ymax = 1, alpha = 0.1, 
             fill = "blue")        ## 添加矩形, 设置透明度,填充色

 。

 

posted @   小鲨鱼2018  阅读(2682)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2022-07-10 使用plink、gemma软件进行gwas分析增加协变量
2022-07-10 主板EATX、ATX、MATX、ITX
2022-07-10 gemma、plink使用一般线性模型进行GWAS分析
2021-07-10 判断系列向量组的线性相关性a1=(1,1,2,0),a2=(1,2,3,-1), a3=(-1,1,0,-2)
2021-07-10 linux系统中tee命令
2021-07-10 若向量组A可由向量组B线性表示,则R(A) <= R(B)
点击右上角即可分享
微信分享提示