gpt4 book ai didi

r - 如何用ggplot2中一列的总和做直方图?

转载 作者:行者123 更新时间:2023-12-01 04:35:51 25 4
gpt4 key购买 nike

我有要绘制为直方图的数据。这是我的数据

convergence,rules,fact,time
1,domain,1802,8629
1,domain,1802,8913
1,rdfs,595,249
1,domain,1,9259
1,videcom,1,9071
2,domain,314151,9413
2,rdfs,8,253
....

我想要的是为每个 convergence 值绘制每个 rulestime 总和

这是我目前所做的

图书馆(ggplot2)

w <- read.csv(file="s2.csv", head=TRUE, sep=",")

p <- ggplot(data=w, aes(group=convergence, x=factor(rules))) + geom_bar(aes(colour="red")) + + geom_text(aes(y=time + 1000, colour="red", label=time)) + facet_grid( convergence ~ . )

ggsave(file="s2.1m.png", width=15)

但结果并没有像我想要的那样对每个 rulestimes 求和。

简单的说

我想要每个 rules 值的直方图,表示其对应的 time 值的总和

enter image description here

我在这里错过了什么?

最佳答案

您需要使用权重 美学。这会根据 bin 的值对每个 bin 的计数进行加权。

ggplot(w, aes(x=rules, weights=time)) + 
geom_bar() + facet_grid(convergence ~ .) +
geom_text(stat="bin", aes(label=..count..), color="red", vjust=-0.1)

enter image description here

为了使文本正常工作,我们需要使用 stat="bin",这与 geom_bar() 隐式执行的操作相同。然后我们可以使用特殊值 ..count.. 引用数据框中的列 ggplot 在计算统计数据后内部生成。

关于r - 如何用ggplot2中一列的总和做直方图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27545024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com