首页 > 解决方案 > 如何将条形图和参数估计图组合为一个图形。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")

标签: rggplot2

解决方案


实现此目的的一种方法是使用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))

更新示例.png


推荐阅读