我正在使用dplyr和ggplot2来了解我在医院的数据。我使用以下代码从我整理的数据(一个名为"最终的数据框)中获取医院的所有权及其绩效百分比:
owner <- final%>% group_by(Ownership)%>% summarise(Score=mean(Total))
这会产生
> owner
Source: local data frame [4 x 2]
Ownership Score
1 HMO 78.84817
2 governmental 84.33656
3 municipal 81.40438
4 semi private 85.01617
我可以使用
绘制上述内容p <- ggplot(owner, aes(Ownership, Score))
p+geom_bar(stat="identity")
我无法发布图片,因为我需要至少10个声誉!
我还可以根据医院的规模对医院进行分类:
owner <- final%>%
group_by(Ownership, Size)%>%
summarise(Score=mean(Total))
给了我这个
> owner
Source: local data frame [10 x 3]
Groups: Ownership
Ownership Size Score
1 HMO big 82.50567
2 HMO medium 83.12919
3 HMO small 67.76271
4 governmental big 85.86831
5 governmental medium 83.70145
6 governmental small 84.69767
7 municipal big 81.40438
8 semi private big 94.07850
9 semi private medium 82.54112
10 semi private small 84.33079
我现在要做的是绘制与第一个相同的数据,但填写大小的百分比:
p <- ggplot(owner, aes(Ownership, Score, fill=Size))
p+geom_bar(stat="identity")
这个情节显然是错误的,因为我所期待的是原始值的细分,例如。对于HMO,就尺寸百分比而言为78.84817。有人可以帮我这个。
答案 0 :(得分:2)
尝试:
library(data.table)
setDT(owner)[,meanscore:=mean(Score),by=Ownership][]
owner[,percentscore:=meanscore*Score/sum(Score),by=Ownership][]
ggplot(owner, aes(Ownership, percentscore, fill=Size)) + geom_bar(stat="identity")