ggplot:如何使用facet_grid创建不同的x轴标题

2023-12-11

我有两个共享 y 轴但具有不同 x 轴的图。我使用facet_grid将它们适当地分开(见图),但是两个x轴需要有不同的标题(而不是一个标题“Num Cell Lines.Tissue”)。我见过类似的方法,首先创建 2 个 ggplot 对象,然后使用 ggplotGrob 这样的函数。有没有办法直接做到这一点?

Edit 1:包含数据、代码

Data:

Variable  Condition  Num.CellLines.Tissue   Percent.Altered
V1        C1                 1              0.20149254
V1        C1                 2              0.03731343
V1        C1                 3              0
V1        C1                 4              0
V1        C1                 5              0
V2        C2                 1              0.74893617
V2        C2                 2              0.37446809
V2        C2                 3              0.16595745
V2        C2                 4              0.09787234
V2        C2                 5              0.06808511
V2        C2                 6              0.05531915
V2        C2                 7              0.02553191
V2        C2                 8              0.01702128
V2        C2                 9              0.01276596
V2        C2                10              0.00851064
V2        C3                 1              0.88554217
V2        C3                 2              0.68072289
V2        C3                 3              0.40361446
V2        C3                 4              0.22289157
V2        C3                 5              0.11445783
V2        C3                 6              0.06626506
V2        C3                 7              0.04819277
V2        C3                 8              0.01807229
V2        C3                 9              0.01807229
V2        C3                10              0.01204819
V2        C4                 1              0.87301587
V2        C4                 2              0.6984127
V2        C4                 3              0.52380952
V2        C4                 4              0.38095238
V2        C4                 5              0.25925926
V2        C4                 6              0.14285714
V2        C4                 7              0.07407407
V2        C4                 8              0.04232804
V2        C4                 9              0.03703704
V2        C4                10              0.03174603

Code:

ggplot(data, aes(y=Percent.Altered, x = Num.CellLines.Tissue, color= Condition )) + geom_line(size=1) + facet_grid(. ~ Variable, scales="free_x")

enter image description here

Edit 2:理想情节的形象

标签 V1 和 V2 默认按照我想要的方式显示,并且它们与它们下方的 x 轴标题不同。

enter image description here


我们可以用switch = 'x'然后放置strip.placement = "outside"

library(tidyverse)

text = "Variable  Condition  Num.CellLines.Tissue   Percent.Altered
V1        C1                 1              0.20149254
V1        C1                 2              0.03731343
V1        C1                 3              0
V1        C1                 4              0
V1        C1                 5              0
V2        C2                 1              0.74893617
V2        C2                 2              0.37446809
V2        C2                 3              0.16595745
V2        C2                 4              0.09787234
V2        C2                 5              0.06808511
V2        C2                 6              0.05531915
V2        C2                 7              0.02553191
V2        C2                 8              0.01702128
V2        C2                 9              0.01276596
V2        C2                10              0.00851064
V2        C3                 1              0.88554217
V2        C3                 2              0.68072289
V2        C3                 3              0.40361446
V2        C3                 4              0.22289157
V2        C3                 5              0.11445783
V2        C3                 6              0.06626506
V2        C3                 7              0.04819277
V2        C3                 8              0.01807229
V2        C3                 9              0.01807229
V2        C3                10              0.01204819
V2        C4                 1              0.87301587
V2        C4                 2              0.6984127
V2        C4                 3              0.52380952
V2        C4                 4              0.38095238
V2        C4                 5              0.25925926
V2        C4                 6              0.14285714
V2        C4                 7              0.07407407
V2        C4                 8              0.04232804
V2        C4                 9              0.03703704
V2        C4                10              0.03174603"

data <- read.table(text = text, header = TRUE)
head(data)
#>   Variable Condition Num.CellLines.Tissue Percent.Altered
#> 1       V1        C1                    1      0.20149254
#> 2       V1        C1                    2      0.03731343
#> 3       V1        C1                    3      0.00000000
#> 4       V1        C1                    4      0.00000000
#> 5       V1        C1                    5      0.00000000
#> 6       V2        C2                    1      0.74893617

ggplot(data, aes(y = Percent.Altered, x = Num.CellLines.Tissue, color = Condition )) + 
  geom_line(size = 1) + facet_grid(. ~ Variable, scales = "free_x", switch = 'x') +
  theme_classic(base_size = 16) +
  theme(strip.placement = "outside") +
  theme(axis.title.x = element_blank(),
        strip.background = element_blank())

# Append the original x-axis label. Code taken from the ref below 
# http://ggplot2.tidyverse.org/reference/as_labeller.html
appender <- function(string, prefix = "Num.CellLines.Tissue: ") paste0(prefix, string)
ggplot(data, aes(y = Percent.Altered, x = Num.CellLines.Tissue, color = Condition )) + 
  geom_line(size = 1) + 
  facet_grid(. ~ Variable, 
             labeller = as_labeller(appender),
             scales = "free_x", switch = 'x') +
  theme_classic(base_size = 16) +
  theme(strip.placement = "outside") +
  theme(axis.title.x = element_blank(),
        strip.background = element_blank())  

根据OP的新图进行编辑,我们需要绘制V1 & V2分别然后使用合并在一起cowplot::plot_grid功能:

# V1
p1 <- ggplot(data %>% filter(Variable == "V1"), 
             aes(y = Percent.Altered, x = Num.CellLines.Tissue, color = Condition )) + 
  geom_line(size = 1) + 
  facet_grid(. ~ Variable, scales = "free_x") +
  scale_y_continuous(limits = c(0, 1), expand = c(0, 0)) +
  theme_classic(base_size = 16) +
  theme(
    legend.position = "none", 
    plot.margin = unit(c(0, 0, 0, 0), "cm")) +
  scale_color_brewer(palette = "Set1") +
  xlab("# Tissues")

# V2
p2 <- ggplot(data %>% filter(Variable == "V2"), 
             aes(y = Percent.Altered, x = Num.CellLines.Tissue, color = Condition )) + 
  geom_line(size = 1) + 
  facet_grid(. ~ Variable, scales = "free_x") +
  scale_y_continuous(limits = c(0, 1), expand = c(0, 0)) +
  theme_classic(base_size = 16) +
  theme(
    legend.position = "none", 
    plot.margin = unit(c(0, 0, 0, 0), "cm")) +
  scale_color_brewer(palette = "Dark2") +
  xlab("# Cell Lines") + ylab("")

# Remove the y-axis for the 2nd plot - p2 then merge 2 plots
cowplot::plot_grid(p1, 
                   p2 + 
                     theme(axis.text.y = element_blank(),
                           axis.line.y = element_blank(),
                           axis.title.y= element_blank(),
                           axis.ticks.y= element_blank()),
                   nrow = 1,
                   rel_widths = c(1.2, 1),
                   align = 'h', axis = 'tb')

创建于 2018-03-04代表包(v0.2.0)。

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

ggplot:如何使用facet_grid创建不同的x轴标题 的相关文章

  • 将数据框中的每个 x 个字符拆分为字符串

    我知道这里有一些关于每隔一段时间分割一个字符串的答案nth字符 例如this one https stackoverflow com questions 23208490 split each character in r and this
  • 如何使用 usmap 标记数字而不是名称?

    我知道 usmap 有一个选项label in plot usmap 我想标记一些数字 而不是状态名称 我想 usmap 中应该有与州质心坐标相关的数据 但我不知道如何找到它 如果我能得到 坐标然后我可以用它来标记数字geom text 这
  • 绘制点之间的所有线

    我有以下 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
  • Dendextend:关于如何根据定义的组为树状图的标签着色

    我正在尝试使用一个名为 dendextend 的很棒的 R 包来绘制树状图并根据一组先前定义的组为其分支和标签着色 我已阅读您在 Stack Overflow 中的答案以及 dendextend vignette 的常见问题解答 但我仍然不
  • twitterR 和 ROAuth R 软件包安装

    我在安装 CRAN 上的 twitteR 和 RAOuth 软件包时遇到一些问题 我尝试了几种不同的方法 在 Windows 下使用源代码 在 Ubuntu 下使用 RStudio 我尝试了以下命令 sudo apt get install
  • 将数据框中重叠的范围合并到唯一的组中

    我有一个 n 行 3 的数据框 df lt data frame start c 178 400 983 1932 33653 end c 5025 5025 5535 6918 38197 group c 1 1 2 2 3 df sta
  • 更改闪亮 R 中的默认浏览器

    我在 RStudio 中使用 01 hello 虽然在 IE 中默认打开程序时它不会显示直方图 但即使在 Chrome 中 滑块也不起作用 我无法滑动条形图并看到直方图中的变化 如何更改 R 中的默认浏览器 以便闪亮启动 Chrome 而不
  • 在 r 中的 group_by 之后建模后取消列表列的嵌套

    我想对所有组进行线性回归group by 将模型系数保存在列表列中 然后使用 unnest 扩展列表列 这里我用的是mtcars以数据集为例 注 我想用do here becausebroom tidy 不适用于所有型号 mtcars gt
  • 相当于 min() 的 rowMeans()

    我在 R 邮件列表上多次看到这个问题 但仍然找不到满意的答案 假设我有一个矩阵m m lt matrix rnorm 10000000 ncol 10 我可以通过以下方式获得每行的平均值 system time rowMeans m use
  • 如何仅删除单括号并保留配对的括号

    你好 我亲爱的老师 R 用户朋友们 我最近开始认真学习正则表达式 最近我遇到了一种情况 我们只想保留配对括号 并省略未配对的 这是我的样本数据 structure list t1 c Book Pg 1 Website Online Jou
  • 如何获得所有大于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
  • ggplot2:如何标记事件发生的日期

    我想从第二个情节中获取第一个情节的信息 第二张图表示事件发生的天数 它看起来更宽 因为它没有图例 但它是相同的时间尺度 我选择在第一个图中手动分配颜色 I would like to overlay the second plot dots
  • R“错误:“}”中出现意外的“}”[重复]

    这个问题在这里已经有答案了 我有一个字符串变量 对于缺少数据的情况 它具有 空值 我想将 空值 重新编码为缺失 而不是说 空值 我正在尝试编写一个循环来删除这些 空值 条目 但我不断收到错误 错误 中出现意外的 for row in dat
  • 以编程方式将字符串宽度值插入到 sprintf() 中

    我正在尝试以编程方式将字符串宽度值插入到sprintf 格式 期望的结果是 sprintf 20s hello 1 hello 但我想插入20在同一通话中即时进行 因此它可以是任何号码 我努力了 sprintf ds 20 hello 1
  • R:使用 tidyverse 将 NA 替换为 df 中的其他变量

    我想使用 tidyverse 替换 df 中的 NA 值 我想要的值应该从其他列中计算出来 input ID X1 X2 X3 A 0 96 NA 0 97 B 1 00 NA 1 01 C 0 98 0 03 NA A 1 00 NA 1
  • 实三次多项式的最快数值解?

    R 问题 寻找最快的方法来数值求解一堆已知具有实系数和三个实根的任意三次方程 据报道 R 中的 polyroot 函数对复杂多项式使用 Jenkins Traub 算法 419 但对于实多项式 作者参考了他们早期的工作 对于实三次或更一般的
  • 如何绘制具有显着性水平的箱线图?

    前段时间问了一个关于绘制箱线图的问题Link1 https stackoverflow com questions 14604439 plot multiple boxplot in one graph 我有一些包含 3 个不同组 或标签
  • 麦当劳 omega:R 中的警告

    我正在计算几种不同尺度的欧米茄 并在 R 中使用不同的 omega 函数获取不同比例的不同警告消息 我的问题是如何解释这些警告以及报告检索到的 omega 统计数据是否安全 当我使用 从 alpha 到 omega 内部一致性估计普遍问题的
  • 如何将plot中的单变量列表图表转换为ggplot2格式?

    我正在搜索 但仍然找不到一个非常简单的问题的答案 我们如何使用 R 中的 ggplot2 生成一个变量的简单线图 我正在分析时间序列数据 并且想要对图表进行更复杂的操作 我认为如果我使用 ggplot2 代替会更好plot It works

随机推荐