gpt4 book ai didi

r - 使用 strip.y 作为 strip.x 而不翻转 facet_grid 轴

转载 作者:行者123 更新时间:2023-12-04 10:12:05 28 4
gpt4 key购买 nike

代码

library(ggplot2)
library(dplyr)

mydata = tribble(
~x, ~y, ~data, ~more,
0, 50, 'iris', 'this',
10, 100, 'iris', 'this',
0, 50, 'iris', 'that',
10, 100, 'iris', 'that',
0, 0, 'wine', 'this',
10, 10, 'wine', 'this',
0, 0, 'wine', 'that',
10, 10, 'wine', 'that',
)

ggplot(mydata, aes(x,y)) + facet_grid(data~more, scale='free') + theme_gray() + geom_point()

ggplot(mydata, aes(x,y)) + facet_grid(more~data, scale='free') + theme_gray() + geom_point()

我正在获取图形:

enter image description here

enter image description here

请注意,第一个图表填充面板中的空间有多好。由于我的真实情节的颜色编码,我不需要显示“这个”和“那个”,所以我想在第一个情节中去掉它。但是旁边的数据标签很奇怪。我更希望他们是这样的:

enter image description here

这类似于 ggplot2: Using gtable to move strip labels to top of panel for facet_grid ,但其他链接对我不起作用,我认为情况有点不同。

我该怎么做?

最佳答案

要让 strip 跨越列,您可能需要使用 gtable 函数(以及几个 grid 函数)。

在解决方案中,行被添加到图表的 gtable,然后条被添加到新行。 strip 可以从头开始构造,但绘制第二张 strip 水平方向的图表可能更容易,然后提取 strip 。

(注意:这很容易与下一个 ggplot 版本冲突。)

library(ggplot2)
library(dplyr)


mydata = tribble(
~x, ~y, ~data, ~more,
0, 50, 'iris', 'this',
10, 100, 'iris', 'this',
0, 50, 'iris', 'that',
10, 100, 'iris', 'that',
0, 0, 'wine', 'this',
10, 10, 'wine', 'this',
0, 0, 'wine', 'that',
10, 10, 'wine', 'that',
)

### Construct two charts:
# 1. ToKeep - the data~more chart, but without the strip labels.
# The strips across the tops of the two rows of panels will be added later.

(ToKeep <- ggplot(mydata, aes(x,y)) +
facet_grid(data ~ more, scale ='free') +
geom_point() +
theme(strip.text.y = element_blank(),
strip.text.x= element_blank()) )

# 2. ToGetStrip - Get the strips from the more~data chart
ToGetStrip <- ggplot(mydata, aes(x,y)) +
facet_grid(more ~ data, scale = 'free') +
geom_point()

# Get the ggplot grob
gt = ggplotGrob(ToGetStrip)

# Get the strips
library(gtable)
library(grid)
strip = gtable_filter(grid.force(gt), "strip-t")


# Make sure
grid.newpage()
grid.draw(strip$grobs[[1]])

grid.newpage()
grid.draw(strip$grobs[[2]])


## Add the strips to the ToKeep chart.

# Get ggplot grob
gt = ggplotGrob(ToKeep)

# Check heights and widths
gt$heights
gt$widths

# The 1null heights and widths are the panels' heights and widths.
# Add rows to the gtable immediately above the panel rows (that is, after row 9 then row 7).
# The height of the new rows is the height of the strip grobs.

gt <- gtable_add_rows(gt, height = strip$grobs[[1]]$height, pos = 9)
gt <- gtable_add_rows(gt, height = strip$grobs[[1]]$height, pos = 7)

# Add the strips to the new rows ( t = c(8, 11) )
# and make sure they span the two columns of panels ( l = 5, r = 7 )
gt <- gtable_add_grob(gt, list(strip$grobs[[1]], strip$grobs[[2]]), t = c(8, 11), l = 5, r = 7)

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

enter image description here

以下是尝试根据面板位置选择行和列。然而,它还没有在你的 2 X 2 图表之外进行测试。 (这可能对 ggplot 的更改更具弹性。)

# Construct the two charts
ToKeep <- ggplot(mydata, aes(x,y)) +
facet_grid(data ~ more, scale ='free') +
geom_point() +
theme(strip.text.y = element_blank(),
strip.text.x= element_blank())


ToGetStrip <- ggplot(mydata, aes(x,y)) +
facet_grid(more ~ data, scale = 'free') +
geom_point()

# Get the strips from the second chart
gt = ggplotGrob(ToGetStrip)

library(grid)
library(gtable)
strip = gtable_filter(grid.force(gt), "strip-t")


# Add rows to the ToKeep chart, and add the strips to the new rows
gt = ggplotGrob(ToKeep)
PanelPos = subset(gt$layout, grepl("panel", gt$layout$name), select = t:r)

PanelRows = rev(unique(PanelPos$t))
for(i in seq_along(PanelRows)) {
gt <- gtable_add_rows(gt, height = strip$grobs[[1]]$height, pos = PanelRows[i]-1)
}

PanelRows = unique(subset(gt$layout, grepl("panel", gt$layout$name), select = t, drop = TRUE))
gt <- gtable_add_grob(gt, strip$grobs, t = PanelRows - 1, l = min(PanelPos$l), r = max(PanelPos$r))

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

关于r - 使用 strip.y 作为 strip.x 而不翻转 facet_grid 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52458376/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com