将空图添加到构面,并与另一个构面组合

2024-01-10

Using this SO solution https://stackoverflow.com/questions/30372368/adding-empty-graphs-to-facet-wrap-in-ggplot2 I created a facet with two "empty" plots, with the aim of combining with another group of facet_wrap plots, as shown below. The purpose is to have two y-axis labels for different unit measurements. How can I make the grid layout look like the top image, which produces the arrangement I want, but not the axis labels? This was accomplished with plot_grid with individual plots. My current output does not scale correctly and overlaps the other plots, as seen in the second image, but provides the axis labels. I have example data below, just copy and run the code to input it. enter image description here

library(ggplot2)
library(grid)
library(cowplot)

clipboard <- readClipboard()
test.data <- read.table(file = "clipboard", sep = ",", header=TRUE)
test.data1 <- test.data[1:24, ]
test.data2 <- test.data[25:32, ]

testplot1 <- ggplot(test.data1, aes(Station, value)) +
  geom_point() +
  labs(x = "Stations", y = "Scale A") +
  theme(legend.position = "none", legend.title = element_blank()) +
  facet_wrap( ~ constituent, ncol = 3, scales = "free_y")

testplot2 <- ggplot(test.data2, aes(Station, value)) +
  geom_point() +
  labs(x = "Stations", y = "Scale B") +
  theme(legend.position = "none", legend.title = element_blank(), axis.title.y = element_text(hjust = 0.2)) +
  facet_wrap( ~ constituent, ncol = 1, scales = "free_y")

blankplots <- ggplotGrob(testplot2)
rm_grobs <- blankplots$layout$name %in% c("panel-1-1", "panel-2-1", "strip-t-1-1", "strip-t-1-2")
blankplots$grobs[rm_grobs] <- NULL
blankplots$layout <- blankplots$layout[!rm_grobs, ]
grid.newpage()
emptygrids <- grid.draw(blankplots)

plot_grid(emptygrids, MPLOOplot1)

日期示例如下:

Station,constituent,value
A1,A,1
B1,A,1
A1,B,2
B1,B,2
A1,C,3
B1,C,3
A1,D,4
B1,D,4
A1,E,5
B1,E,5
A1,F,6
B1,F,6
A1,G,7
B1,G,7
A1,H,8
B1,H,8
A1,I,9
B1,I,9
A1,J,10
B1,J,10
A1,K,11
B1,K,11
A1,L,1.4
B1,L,1.4
A1,Blank1,NA
B1,Blank1,NA
A1,Blank2,NA
B1,Blank2,NA
A1,XX,0.52
B1,XX,0.52
A1,YY,0.355
B1,YY,0.355

我不确定我是否完全理解您想要做什么,所以请告诉我这是否是您的想法。我不确定你想要将颜色映射到什么,所以我只是使用constituent对于这个例子。

library(gridExtra)
library(ggplot2)
library(dplyr)
library(cowplot)
theme_set(theme_classic())

testplot1 <- ggplot(test.data1, aes(Station, value, colour=constituent)) +
  geom_point() +
  labs(x = "Stations", y = "Scale A") +
  theme(legend.title = element_blank()) +
  facet_wrap( ~ constituent, ncol = 3, scales = "free_y") +
  guides(colour=guide_legend(ncol=2))

testplot2 <- ggplot(test.data2 %>% filter(!grepl("Blank", constituent)), 
                    aes(Station, value, colour=constituent)) +
  geom_point() +
  labs(x = "Stations", y = "Scale B") +
  theme(legend.title = element_blank(), 
        axis.title.y = element_text(hjust = 0.2)) +
  facet_wrap( ~ constituent, ncol = 1, scales = "free_y")

leg1 = get_legend(testplot1)
leg2 = get_legend(testplot2)

testplot1 = testplot1 + guides(colour=FALSE)
testplot2 = testplot2 + guides(colour=FALSE)

现在我们列出情节和传说grid.arrange。这需要手动调整高度和宽度。

grid.arrange(
  arrangeGrob(
    arrangeGrob(nullGrob(), leg2, leg1, nullGrob(), ncol=4, widths=c(1,4,4,1)),
    testplot2, ncol=1, heights=c(4.2,5)
    ),
  testplot1, ncol=2, widths=c(1.1,3))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将空图添加到构面,并与另一个构面组合 的相关文章

  • 指定 R 中 hist() 中的 bin 数量?

    我尝试指定垃圾箱的数量hist R为10 如下 gt hist x breaks 10 但垃圾箱的数量并不完全是 10 我尝试了几个其他数量的垃圾箱 结果发生了同样的情况 hist says breaks可以指定 给出直方图单元格数量的单个
  • 如何使用 r 中的 caret 包在最佳调整超参数的 10 倍交叉验证中获得每次折叠的预测?

    我试图使用 R 中的插入符包使用 10 倍交叉验证和 3 次重复来运行 SVM 模型 我想使用最佳调整的超参数获得每次折叠的预测结果 我正在使用以下代码 Load packages library mlbench library caret
  • R从列表中提取数据框,列名中没有前缀

    我在列表中放置了一个数据框 然后 当尝试将其提取回来时 我得到了该数据帧的所有以列表键为前缀的列名称 有没有办法完全按照最初传递的方式提取数据帧 cols lt c column1 Column2 Column3 df1 lt data f
  • 将summary()写入as.data.frame以在ggplot / R中使用

    请查找 af 数据样本t below 我正在使用以下方法进行竞争风险分析etmCIF来自etm package 产生以下结果 这很好 但需要更好的图形 曾经有一个ggtrans etm函数将数据导入ggplot 然而 这个功能显然被删除了
  • R 中的 NA 替换函数

    我正在尝试替换矩阵中的 NA mat 零 我在用着mat is na mat lt 0 当我有 18946 个变量的 94531 个观察值或更小的矩阵时 效果很好 但我在 22752 个变量的 112039 个观察值的矩阵上尝试它 R 显示
  • 用于清除工作空间和转储存储的 R 全局函数

    我希望创建一个全局函数来清除我的工作区并转储我的内存 我将我的函数称为 cleaner 并希望它执行以下代码 remove list ls gc 我尝试在全局环境中创建该函数 但是当我运行它时 控制台仅打印该函数的文本 在我要获取的函数文件
  • dplyr 中的 Summarize 是否可以不删除数据框中的其他列?

    我有一个包含三列的数据框 我正在尝试进行简单的总结以查找数据框中每个城市的最高温度 但同时保留每个最高温度列出的日期 这是数据框 我们称之为 maxT new ID Date Max TemperatureF 1 TUS 1960 04 0
  • 使用底格里斯河从纬度/经度获取人口普查区

    我有相对较多的坐标 我想获取其人口普查区 除了 FIPS 代码 我知道我可以使用以下命令查找各个纬度 经度对call geolocator latlon 已完成here https stackoverflow com questions 5
  • 将值替换为其各自列的名称

    我有一个数据框 Code 401k CVS 101A true 231N true FD54 true 99JB 85F4 true 我试图用相应的列名称 例如 401k 替换 true 字符值 这是我想要的输出 Code 401k CVS
  • 在函数内部调用 clusterApply 时,性能会下降

    我遇到了一个奇怪的问题clusterApply 我已经能够尽可能地隔离它 如下所示 首先 我从全局环境运行以下代码 require parallel cl lt makeCluster rep localhost 20 SOCK xl lt
  • 使用 dplyr:group_by 将数据帧分成多个子集?

    有没有办法根据 group by 组使用 dplyr 将一个数据帧拆分为数据帧的子集 mtcars gt group by cyl gear gt codes 非常感谢 好吧 并不是你真的想要 但你可以这样做tidyr 即nearly一样的
  • R 中的发散积分可在 Wolfram 中求解

    我知道我以前问过同样的问题 但由于我是新来的 这个问题问得不好而且不可重现 因此我在这里尝试做得更好 如果我只编辑旧的 可能没有人会读它 我有一个想要积分的二重积分 ff lt function g t exp 16 g exp 8 t t
  • 错误:“rjags”的包或命名空间加载失败

    在终端的 conda 环境之一中 我能够成功安装包 rjags 但是 当我在该环境中运行 R 并运行库 rjags 时 出现以下错误 加载所需的包 coda 错误 rjags 的包或命名空间加载失败 rjags 的 loadNamespac
  • R 更改小数位且不四舍五入

    gt signif 1 89 digits 2 1 1 9 我想要1 8 这有点笨拙 但它会起作用并保持所有数字 x lt 1 829380 trunc dec lt function x n floor x 10 n 10 n Resul
  • 将数据从 R 导出到 Excel

    我试图将从 R 获得的一些结果导出到 Excel 中 但未成功 我尝试过以下代码 write table ALBERTA1 D ALBERTA1 txt sep t write csv ALBERTA1 ALBERTA1 csv your
  • r 中的 5 维图

    我正在尝试在 R 中绘制 5 维图 我目前正在使用rgl包以 4 个维度绘制数据 使用 3 个变量作为 x y z 坐标 另一个变量作为颜色 我想知道是否可以使用这个包添加第五个变量 例如空间中点的大小或形状 这是我的数据和当前代码的示例
  • 如何在R中同时对三个字段进行网络分析

    如何在 R 中同时对三个字段进行网络分析 下面是示例数据以及desired output在最后一栏中 df lt data frame stringsAsFactors FALSE id 1 c ABC ABC BCD CDE DEF EF
  • 具有 dplyr、tidyverse 和 broom 的相关矩阵 - P 值矩阵

    全部 我想使用以下方法从相关矩阵中获取 p 值dplyr 和 或扫帚包 并同时测试多个变量 我知道其他方法 但 dplyr 对我来说似乎更简单 更直观 此外 dplyr 需要关联每个变量以获得特定的 p 值 这使得该过程更容易 更快 我检查
  • 如何将 ggrough 图表另存为 .png

    说我正在使用R包裹ggrough https xvrdm github io ggrough https xvrdm github io ggrough 我有这个代码 取自该网页 library ggplot2 library ggroug
  • 非闪亮上下文中的反应式对象绑定

    实际问题 你怎样才能近似反应性环境 行为 http shiny rstudio com tutorial lesson6 建立者shiny http shiny rstudio com函数 或者甚至可能在一个函数中使用这些函数无光泽上下文以

随机推荐