Home > Mobile >  How to combine grouped bar chart and make them like Marimekko chart?
How to combine grouped bar chart and make them like Marimekko chart?

Time:05-13

How can I remove space between bars to make the graph looks like Marimekko chart? In addition I want to convert y-axis from index to percentages and add % values each category with graph

# create a dataset
specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
value <- abs(rnorm(12 , 0 , 15))
data <- data.frame(specie,condition,value)
 
# Stacked   percent
ggplot(data, aes(fill=condition, y=value, x=specie))   
    geom_bar(position="fill", stat="identity")

Output: enter image description here

Expected output (like this): enter image description here

CodePudding user response:

You could do:

library(tidyverse)

data %>% 
  group_by(specie) %>%
  mutate(value = value / sum(value)) %>%
  ggplot(aes(fill=condition, y=value, x=specie))   
  geom_col(position="fill", width = 1, color = "white")  
  geom_text(aes(label = scales::percent(value, accuracy = 0.1)), 
            position = position_fill(vjust = 0.50),
            color = "white")  
  scale_y_continuous(labels = scales::percent)  
  scale_fill_brewer(palette = "Set1")

enter image description here

CodePudding user response:

  data <- data.frame(specie,condition,value) %>%
  group_by(specie) %>%
  mutate(freq = round(value/ sum(value) * 100,1))


ggplot(data, aes(fill=condition, y=freq, x=specie))   
  geom_bar(position="fill", stat="identity", width = 1, color = "black") 
  geom_text(aes(label = freq), position = position_fill(vjust = 0.5)) 
  scale_y_continuous(labels = scales::percent)

[1]: https://i.stack.imgur.com/XBoj1.png

  • Related