2

我希望标题不被切掉,并从我用来自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的情况下创建它会更简单,你有什么建议?

4

3 回答 3

5

这是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)

产量:

在此处输入图像描述

于 2015-12-26T06:08:17.040 回答
2

这个锻炼适合你吗?

library(ggplot2)
library(gridExtra)
a <- ggplot() + geom_boxplot(aes(y = mpg,x = as.factor(cyl)),data=mtcars) + coord_flip()  + theme_grey() + theme(axis.title.x = element_blank()) 
b <- ggplot() + geom_boxplot(aes(y = mpg,x = as.factor(am)),data=mtcars) + coord_flip()  + theme_grey() + theme(axis.title.x = element_blank())
c <- ggplot() + geom_boxplot(aes(y = mpg,x = as.factor(vs)),data=mtcars) +
coord_flip() + theme_grey() + theme(axis.title.x = element_blank())
d <-  ggplot() + geom_boxplot(aes(y = mpg,x = as.factor(gear)),data=mtcars) +
coord_flip() + theme_grey() + theme(axis.title.x = element_blank()) 
grid.arrange(a, b, c, d, ncol=2, top = "Variables effecting Mileage")

在此处输入图像描述

于 2015-12-26T01:39:27.577 回答
1

谢谢迈克。这可以完成工作。

 data(mtcars)
 library(ggplot2)
 library(cowplot)
 library(gtable)
 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, 
        labels = "Variables effecting Mileage", 
        label_size = 14, hjust = -1.3[![enter image description here][1]][1], vjust = -0.1)+
 theme_grey() +

 # 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)

这是我的情节 解决方案

于 2015-12-26T15:41:02.857 回答