将数据标准化为 100%,但总值小于 1.00

Geo*_*cro 2 r ggplot2

我正在寻求标准化值,同时保留它们的相对频率。例如,一个变量的总计数为 219,由值 56、89、145 组成。为了对这些数据进行标准化,我将每个值除以总数,然后将结果可视化为条形图,如下所示。为什么总值总和不是 1.00?

p.perc <- ggplot(bNTI.perc, aes(fill=variable,x=pond,y=value/total)) +
  geom_bar(stat = "identity")
print (p.perc)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

谢谢你!我的数据:

> dput(bNTI.perc)
structure(list(pond = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 
4L, 1L, 2L, 3L, 4L), .Label = c("RHM", "TS", "SS", "Lilly"), class = "factor"), 
    total = c(291, 740, 241, 42, 291, 740, 241, 42, 291, 740, 
    241, 42), variable = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 3L, 3L, 3L, 3L), .Label = c("sum(cor > 2)", "sum(cor < -2)", 
    "sum(cor > 2 | cor < -2)"), class = "factor"), value = c(56L, 
    213L, 49L, 0L, 89L, 156L, 70L, 19L, 145L, 369L, 119L, 19L
    )), row.names = c(NA, -12L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)

All*_*ron 5

您不需要手动执行此操作。使用position = 'fill'

ggplot(bNTI.perc, aes(pond, value, fill = variable)) + geom_col(position = 'fill')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但你实际问题的答案是你的total专栏是错误的。有三个值Lilly(0、19 和 19),总和为 38,但您的totalLilly是 42,而不是 38,因此您的Lilly条形加起来仅为 38/42 (0.9047619)。同样,你的SS119 + 70 + 49加起来是 238,但你的totalforSS是 241