删除gg.gap()图中的axis.titles

问题描述 投票:1回答:1

有人知道如何从gg.gap()图中删除axis.title.y吗?我正在尝试使用主题(),但无法正常工作...

例如,使用gg.gap()包示例:我做了一个ggplot,没有任何y和x轴标题(“ p”图),但是当我在gg.gap中使用“ p”在y轴上创建间隙时,轴标题再次出现...

data(mtcars)
library(ggplot2)

p<-ggplot(data = mtcars, aes(x = gear, fill = gear)) +geom_bar() +ggtitle("Number of Cars by Gear") +xlab("Gears") + 
          theme( axis.title.y = element_blank(),
             axis.ticks.y = element_blank(),
             axis.text.y = element_blank(),
             axis.ticks.x = element_blank(),
             axis.text.x = element_blank())

pgg <- gg.gap(plot=p,segments=c(5,10),tick_width = c(1,10),ylim=c(0,50))

而且这两个theme()都不起作用...

pgg + theme( axis.title.y = element_blank(),
             axis.ticks.y = element_blank(),
             axis.text.y = element_blank(),
             axis.ticks.x = element_blank(),
             axis.text.x = element_blank())

谢谢你!

ggplot2 plot label axis
1个回答
0
投票

这很有趣。如@teunbrand所述,您应该可以通过scale_y_continuous(name="")或我的偏好设置name=NULL来解决此问题。尽管有任何排列,它也不起作用!

似乎有效的方法是使用NULL""将轴标题设置为lab(y=...)ylab()。无论您在哪里添加它,它都可以工作:

# this doesn't work
p <- p + scale_y_continuous(NULL)
pgg <- gg.gap(plot=p,segments=c(5,10),tick_width = c(1,10),ylim=c(0,50))
pgg

# this doesn't work either
pgg + scale_y_continuous(NULL)


# this works
p <- p + ylab(NULL)
pgg <- gg.gap(plot=p,segments=c(5,10),tick_width = c(1,10),ylim=c(0,50))
pgg

# this works too
pgg <- gg.gap(plot=p,segments=c(5,10),tick_width = c(1,10),ylim=c(0,50))
pgg + ylab(NULL)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.