Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can this type of plot be done with ggplot2?

I am using R and ggplot2 to do some plots for publishing purposes. I have come across this plot and I would like to replicate it using ggplot2. However, I have never seen a plot like this made using ggplot2.

Can it be done with ggplot2? What about the text below the bars? I guess these will have to be hard coded in the ggplot2 codes. And how do you align those text?

Bar graph

like image 335
user3115933 Avatar asked Dec 11 '22 05:12

user3115933


1 Answers

This gets fairly close:

# Generate sample data (I'm too lazy to type out the full labels)
df <- data.frame(
    perc = c(60, 36, 44, 41, 42, 57, 34, 52),
    type = rep(c("blue", "green"), 4),
    label = rep(c(
        "Individual reports created as needed",
        "Regular reports on single topics",
        "Analytics using data integrated from multiple systems",
        "Business unit-specific dashboards and visuals"), each = 2))


library(ggplot2)
ggplot(df, aes(1, perc, fill = type)) +
    geom_col(position = "dodge2") +
    scale_fill_manual(values = c("turquoise4", "forestgreen"), guide = FALSE) +
    facet_wrap(~ label, ncol = 1, strip.position = "bottom") +
    geom_text(
        aes(y = 1, label = sprintf("%i%%", perc)),
        colour = "white",
        position = position_dodge(width = .9),
        hjust = 0,
        fontface = "bold") +
    coord_flip(expand = F) +
    theme_minimal() +
    theme(
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.text = element_text(angle = 0, hjust = 0, face = "bold"))

enter image description here

A few explanations:

  1. We use dodged bars and matching dodged labels with position = "dodge2" (note that this requires ggplot_ggplot2_3.0.0, otherwise use position = position_dodge(width = 1.0)) and position = position_dodge(width = 0.9), respectively.
  2. We use facet_wrap and force a one-column layout; strip labels are moved to the bottom.
  3. We rotate the entire plot with coord_flip(expand = F), where expand = F ensures that left aligned (hjust = 0) facet strip texts align with 0.
  4. Finally we tweak the theme to increase the overall aesthetic similarity.
like image 181
Maurits Evers Avatar answered Jan 06 '23 19:01

Maurits Evers