gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:19:54 27 4
gpt4 key购买 nike

我不希望标题被截断,并且希望从我使用 cowplot 中的 plot_grid 生成的图表中删除轴标签。 enter image description here

这是我的代码

    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 创建它会更简单,您有什么建议?

最佳答案

这里只有一个 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)

产量:

enter image description here

关于r - 如何使用 plot_grid 自定义边距和标签设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34466750/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com