[英]Trouble with plotting using ggplot2
我正在使用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
我现在想做的是绘制与第一个相同的数据,但以size的百分比填充:
p <- ggplot(owner, aes(Ownership, Score, fill=Size))
p+geom_bar(stat="identity")
该图显然是错误的,因为我期望的是原始值的细分,例如。 对于HMO,按大小百分比计为78.84817。 有人可以帮我吗
尝试:
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.