簡體   English   中英

使用facet_grid的每個子圖的y軸

[英]y-axis for each subplot using facet_grid

我無法解決該問題的答案。 我和該用戶都希望在使用facet_grid()時向所有列添加軸刻度和標簽。

在構面時顯示每個子圖的y軸

當我運行可重現的示例和解決方案時(添加abc = as.data.frame(abc)以修復初始錯誤后),我收到一條錯誤消息

gtable_add_grob(g,grobs = list(segmentsGrob(1,0,1,1),segmentGrob(1,:)中的錯誤:並非所有輸入的長度都等於1或與'grobs相同

我做了一個可復制的示例,因為原始示例是ehhm,有點奇怪:-)。 結果相同的錯誤信息

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)

答案應該是這樣的:

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")

這就是我的想法(使用ggplot2_2.1.0):

g <- ggplotGrob(plot1)
axis <- gtable_filter(g, "axis-l")
newG <- gtable_add_grob(g, list(axis, axis, axis), 
                t = rep(4, 3), b = rep(8, 3), l = c(5, 7, 9))
grid.draw(newG)

..哪個看起來像這樣:

在此處輸入圖片說明

這是我經歷的過程:

  1. g <- ggplotGrob(plot1)創建一個gtable。
  2. print(g)查看gtable的元素...我正在尋找我想弄亂的grob的名稱。 在這里,這是三個被稱為“ axis-1”的小怪人。
  3. axis <- gtable_filter(g, "axis-l")我從較大的gtable對象g選擇了三個grob,並將它們保存在稱為axis的gtable中。 請注意, gtable_filter實際上是在選擇grob,而不是從g過濾它們。
  4. gtable_show_layout(g)看過來的布局g這樣我就可以揣摩出我希望把axis的關系,整體劇情。
  5. gtable_add_grob等。現在,我知道我要去哪里了,可以在原始圖上附加axis

我認為在涉及gtable時,這些步驟是非常常見的工作流程。 當然,您可能還會遇到其他一些問題。 例如,在這種情況下,為除最左邊的y軸標簽之外的所有標簽提供的空間不足。 所以也許就是:

newG$widths[c(5, 7, 9)] <- grid:::unit.list(axis$widths) # you won't need to wrap this in grid
grid.draw(newG)

在此處輸入圖片說明

您所指的答案不適用於您的情況。

為了更好地放置刻度線和刻度線標簽,我將在gtable中添加列以獲取軸材質。 新列的寬度與原始y軸的寬度相同。

您可能要在面板之間添加更多的空白空間。 使用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)

在此處輸入圖片說明

或者,將軸放置在與原始y軸相同寬度的視口中,但右對齊。 然后,將生成的grob添加到面板之間的現有margin列中,調整這些列的寬度以適合。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM