如何组合分组条形图并使它们像 Marimekko 图表?

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

如何删除条形之间的 space 以使图表看起来像 Marimekko 图表?此外,我想将 y 轴从索引转换为百分比,并使用图表

添加每个类别的百分比值
# 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")

输出:

预期输出(像这样):

  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)

你可以这样做:

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