[英]How to overlay mean of a column with corresponding binwidth values in histogram in ggplot2
[英]How to do histogram with the sum of value of a column in ggplot2?
我有要繪制為直方圖的數據。 這是我的數據
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
值,每個rules
的time
總和
這是我到目前為止所做的
庫(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)
但是結果並不像我想要的那樣總結每個rules
times
。
簡單地說
我想要每個rules
值的直方圖代表其相應time
值的總和
我在這里想念什么?
您需要使用weights
美學。 這將通過倉的值對每個倉的計數加權。
ggplot(w, aes(x=rules, weights=time)) +
geom_bar() + facet_grid(convergence ~ .) +
geom_text(stat="bin", aes(label=..count..), color="red", vjust=-0.1)
為了使文本起作用,我們需要使用stat="bin"
,這與geom_bar()
隱式執行的操作相同。 然后,我們可以使用特殊值..count..
,該值引用ggplot
在計算統計信息后內部產生的數據幀中的列。
根據我對這個問題的理解,這是一種嘗試。
# sample data
DF = read.table(text=" convergence rules fact time
1 1 domain 1802 8629
2 1 domain 1802 8913
3 1 rdfs 595 249
4 1 domain 1 9259
5 1 videcom 1 9071
6 2 domain 314151 9413
7 2 rdfs 8 253", header=T)
# the operation
# you need to tranform the data before plot (below is what I guess on what you what)
library(dplyr); library(ggplot2)
DF_new = DF %>% group_by(convergence, rules) %>% summarise(sum_time = sum(time))
# convergence rules sum_time
# 1 1 domain 26801
# 2 1 rdfs 249
# 3 1 videcom 9071
# 4 2 domain 9413
# 5 2 rdfs 253
ggplot(data=DF_new, aes(x=rules, y=sum_time)) +
geom_bar(stat="identity") +
geom_text(aes(y=sum_time + 1000, label=sum_time), colour="red") +
facet_grid(convergence ~.)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.