在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 代码 x lt c 0 01848598 0 08052353 0 06741172 0 11652034 y lt c 0 4177541 0 4042247 0 3964025 0 4074685 d lt data fr
  • kernlab 中 SVM 训练之外的核矩阵计算

    我正在开发一种新算法 该算法可以生成修改后的核矩阵以用于 SVM 训练 但遇到了一个奇怪的问题 出于测试目的 我比较了使用 kernelMatrix 接口和普通内核接口学习的 SVM 模型 例如 Model with kernelMatri
  • twitterR 和 ROAuth R 软件包安装

    我在安装 CRAN 上的 twitteR 和 RAOuth 软件包时遇到一些问题 我尝试了几种不同的方法 在 Windows 下使用源代码 在 Ubuntu 下使用 RStudio 我尝试了以下命令 sudo apt get install
  • 从命令行运行 R 代码 (Windows)

    我在名为 analysis r 的文件中有一些 R 代码 我希望能够从命令行 CMD 运行该文件中的代码 而无需通过 R 终端 并且我还希望能够传递参数并在我的代码中使用这些参数 例如就像下面的伪代码 C gt execute r scri
  • 为什么 dplyr filter() 不能在函数内工作(即使用变量作为列名)?

    使用 dplyr 函数对数据进行过滤 分组和变异的函数 基本管道序列在函数之外工作得很好 这就是我使用真实列名称的地方 将其放入一个函数中 其中列名称是一个变量 并且某些函数可以工作 但有些函数则不能 尤其是 dplyr filter 例如
  • API 请求和curl::curl_fetch_memory(url, handle = handle) 中的错误:SSL 证书问题:证书已过期

    几天前 我运行了代码几个月 没有任何问题 GET url myurl query 今天我遇到一个错误 Error in curl curl fetch memory url handle handle SSL certificate pro
  • 在 R 中创建虚拟变量,排除某些情况为 NA

    我的数据看起来像这样 V1 V2 A 0 B 1 C 2 D 3 E 4 F 5 G 9 我想创建一个虚拟变量R where 0 1 1 2 3 4 and NA 0 5 9 应该很简单 有人可以帮忙吗 我们可以转换V2 into a fa
  • 使用 R 选择第一个非 NA 值

    df lt data frame ID c 1 1 1 2 3 3 3 test c NA 5 5 6 4 NA 7 3 NA 10 9 我想创建一个名为 value 的变量 它是每个单独 ID 测试的第一个非 NA 值 对于只有NA的个体
  • 在 R 中使用 lapply 绘制多个数据帧

    我正在尝试使用 lapply 函数绘制多个数据帧 每个数据帧一个图 但是尽管有关此主题的所有帖子我都找不到答案 因为我不断收到错误 图的输出列表为空 我的数据结构如下 df1 lt mtcars gt group by cyl gt tal
  • 相当于 min() 的 rowMeans()

    我在 R 邮件列表上多次看到这个问题 但仍然找不到满意的答案 假设我有一个矩阵m m lt matrix rnorm 10000000 ncol 10 我可以通过以下方式获得每行的平均值 system time rowMeans m use
  • 旋转 Markdown 的表格 pdf 输出

    我想将 pdf 上的表格输出旋转 90 度 我正在使用 Markdown 生成报告并kable循环显示表格 如果可以的话我想继续使用kable因为还有很多其他依赖于它的东西我没有包含在这个 MWE 中 这是一个简单的例子 使用iris数据集
  • 如何获得所有大于x且有位置的数字?

    V lt c 1 3 2 4 2 3 1 X lt 3 pos lt V V X pos is 3 3 我需要的是所有 3 个的位置 I need 2 and 6 哪些职位是3 in V Use which pos lt which V 3
  • 所有 x 轴标签未以 45 度显示

    I m having the code as like below But I m not getting all the x axis labels and it is not displaying in 45 degree when I
  • 将不均匀的层次列表转换为数据框

    我认为还没有有人问过这个问题 但是有没有一种方法可以将具有多个级别和不均匀结构的列表的信息组合成 长 格式的数据帧 具体来说 library XML library plyr xml inning lt http gd2 mlb com c
  • python 相当于 R 中的 get() (= 使用字符串检索符号的值)

    在 R 中 get s 函数检索名称存储在字符变量 向量 中的符号的值s e g X lt 10 r lt XVI s lt substr r 1 1 X get s 10 取罗马数字的第一个符号r并将其转换为其等效整数 尽管花了一些时间翻
  • 删除极坐标图边缘的多余空间和圆环

    我有一个极坐标图ggplot2我已经非常接近完成 相当简单的情节 我已经能够在删除矩形边框方面获得帮助 但我不需要删除最后一个范围轮廓与带有方位角标签的绘图周围的环之间的额外空间 我希望该图的边界为 15 000 而不是 15 214 我编
  • 如何在将两根柱子保持在一起的同时熔化柱子?

    我有这种宽格式的数据 我想将其转换为长格式 Cond Construct Line Plant Tube shoot weight shoot Tube root weight root 1 Standard NA NA 2 199 95
  • 如何绘制具有显着性水平的箱线图?

    前段时间问了一个关于绘制箱线图的问题Link1 https stackoverflow com questions 14604439 plot multiple boxplot in one graph 我有一些包含 3 个不同组 或标签
  • 如何修复 R 中 Kaplan Meier 图的风险表计算错误

    以下是一个数据帧 其中 6 个参与者中的每一个都有唯一的 record ID 我想绘制一个生存分析图 其中包含感兴趣事件的复发以及在时间间隔 tstart 到 tstop 内 暴露 药物剂量 数值变量 的时间依赖性协变量 每个参与者的最大
  • 在 Shiny 中的用户会话之间共享反应数据集

    我有一个相当大的反应数据集 该数据集是通过轮询文件然后按预定义的时间间隔读取该文件而派生的 数据更新频繁 需要不断重新加载 诚然 重新加载可以增量完成并附加到 R 中的现有对象 但事实并非如此 然而目前 尽管会话中的数据相同 但此操作是针对

随机推荐