r - 如何将条形图和参数估计图组合为一个图形。ggplot2
问题描述
大家,我遇到了一个问题,直到现在我都没有解决它。我想制作如下图,但我只能画出正确的图片,但挑战是如何将左右组合成一张图片。我希望有人能帮助我。我真的很感激你。谢谢。
左面板是重要性变量,右面板是参数估计图。顺序根据重要性变量存在。
这就是如何绘制正确的图片。
library(ggplot2)
df %>% ggplot(aes(x = Variables, y = Estimate, color = importance)) +
geom_hline( yintercept = 0, color = 'red', linetype = 'dashed', lwd = 0.5) +
geom_errorbar(aes(ymin = Estimate - lowerCI, ymax = Estimate + upperCI), width = 0, lwd = 1.5) +
coord_flip() +
geom_point(size = 4) +
scale_color_gradient(low="blue", high="red") +
scale_x_discrete(limits=c("AREA_MN_LAND","Semi_habitats_PLAND","SHDI_CROP"))
这是我的原始数据。
df <-
structure(list(Variables = c("AREA_MN_LAND", "Semi_habitats_PLAND",
"SHDI_CROP"), Estimate = c(-0.463930572435947, 0.0937050717425011,
0.925024309144037), Std.Error = c(0.164850815862808, 0.0345256062907729,
0.556521436298426), lowerCI = c(-0.787032234349095, 0.0260361268681767,
-0.16573766262538), upperCI = c(-0.1408289105228, 0.161374016616825,
2.01578628091345), importance = structure(c(1, 1, 0.589768028982561
), n.models = c(AREA_MN_LAND = 2, Semi_habitats_PLAND = 2, SHDI_CROP = 1
), class = c("sw", "numeric"))), row.names = c(NA, -3L), class = "data.frame")
解决方案
实现此目的的一种方法是使用cowplot将两个图“缝合”在一起,例如
library(tidyverse)
library(cowplot)
df <- structure(list(Variables = c("AREA_MN_LAND", "Semi_habitats_PLAND",
"SHDI_CROP"), Estimate = c(-0.463930572435947, 0.0937050717425011,
0.925024309144037), Std.Error = c(0.164850815862808, 0.0345256062907729,
0.556521436298426), lowerCI = c(-0.787032234349095, 0.0260361268681767,
-0.16573766262538), upperCI = c(-0.1408289105228, 0.161374016616825,
2.01578628091345), importance = structure(c(1, 1, 0.589768028982561
), n.models = c(AREA_MN_LAND = 2, Semi_habitats_PLAND = 2, SHDI_CROP = 1
), class = c("sw", "numeric"))), row.names = c(NA, -3L), class = "data.frame")
p1 <- df %>%
ggplot(aes(x = Variables, y = Estimate, color = importance)) +
geom_hline( yintercept = 0, color = 'red', linetype = 'dashed', lwd = 0.5) +
geom_errorbar(aes(ymin = Estimate - lowerCI, ymax = Estimate + upperCI),
width = 0, lwd = 1.5) +
geom_text(aes(label = Variables), nudge_y = 0.5, nudge_x = 0.1) +
coord_flip() +
geom_point(size = 4) +
scale_color_gradient(low="blue", high="red") +
scale_x_discrete(limits=c("AREA_MN_LAND","Semi_habitats_PLAND","SHDI_CROP"),
name = "") +
theme_minimal(base_size = 16) +
theme(axis.text.y = element_blank())
p2 <- df %>%
ggplot(aes(x = "1", y = importance, fill = Estimate)) +
geom_bar(position = "fill", stat = "identity", color = "black") +
geom_text(aes(y = c(.2, .6, .9), label = Variables),
color = "white", angle = 90) +
scale_y_continuous(labels = scales::percent, name = "Relative Importance") +
theme_minimal(base_size = 20) +
theme(legend.position = "none", axis.title.x = element_blank(),
axis.text.x = element_blank())
cowplot::plot_grid(p2, p1, nrow = 1, rel_widths = c(0.4, 1))
推荐阅读
- visual-studio-2015 - 如何在解决方案中将 GitLab 与多个项目文件夹一起使用?
- regex - 正则表达式仅删除特定嵌套级别的括号中的文本?
- java - 我是 Java 初学者,我有一条带有 .gradle 和 Soursets 的错误消息。我怎么解决这个问题?
- c# - 将 [FromQuery] 添加到 Api 方法解决了招摇问题
- mysql - 当插入超出范围的十进制值时,MySQL 插入一个 99999 值而不是抛出一个超出范围的错误
- ios - Swift Combine:没有“distinct”运算符?
- r - FAMD 可视化的重复行名错误
- c# - 如何在嵌套类中的方法上应用 DI
- r - 如何从 R 中的 for 循环返回向量?
- javascript - 无法通过javascript创建元素的onmouseover属性