当前位置:  开发笔记 > 编程语言 > 正文

如何使用plot_grid自定义边距和标签设置?

如何解决《如何使用plot_grid自定义边距和标签设置?》经验,为你挑选了1个好方法。

我想不删除标题,并从该图表中删除轴标签,该图表是我从cowplot用plot_grid生成的。

这是我的代码

    data(mtcars)
 library(ggplot2)
 library(cowplot)
 mpg = ggplot() +
 geom_boxplot(aes(y = mpg,x = as.factor(cyl)),data=mtcars) +
 coord_flip()
 am=ggplot() +
 geom_boxplot(aes(y = mpg,x = as.factor(am)),data=mtcars) +
 coord_flip()
 vs=ggplot() +
 geom_boxplot(aes(y = mpg,x = as.factor(vs)),data=mtcars) +
 coord_flip()
 gear = ggplot() +
 geom_boxplot(aes(y = mpg,x = as.factor(gear)),data=mtcars) +
 coord_flip()
 p=plot_grid(mpg,am,vs,gear, labels = "Variables effecting Mileage", label_size = 14, hjust = -0.5,
 + vjust = 0.5)+theme_grey()
p

此外,如果不使用Cowplot来创建它会更简单,那么您有何建议?



1> Mike Wise..:

这是cowplot唯一的答案。可能更多是您想要的。

library(ggplot2)
library(cowplot)
library(gtable)

data(mtcars)

mpg = ggplot() +
  geom_boxplot(aes(y = mpg,x = as.factor(cyl)),data=mtcars) +
  coord_flip() + labs(x="",y="")
am=ggplot() +
  geom_boxplot(aes(y = mpg,x = as.factor(am)),data=mtcars) +
  coord_flip()+ labs(x="",y="")
vs=ggplot() +
  geom_boxplot(aes(y = mpg,x = as.factor(vs)),data=mtcars) +
  coord_flip()+ labs(x="",y="")
gear = ggplot() +
  geom_boxplot(aes(y = mpg,x = as.factor(gear)),data=mtcars) +
  coord_flip()+ labs(x="",y="")
p=plot_grid(mpg,am,vs,gear) +
  theme_grey() +

# Use annotation text as it is centered at the x,y point
  annotate("text",x=0.5,y=1.04,size=7,label="Variables affecting Mileage") +

# Add some space around the edges  
  theme(plot.margin = unit(c(1,0.5,0.5,0.5), "cm")) 


# Suppress tick marks
p=p+scale_y_continuous(breaks=NULL)+scale_x_continuous(breaks=NULL)

# Have to turn off clipping
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"

# need to draw it with the new clip settings
grid.draw(gt)

产量:

推荐阅读
个性2402852463
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有