5
如何绘制ggplot2中堆积条上方每个类的总和值(在我的情况下:a = 450,b = 150,c = 290,d = 90) ?这里是我的代码:在ggplot2中绘制堆积条上方的总和值
library(dplyr)
totals <- hp %>%
group_by(class) %>%
summarize(total = sum(value))
然后加入geom_text
:
#Data
hp=read.csv(textConnection(
"class,year,amount
a,99,100
a,100,200
a,101,150
b,100,50
b,101,100
c,102,70
c,102,80
c,103,90
c,104,50
d,102,90"))
hp$year=as.factor(hp$year)
#Plotting
p=ggplot(data=hp)
p+geom_bar(binwidth=0.5,stat="identity")+
aes(x=reorder(class,-value,sum),y=value,label=value,fill=year)+
theme()
你有一个字段'数量“,而美学中的”价值“;不应该一样吗? –
确实。我尝试编辑以修正该示例,但编辑被拒绝... 'aes'调用应该是:'aes(x = reorder(class,-amount,sum),y = amount,label = amount,fill =年)+' –