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