在facet_grid上强制x轴标签ggplot:x轴标签每行不同 [英] Force x-axis labels on facet_grid ggplot: x-axis labels differ per row

查看:172
本文介绍了在facet_grid上强制x轴标签ggplot:x轴标签每行不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很高兴能在帖子中找到解决我问题的大部分内容,为facet_grid图的所有方面强制X轴文本



我想创建一个图形,看起来有点像OP Drew Steen's,除了我有两行以上的小平面,并且我想让每行的x轴标签不同。

我从@ baptiste的真棒答案中提出了一个超级黑客解决方案(主要是因为我不熟悉gtable包),我想知道:


  1. 如果有比我在下面写的乱七八糟的更优雅的解决方案

  2. (中)行。

这里是我从@Drew Steen和@baptiste改编的代码,

  library(ggplot2)

diamondSub< -subset(diamonds,(cut ==Ideal| cu t ==高级| cut ==Very Good)& (color ==E| color ==I))

p < - ggplot(diamondSub,aes(x = carat,y = price))+
geom_blank +
geom_point()+
scale_x_discrete(breaks = c(1,2,3,4),labels = c(a,b,c,d))+
facet_grid(cut〜color,scales =free_x)
p

p2 < - ggplot(diamondSub,aes(x = carat,y = price))+
geom_blank()+
geom_point()+
scale_x_discrete(breaks = c(1,2,3,4),labels = c(f,g,h,i ))+
facet_grid(cut〜color,scales =free_x)
p2

library(gtable)

g< - ggplotGrob p)
g2 < - ggplotGrob(p2)

#找到面板
面板< - grep(panel,g $ layout $ name)
panel2< - grep(panel,g2 $ layout $ name)

top< - unique(g $ layout $ t [panels])
top2< - unique(g2
#散布底层的副本
全部< - gtable ::: rbind_gtable(gtable ::: rbind_gtable(g [seq.int(min(top) ),],
g [max(top)+1,],first),
g2 [seq(min(top2)+1,nrow(g2)),],first)
grid.newpage )
grid.draw(全部)

解决方案

strong>(2015年4月24日添加),显示元素的手动构建并包含更多评论。

  ##替代版本
库(gtable)
库(网格)

#获取ggplot grobs
g< - ggplotGrob(p)
g2< - ggplotGrob(p2)

#显示布局。
#注意行和列
#在这种情况下,g2布局与g布局相同
gtable_show_layout(g)

#大面板是情节板。
#我们需要第4行,第6行和第8行。

#对于顶部的非常好行,我们还需要行1,2和3.

#该轴位于第9行

#因此,在第1行,第2行,第3行和第4行中将grob与第9行中的grob绑定在一起。
top.row< ; - rbind(g [1:4,],g [9,],size =first)

#第二个Premium行
#我们需要行中的面板6加上上面的小间隙行
#并将其与轴
中的行联系起来
middle.row = rbind(g2 [5:6,],g2 [9,],size =first)

#底部理想行
#我们需要第8行的面板加上
#上面的行中的小间隙加上
下面一行中的轴#加上下面的行(轴标签和边距)
bottom.row = g2 [7:11,]


#rbind三行
all< - rbind(rbind(top.row,middle.row,size =first),bottom.row,size =first)

#绘制
grid.newpage()
grid.draw(全部)

#也许在行之间添加更多空间
gtable_show_layou t(全部)
全部$高度[c(6,9)] =单位(1,lines)

#绘制
grid.newpage()
grid.draw(全部)

但是,您甚至不需要执行面板到轴;只需从布局中选择适当的行:

  top.row < -  g [c(1:4,9), ] 
middle.row = g2 [c(5:6,9),]
bottom.row = g2 [7:11,]


原始版本



只有在行的绑定中,你犯了一个错误。您已将轴绑定到顶部的非常好行。底部的理想行已经有一个轴,所以不需要绑定。修复:中间的Premium行需要一个轴。

我分别构建了每行,将一个轴绑定到顶部和中间行,然后绑定这三行。

我增加了另一步在行之间添加更多空间。

  g < -  ggplotGrob(p)
g2 < - ggplotGrob(p2)

#找到面板
面板< - grep(panel,g $ layout $ name)
panels2< - grep(panel,g2 $ layout $ name)

top < - unique(g $ layout $ t [panels])
top2< - unique(g2 $ layout $ t [panels2])

#构建每一行分别为
top.row < - gtable ::: rbind_gtable(g [seq.int(min(top)),],g [max(top)+1,],first)
middle.row < - gtable ::: rbind_gtable(g2 [c(top [2] -1,top [2]),],g2 [max(top)+1,],first)
bottom.row < - g2 [(max(top2)-1):nrow(g2),]

全部< - gtable ::: rbind_gtable(gtable ::: rbind_gtable(top.row ,middle.row,first),bottom.row,first)

#绘制
grid.newpage()
grid.draw(全部)

#也许在行之间增加一点空间
all $ heights [c(6,9)] = unit(1,lines)

grid。 newpage()
grid.draw(全部)


I was so happy to find the greater part of a solution to my question in the post, "Force X axis text on for all facets of a facet_grid plot".

I'd like to create a graph to look somewhat like the OP Drew Steen's, except I have more than two rows of facets, and I'd like to make the x-axes labels different for each row.

I made a super-hacky solution out of @baptiste's awesome answer (mostly because I am unfamiliar with the gtable package), and I'd like to know:

  1. If there is a more elegant solution than the mess I wrote below
  2. How to insert labels for the "Premium" (middle) row.

Here is the code I adapted from @Drew Steen and @baptiste,

library(ggplot2)

diamondSub <-subset(diamonds, (cut=="Ideal" | cut=="Premium" | cut == "Very Good") & (color=="E" | color=="I"))

p<- ggplot(diamondSub, aes(x=carat, y=price)) + 
  geom_blank()+ 
  geom_point() +
  scale_x_discrete(breaks=c(1, 2, 3, 4), labels=c("a", "b", "c", "d")) +
  facet_grid(cut~color, scales="free_x")
p

p2<- ggplot(diamondSub, aes(x=carat, y=price)) + 
  geom_blank()+ 
  geom_point() +
  scale_x_discrete(breaks=c(1, 2, 3, 4), labels=c("f", "g", "h", "i")) +
  facet_grid(cut~color, scales="free_x")
p2

library(gtable)

g <- ggplotGrob(p)
g2 <- ggplotGrob(p2)

# locate the panels
panels <- grep("panel", g$layout$name)
panels2 <- grep("panel", g2$layout$name)

top <- unique(g$layout$t[panels])
top2 <- unique(g2$layout$t[panels2])
# intersperse a copy of the bottom axes
all <- gtable:::rbind_gtable(gtable:::rbind_gtable(g[seq.int(min(top)), ], 
                                                   g[max(top)+1,], "first"), 
                             g2[seq(min(top2)+1, nrow(g2)),], "first")
grid.newpage()
grid.draw(all)

解决方案

Alternative Version (added 24 April 2015) showing manual construction of the elements and containing more commenting.

## Alternative version
library(gtable)
library(grid)

# Get the ggplot grobs
g <- ggplotGrob(p)
g2 <- ggplotGrob(p2)

# Show the layout.
# Note the rows and columns
# In this case, the g2 layout is the same as the g layout
gtable_show_layout(g)

# The large panels are the plot panels.
# We will need rows 4, 6 and 8.

# For the top "Very Good" row, we will also need rows 1, 2, and 3.

# The axis is located in row 9

# Therefore rbind the grob in rows 1, 2, 3, and 4, with the grob in row 9.
top.row <- rbind(g[1:4, ], g[9, ], size = "first")

# The second "Premium" row
# We need the panels in row 6 plus the small gap row above,
# and rbind that to the axis
middle.row = rbind(g2[5:6, ], g2[9,], size = "first")

# The bottom "Ideal" row
# We need the panel in row 8 plus the small gap in the row above
# plus the axis in the row below
# plus rows below that (axis label and margin)
bottom.row = g2[7:11, ]


# rbind the three rows 
all <- rbind(rbind(top.row, middle.row, size = "first"), bottom.row, size = "first")

# Draw it
grid.newpage()
grid.draw(all)

# Maybe add a little more space between the rows
gtable_show_layout(all)
all$heights[c(6,9)] = unit(1, "lines")

# Draw it
grid.newpage()
grid.draw(all)

But you don't even need to do the binding of the panel to the axis; just select the appropriate rows from the layout:

top.row <- g[c(1:4, 9), ]
middle.row = g2[c(5:6, 9), ]
bottom.row = g2[7:11, ]

then bind these three new rows.

Original Version

It is only in the binding of the rows that you have made a mistake. You have bound an axis to the top "Very Good" row. The bottom "Ideal" row already has an axis, so no binding is required. To Fix: The middle "Premium" row needs an axis.

I have constructed each row separately, binding an axis to the top and middle rows, then binding the three rows.

I've added another step to add a little more space between the rows.

g <- ggplotGrob(p)
g2 <- ggplotGrob(p2)

# locate the panels
panels <- grep("panel", g$layout$name)
panels2 <- grep("panel", g2$layout$name)

top <- unique(g$layout$t[panels])
top2 <- unique(g2$layout$t[panels2])

# Construct each row separately
top.row <- gtable:::rbind_gtable(g[seq.int(min(top)), ], g[max(top)+1,], "first")
middle.row <- gtable:::rbind_gtable(g2[c(top[2]-1,top[2]), ], g2[max(top)+1,], "first")
bottom.row <- g2[(max(top2)-1):nrow(g2), ]

all <- gtable:::rbind_gtable(gtable:::rbind_gtable(top.row, middle.row, "first"), bottom.row, "first")

# Draw it
grid.newpage()
grid.draw(all)

# Maybe add a little more space between the rows
all$heights[c(6,9)] = unit(1, "lines")

grid.newpage()
grid.draw(all)

这篇关于在facet_grid上强制x轴标签ggplot:x轴标签每行不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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