楼主: dabap
3424 0

[问答] cowplot画图如何让图像连接在一起? [推广有奖]

  • 0关注
  • 0粉丝

大专生

46%

还不是VIP/贵宾

-

威望
0
论坛币
2 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
928 点
帖子
17
精华
0
在线时间
63 小时
注册时间
2012-4-1
最后登录
2016-8-8

楼主
dabap 发表于 2015-11-22 21:21:15 |AI写论文
30论坛币
代码让下:
  1. library(ggplot2)
  2. library(grid)
  3. library(cowplot)

  4. rm(list = ls())

  5. #####################3

  6. x1 <- c(1:24)
  7. y1 <- rep(0,24)
  8. x2 <- c(1.01832,2.5111,3.29454,4.2111,5.56558,6.7779,7.69868,8.1116,8.54911,8.8719,10.502,11.624,12.6021,13.38369,14.6044,15.53498,16.79781,17.64545,18.7055,19.89457,21.28147,21.90863,22.92,23.79068)
  9. y2 <- c(0.49966,0.47829,0.52273,0.56164,0.20028,0.18676,0.04273,0.38412,0.53544,0.64607,0.98098,1.45222,1.54973,1.14462,1.23707,1.10624,1.18284,0.82722,0.85045,1.09494,0.74885,0.69401,0.89644,0.97778)
  10. s  <- seq(length(x1))
  11. data <- data.frame(x1,x2,y1,y2)

  12. cp <- ggplot(data,aes(x=x1,y=y1))+geom_point(size=0)+
  13.   geom_segment(aes(x = x1[s], y = y1[s], xend = x2[s], yend = y2[s]),size=1.5, arrow = arrow(length = unit(0.5, "cm")))+
  14.     geom_hline(yintercept=0,linetype='dashed',color='blue',size=1)+
  15.         ylab('wind-speed (m/s)')+xlab('Time (h)')+
  16.          scale_y_continuous(limits=c(-4,4),breaks=c(-4.0,-2.0,0,2.0,4.0))+
  17.             scale_x_continuous(limits=c(1,24),breaks=c(0,3,6,9,12,15,18,21,24))+theme_bw()+
  18.              theme(panel.background=element_rect(fill='seagreen', color='black'),axis.text.x =element_blank(),axis.title.x=element_blank(),axis.ticks.x=element_blank() ,plot.margin = unit(c(0, 0.5, 0, 0.5), "lines"))
  19.          

  20. #####################

  21. tep <- c(29.1,28.1,28,27.4,27.2,26.8,26.8,27.1,28.9,30.8,32.9,34,34.7,35.1,35.6,36.2,36.3,36.,35.8,34.5,32.8,31.8,31.3,30.8)
  22. rh <- c(75,78,77,77,78,80,80,80,72,64,54,50,49,49,47,45,45,47,49,54,61,66,68,71)
  23. tep1 <- data.frame(x1,tep)

  24. sp <- ggplot(tep1,aes(x=x1,y=tep))+geom_line(size=1.5,colour='red')+ylab('Tep (°C)')+xlab('Time (h)')+
  25.         scale_x_continuous(limits=c(1,24),breaks=c(0,3,6,9,12,15,18,21,24))+
  26.            geom_hline(yintercept=mean(tep),linetype='dashed',color='blue',size=1)+theme_bw()+
  27.              theme(plot.background=element_rect(fill='lightblue', color='black'),
  28.                     axis.line.x=element_blank(),
  29.                       axis.ticks.length=unit(0, "cm")        ,
  30.                         axis.ticks.margin=unit(0, 'lines'),
  31.                            axis.text.x =element_blank(),
  32.                              axis.title.x=element_blank(),
  33.                               axis.ticks.x=element_blank() ,
  34.                                   plot.margin = unit(c(0, 0, 0, 0), "lines"),
  35.                                    panel.margin = unit(0, "lines"),
  36.                                     panel.border = element_rect(fill = NA, colour = "orange", size = 1),
  37.                                      legend.position = "none"
  38.                                         )

  39. ##############################3

  40. aqi <- c(19,59,119,118,87,67,126,216,217,179,186,300,298,309,319,320,319,220,221,150,120,100,119,119)
  41. aqi1 <- data.frame(x1,aqi)

  42. bp <- ggplot(aqi1,aes(x=x1,y=aqi))+geom_line(size=1.5,colour='blue')+ylab('pm')+xlab('Time (h)')+
  43.          scale_x_continuous(limits=c(1,24),breaks=c(0,3,6,9,12,15,18,21,24))+
  44.           scale_y_continuous(limits=c(0,500),breaks=c(0,50,100,150,200,300,500))+
  45.             geom_hline(yintercept=100,linetype='dashed',color='orange',size=1)+theme_bw()+
  46.               theme(plot.margin = unit(c(0.0, 1, 0, 0.5), "lines"))

  47. #print(grid.arrange(cp,sp,bp,ncol=1))
  48. ap <- plot_grid(cp,sp,bp,ncol=1,align='v')
  49. plot(ap)
复制代码



使用cowplot包将三个图像绘制在一起,但他们之间有缝隙,如何能让画出来的图像拼接在一起,我查了网上的一些画图区域设置,但是还是有一个空间在里面,请问哪个参数可以让第二幅图和第三幅图完美贴合在一起呢?





arrow.png (18.33 KB)

arrow.png

关键词:plot 在一起 Library ggplot2 BRARY library 如何

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-30 11:22