首页 > 解决方案 > 如何制作分数条形图?

问题描述

当我这样做时:

ggplot(d, aes(x=variable, fill=value))+geom_bar(position = "fill")

我明白了

在此处输入图像描述

但我想要这样的东西,基本上这些值应该按分数着色。我怎样才能做到这一点?这个想法是基本上看到models, percentiles, and models:percentiles每个的相对贡献R2, R5, and R10

示例图

数据框

structure(list(term = c("models", "percentiles", "models:percentiles", 
"models", "percentiles", "models:percentiles", "models", "percentiles", 
"models:percentiles"), variable = structure(c(1L, 1L, 1L, 2L, 
2L, 2L, 3L, 3L, 3L), .Label = c("R2", "R5", "R10"), class = "factor"), 
    value = c(0.435697205847009, 0.533615307749147, 0.0306874864038442, 
    0.441369621882273, 0.520198994695284, 0.0384313834224421, 
    0.394491546635206, 0.579421546902868, 0.0260869064619254)), row.names = c(NA, 
-9L), class = "data.frame")

标签: rggplot2

解决方案


geom_col在这里完成工作:

ggplot(d, aes(x = variable, y = value, fill = term)) + geom_col()

或者

ggplot(d, aes(x = variable, y = value, fill = term)) + geom_bar(stat = "identity")

在此处输入图像描述


推荐阅读