每个子图的y轴使用facet_grid [英] y-axis for each subplot using facet_grid

查看:234
本文介绍了每个子图的y轴使用facet_grid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法得到这个问题的答案。
我和用户都希望在使用facet_grid()时为所有列添加轴刻度和标签。



或者,将轴放置在与原始y轴宽度相同的视口中,但使用右对齐。然后,将生成的grob添加到面板之间的现有边界列中,调整这些列的宽度以适合它们。

  require ggplot2)
require(grid)
require(gtable)
data(iris)
iris $ category = rep(letters [1:4],length.out = 150)
plot1 = ggplot(data = iris,aes(x = 1,y = Sepal.Width))+
geom_boxplot()+
facet_grid(物种分类)

#获取ggplot grob
g< - ggplotGrob(plot1)

#获取yaxis
轴< - gtable_filter(g,axis-l)

#获取y轴的宽度
宽度=轴宽度

#将轴放入一个视口中,宽度等于原始y轴的
#材料,
#并定位为右对齐
轴$ vp = viewport(x = unit(1,npc),width = Widths,just =right)

#将y轴添加到面板之间的现有边界列
面板<-g $ layout [grepl(panel,g $ layout $ name),]
posx = unique(panels $升)[ - 1] - 1
posy =唯一(panel $ t)
$ bg = gtable_add_grob(g,rep(list(axis),length(posx)),
t = rep(min(posy) ),length(posx)),b = rep(max(posy),length(posx)),l = posx)

#增加边距列的宽度
g $ widths [ posx]< - unit(25,pt)
#或者增加plot1的原始构造的面板边距的宽度

#绘制它
grid.newpage( )
grid.draw(g)


I can't get the answer to this question to work. What both me and that user want is to add axis ticks and labels to all columns when using facet_grid().

Display y-axis for each subplot when faceting

When I run the reproducable example and the solution (after adding abc=as.data.frame(abc) to fix the initial error) I receive an error message

Error in gtable_add_grob(g, grobs = list(segmentsGrob(1, 0, 1, 1), segmentsGrob(1, : Not all inputs have either length 1 or same length same as 'grobs

I made my own reproducible example because the original one is ehhm, a bit odd :-). It results in the same error message

require(ggplot2)
require(reshape)
require(grid)
require(gtable)
data(iris)
iris$category=rep(letters[1:4],length.out=150)
plot1=ggplot(data=iris,aes(x=1,y=Sepal.Width))+geom_boxplot()+facet_grid(Species~category)

The answer should be this:

g <- ggplotGrob(plot1)

require(gtable)
axis <- gtable_filter(g, "axis-l")[["grobs"]][[1]][["children"]][["axis"]][,2]
segment <- segmentsGrob(1,0,1,1)
panels <- subset(g$layout, name == "panel")
g <- gtable_add_grob(g, grobs=list(axis, axis), name="ticks",
                     t = unique(panels$t), l=tail(panels$l, -1)-1)

g <- gtable_add_grob(g, grobs=list(segmentsGrob(1,0,1,1), 
                                   segmentsGrob(1,0,1,1)), 
                     t = unique(panels$t), l=tail(panels$l, -1)-1, 
                     name="segments")

解决方案

The answer you refer to does not apply to your situation.

To get nice placement of the tick marks and tick mark labels, I would add columns to the gtable to take the axis material. The new columns have the same width as the original y axis.

You might want to add more margin space between the panels. Do so with theme(panel.margin.x = unit(1, "lines")).

require(ggplot2)
require(grid)
require(gtable)
data(iris)
iris$category = rep(letters[1:4], length.out = 150)
plot1 = ggplot(data = iris, aes(x = 1, y = Sepal.Width))+
   geom_boxplot()+
   facet_grid(Species~category)

# Get the ggplot grob
g <- ggplotGrob(plot1)

# Get the yaxis
yaxis <- gtable_filter(g, "axis-l")

# Get the width of the y axis
Widths = yaxis$widths

# Add columns to the gtable to the left of the panels, 
# with a width equal to yaxis width
panels <- g$layout[grepl("panel", g$layout$name), ]
pos = rev(unique(panels$l)[-1] - 1)
for(i in pos) g = gtable_add_cols(g, Widths, i)

# Add y axes to the new columns
panels <- g$layout[grepl("panel", g$layout$name), ]
posx = rev(unique(panels$l)[-1] - 1)
posy = unique(panels$t)

g = gtable_add_grob(g, rep(list(yaxis), length(posx)), 
     t = rep(min(posy), length(posx)), b = rep(max(posy), length(posx)), l = posx)

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

Alternatively, place the axis in a viewport of the same width as the original y axis, but with right justification. Then, add the resulting grob to the existing margin columns between the panels, adjusting the width of those columns to suit.

require(ggplot2)
require(grid)
require(gtable)
data(iris)
iris$category = rep(letters[1:4], length.out = 150)
plot1 = ggplot(data = iris, aes(x = 1, y = Sepal.Width))+
   geom_boxplot() + 
   facet_grid(Species ~ category ) 

# Get the ggplot grob
g <- ggplotGrob(plot1)

# Get the yaxis
axis <- gtable_filter(g, "axis-l")

# Get the width of the y axis
Widths = axis$width

# Place the axis into a viewport, 
# of width equal to the original yaxis material,
# and positioned to be right justified
axis$vp = viewport(x = unit(1, "npc"), width = Widths, just = "right")

# Add y axes to the existing margin columns between the panels
panels <- g$layout[grepl("panel", g$layout$name), ] 
posx = unique(panels$l)[-1] - 1
posy = unique(panels$t)

g = gtable_add_grob(g, rep(list(axis), length(posx)), 
     t = rep(min(posy), length(posx)), b = rep(max(posy), length(posx)), l = posx)

# Increase the width of the margin columns
g$widths[posx] <- unit(25, "pt") 
# Or increase width of the panel margins in the original construction of plot1

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

这篇关于每个子图的y轴使用facet_grid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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