如何洗掉條形之間的空間以使圖表看起來像 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")
輸出:

預期輸出(像這樣):

uj5u.com熱心網友回復:
你可以這樣做:
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")

uj5u.com熱心網友回復:
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)

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/474404.html
標籤:r ggplot2 数据可视化 可视化 ggplotly
上一篇:如何在R中建立斜率圖?
