5794 2

[问答] ggplot如何控制图表的宽度? [推广有奖]

  • 0关注
  • 0粉丝

初中生

42%

还不是VIP/贵宾

-

威望
0
论坛币
315 个
通用积分
1.0003
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
646 点
帖子
13
精华
0
在线时间
17 小时
注册时间
2015-12-12
最后登录
2020-7-9

3论坛币
QQ图片20170720225207.png
最下面一个图没有图例结果宽度就和上面三张图不一样了。。。有什么办法可以让四个图的宽度一致吗?前三个图的图例还是需要的。
代码如下:
  1. p1<-ggplot(saturday,aes(x=hours))+geom_point(aes(y=Ridership,size=1,alpha=0.6,color="Ridership"))+
  2.   geom_point(aes(y=moving_average,size=1,alpha=0.6,color="moving_average"))+
  3.   labs(title="Saturday Hourly Ridership")+
  4.   theme(plot.title = element_text(hjust = 0.5,size=28))+
  5.   theme(text = element_text(size=25))+
  6.   guides(alpha=FALSE)+guides(size=FALSE)+
  7.   ylab("Daily Ridership")+
  8.   scale_colour_discrete(breaks = c('Ridership','moving_average'), labels = c('Daily Ridership','Moving Average'))+
  9.   xlim(0,168)

  10. p2<-ggplot(sunday,aes(x=hours))+geom_point(aes(y=Ridership,size=1,alpha=0.6,color="Ridership"))+
  11.   geom_point(aes(y=moving_average,size=1,alpha=0.6,color="moving_average"))+
  12.   labs(title="Sunday Hourly Ridership")+
  13.   theme(plot.title = element_text(hjust = 0.5,size=28))+
  14.   theme(text = element_text(size=25))+
  15.   guides(alpha=FALSE)+guides(size=FALSE)+
  16.   ylab("Daily Ridership")+
  17.   scale_colour_discrete(breaks = c('Ridership','moving_average'), labels = c('Daily Ridership','Moving Average'))+
  18.   xlim(0,168)

  19. p3<-ggplot(weekday,aes(x=hours))+geom_point(aes(y=Ridership,size=1,alpha=0.6,color="Ridership"))+
  20.   geom_point(aes(y=moving_average,size=1,alpha=0.6,color="moving_average"))+
  21.   labs(title="WeekDay Hourly Ridership")+
  22.   theme(plot.title = element_text(hjust = 0.5,size=28))+
  23.   theme(text = element_text(size=25))+
  24.   guides(alpha=FALSE)+guides(size=FALSE)+
  25.   ylab("Daily Ridership")+
  26.   scale_colour_discrete(breaks = c('Ridership','moving_average'), labels = c('Daily Ridership','Moving Average'))+
  27.   xlim(0,168)

  28. p4<-ggplot(mydataframe,aes(x=hours,y=residual))+geom_line()+
  29.   labs(title="WeekDay Hourly Ridership")+
  30.   theme(plot.title = element_text(hjust = 0.5,size=28))+
  31.   theme(text = element_text(size=25))+
  32.   guides(alpha=FALSE)+guides(size=FALSE)+
  33.   ylab("Daily Ridership")+
  34. geom_hline(yintercept = 0)
  35.   xlim(0,168)
  36. easyGgplot2::ggplot2.multiplot(p1,p2,p3,p4,cols = 1)
复制代码

关键词:gplot plot 控制图 GPL
沙发
jimaocai 发表于 2017-7-21 23:05:38 |只看作者 |坛友微信交流群
用gridExtra工具包可以实现,比如你可以画两个图,用代码grid.arrange(g1,g2,ncol=2,widths=c(1,2),heights=1)其中g1 和g2是两个ggplot的图,具体详情请看分析资料: https://bbs.pinggu.org/forum.php?mod=viewthread&tid=5866021&extra=, 或者查阅gridExtra工具包帮助文件
已有 1 人评分论坛币 收起 理由
admin_kefu + 20 热心帮助其他会员

总评分: 论坛币 + 20   查看全部评分

使用道具

藤椅
stormhz 发表于 2018-5-30 18:35:55 |只看作者 |坛友微信交流群
  1. library(cowplot)
  2. ggdraw() +
  3.   draw_plot(p4, x = 0, y = 0,   width = .9, height = .25) +
  4.   draw_plot(p1, x = 0, y = .25, width = 1,  height = .25) +
  5.   draw_plot(p2, x = 0, y = .5,  width = 1,  height = .25) +
  6.   draw_plot(p3, x = 0, y = .75, width = 1,  height = .25)
复制代码

Rplot18.png (15.66 KB)

Rplot18.png

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-26 11:25