在facet_grid ggplot上强制x轴标签:x轴标签每行不同

2023-12-05

我很高兴在帖子中找到了我的问题的大部分解决方案,"强制为facet_grid 图的所有方面启用X 轴文本".

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

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

  1. 如果有比我下面写的混乱更优雅的解决方案
  2. 如何为“Premium”(中间)行插入标签。

这是我改编自@Drew Steen 和@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)

see graph


替代版本(2015 年 4 月 24 日添加)显示了元素的手动构建并包含更多评论。

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

但您甚至不需要将面板绑定到轴上;只需从布局中选择适当的行:

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)

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

enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在facet_grid ggplot上强制x轴标签:x轴标签每行不同 的相关文章

  • R,使用具有两种以上可能性的二项式分布

    我知道这可能是基本的 但我似乎有一个心理障碍 假设您想要计算在一个骰子上掷出 4 5 或 6 的概率 在 R 中 这很简单 sum 1 6 1 6 1 6 这给出了 1 2 这是正确答案 然而 我内心深处 可能应该保留的地方 认为我应该能够
  • 在 RMarkdown 输出到 PDF 时缩进而不添加项目符号点或编号

    之前有人问过如何在没有项目符号的情况下缩进文本 RMarkdown 中的点 但这是针对 HTML 输出的 在 RMarkdown 中缩进而不添加项目符号点或数字 https stackoverflow com questions 47087
  • 增加雷达图中长轴标签的空间

    我想创建一个雷达图ggirahExtra ggRadar 问题是我的标签很长并且被剪掉了 我想我可以通过添加在标签和绘图之间创建更多空间margin margin 0 0 2 0 cm to element text in axis tex
  • R中IF函数的使用

    我正在短跑ifR 中的函数 但收到以下警告消息 In if runif 50 0 1 lt 0 69 the condition has length gt 1 and only the first element will be used
  • ggplot:如何限制条形图中的输出,以便仅显示最频繁出现的情况?

    我几个小时以来一直在寻找这个简单的东西 但没有结果 我有一个数据框 其中一列为变量 国家 地区 我想要两件事以下 绘制最常见的国家 地区 最常见的位于顶部 找到部分解决方案EDIT找到完整的解决方案 gt gt 重点问题是根据频率限制条形图
  • 需要在R中跳过不同数量的行

    我正在使用以下代码来处理我的数据 但最近我意识到使用skip 27 在数据开始之前跳过存储在我的文件中的信息 不是一个好的选择 因为每个文件中要跳过的行数不同我的目标是读取存储在多个文件夹中的各种txt文件 并非所有文件都有相同的列数 列的
  • case_when 与部分字符串匹配和 contains()

    我正在使用一个数据集 其中有许多名为 status1 status2 等的列 在这些列中 它表示某人是否豁免 完整 注册等 不幸的是 豁免投入并不一致 这是一个示例 library dplyr problem lt tibble perso
  • R:如何将字符/数字转为1,NA转为0?

    有没有一种简单的方法可以将列的字符 数字变为 1 将 NA 变为 0 这里有一些示例数据 我想将其应用于 3 4 structure list Item Code c 176L 187L 191L 201L 217L 220L Item x
  • 平滑连续 2D 点

    UPDATE 感谢 user20650和 李哲源Zheyuan Li 这是我想出的解决方案 Example data set df 3600 observations points Create a vector of the cumula
  • 使用数据帧的 R 中的 EWMA 波动性

    我正在尝试从一系列股票每日收益中获取 EWMA 波动性 这些收益来自一个名为base retorno diario Data IBOV ABEV3 AEDU3 ALLL3 BBAS3 BBDC3 BBDC4 1 2000 01 04 0 0
  • R:单纯形错误:在下标赋值中不允许使用 NA

    对于以下具有目标函数和约束的最小化 boot simplex返回错误 Error in tab pr lt tab pr tab pr pc pv o tab pr NAs are not allowed in subscripted as
  • 计算 R 行中的非零条目数

    我有以下类型的数据 mode1 mode2 mode3 1 8 1 0 2 0 0 0 3 6 5 4 4 1 2 3 5 1 1 1 数据使用dput structure list mode1 c 8L 0L 6L 1L 1L mode2
  • 如何像在facet_grid中一样在facet_wrap中定位条带标签

    我想在使用时删除多余的条带标签facet wrap 并用两个变量进行分面 并且都是自由尺度的 例如 这个facet wrap下图的版本 library ggplot2 dt lt txhousing txhousing year in 20
  • 无法将“gather”输出的列名称更改为默认名称以外的任何名称

    我正在尝试使用gather in the tidyr包 但我无法更改默认名称的输出列名称 例如 df data frame time 1 100 a 1 100 b 101 200 df long df gt gather foo bar
  • 在闪亮的数据表中为每个单元格显示工具提示或弹出窗口?

    有没有什么方法可以为 r闪亮数据表中的每个单元格获取工具提示 有很多方法可以获取悬停行或列 但我找不到一种方法来获取行和列索引并为每个单元格显示不同的悬停工具提示 任何人都可以修改以下代码吗 library shiny library DT
  • ggplot2:带有 geom_line 的 x 轴因子不起作用

    我想要一个线图 其中value绘制为函数expt每级一行var 这是我的数据 lines lt expt var value 1 none p 0 183065327746799 2 none p 0 254234138384241 3 n
  • 安装 2.15 后 ggplot2 中的 alpha 通道不起作用

    更新到 R 2 15 后 ggplot 中的 alpha 通道似乎不再起作用 plot rnorm 100 rnorm 100 bg cc000055 pch 21 工作得很好但是 qplot rnorm 100 rnorm 100 col
  • 使用“assign()”为列表项分配值

    首先了解一些背景 我写了一个中缀函数 本质上取代了这个习惯用法 x length x 1 lt y 或者简单地说x lt append x y 对于向量 这里是 lt function x y xcall lt substitute x x
  • 将 Excel 文件读入 R 并锁定单元格

    我有一个 Excel 电子表格要读入 R 它受密码保护并锁定了单元格 我可以使用 excel link 导入受密码保护的文件 但我不知道如何解锁 取消保护单元格 excel link 给了我这个错误 gt
  • 闪亮井板宽度

    library shiny library shinydashboard ui lt dashboardPage dashboardHeader dashboardSidebar dashboardBody wellPanel tags d

随机推荐