How to combine barchart and parameter estimate plot as one figure. ggplot2

Viewed 85

everyone, I encountered a problem, until now I don't resolve it. I want to make a plot as below, but I am just able to draw the right picture but the challenge is how to combine left and right to one picture. I wish someone could help me. I really appreciate it for you. Thanks.

left panel is importance variable and the right panel is parameter estimate plot. The order exists according to importance variable.

enter image description here

This is how to draw the right picture.

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"))

Here is my raw data.

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")

1 Answers

One way to achieve this is to use cowplot to 'stitch' the two plots together, e.g.

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))

updated_example.png

Related