我正在寻求标准化值,同时保留它们的相对频率。例如,一个变量的总计数为 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 …Run Code Online (Sandbox Code Playgroud) 试图获取总数 > 0 的列的名称。我已经尝试过
df[sapply(df, function(x) any(x > 0))]
Run Code Online (Sandbox Code Playgroud)
但这给了我实际的数值。然后我尝试了
names(df)[colSums(df)>0]
Run Code Online (Sandbox Code Playgroud)
但收到一个 NULL。这是我的 df 的示例,其中我期望输出“SRR960396”和“SRR960403”。
> dput(df)
structure(c(14, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0), dim = 4:3, dimnames = list(
c("27b1da20c7a5364614a540521d5e38c0", "bd60888ac07bff9651845c6f6aca4fa8",
"e7474b1fd688aa54814538b69a56d202", "85907e71eb899b7edce9bf8d23746413"
), c("SRR960396", "SRR960402", "SRR960403")))
Run Code Online (Sandbox Code Playgroud)