为facet_grid图的所有构面强制X轴文本 [英] Force X axis text on for all facets of a facet_grid plot

查看:126
本文介绍了为facet_grid图的所有构面强制X轴文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与这个用户:我想用一个离散的x轴制作一个 facet_grid 图,我想让x轴标签写在每个面的下面,而不是只写在底面的小面下面。例如:

 #降低一些因子水平以使图较小
diamondSub < - subset(diamonds,( cut ==Ideal| cut ==Premium)&
(color ==E| color ==I))

#请注意scales = free_x在这里没有实际效果
ggplot(diamondSub,aes(x = clarity,y = price))+
geom_blank()+
geom_boxplot()+
facet_grid(cut 〜color,scales =free_x)


然而,我不想使用该帖子中的解决方案,这是只是为了使用 facet_wrap 而不是 facet_grid ,因为我更喜欢 facet_grid 在条的顶部添加一个变量,在行的两侧添加另一个变量。



有没有办法在每个方面都得到x轴标签,而所有的x轴实际上都是相同的,使用 facet_grid

解决方案

您可以在gtable中插入轴的副本,

< $ p $ library(gtable)
g < - ggplotGrob(p)
#找到面板
面板< - grep(panel,g $布局$名称)
顶部< - 独特(g $布局$ t [面板])
#散布底层的副本
全部< - gtable ::: rbind_gtable(gtable ::: rbind_gtable(g [seq.int(min(top)),],
g [max(top)+1,],first),
g [seq(min(top)+ 1,nrow(g)),],first)
grid.newpage()
grid.draw(全部)


I have the same problem as this user: I'd like to make a facet_grid plot with a discrete x-axis, and I'd like to have the x-axis labels be written under each facet rather than only underneath the bottom row of facets. For instance:

# Drop some factor levels to make the plot smaller 
diamondSub <- subset(diamonds, (cut=="Ideal" | cut=="Premium") & 
                     (color=="E" | color=="I"))

# Note that scales="free_x" has no practical effect here
ggplot(diamondSub, aes(x=clarity, y=price)) + 
  geom_blank()+ 
  geom_boxplot() +
  facet_grid(cut~color, scales="free_x")

However, I'd prefer not to use the solution from that post, which was just to use facet_wrap instead of facet_grid, because I prefer the way facet_grid labels the strip text with one variable on top of the columns, and the other variable on the sides of the rows.

Is there a way to get x-axis labels under each facet, when all the x-axes are actually the same, using facet_grid?

解决方案

You can insert a copy of the axes inside the gtable,

library(gtable)
g <- ggplotGrob(p)
# locate the panels
panels <- grep("panel", g$layout$name)
top <- unique(g$layout$t[panels])
# intersperse a copy of the bottom axes
all <- gtable:::rbind_gtable(gtable:::rbind_gtable(g[seq.int(min(top)), ], 
                                                   g[max(top)+1,], "first"), 
                             g[seq(min(top)+1, nrow(g)),], "first")
grid.newpage()
grid.draw(all)

这篇关于为facet_grid图的所有构面强制X轴文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆