[英]Trouble with plotting using ggplot2
I am using dplyr and ggplot2 to make some sense out of data that I have on hospitals. 我正在使用dplyr和ggplot2来了解我在医院中得到的数据。 I'm use the following code to get Ownership of hospitals and their performance in percentage from my tidied data (a data frame called "final):
我使用以下代码从整理的数据(称为“最终”数据框)中获取医院的所有权及其绩效的百分比:
owner <- final%>% group_by(Ownership)%>% summarise(Score=mean(Total))
This produces 这产生
> 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
I can plot the above using 我可以使用绘制
p <- ggplot(owner, aes(Ownership, Score))
p+geom_bar(stat="identity")
I can't post images as I need at least 10 reputations! 我无法发布图片,因为我需要至少10个信誉!
I can also classify hospitals based on their sizes : 我还可以根据规模对医院进行分类:
owner <- final%>%
group_by(Ownership, Size)%>%
summarise(Score=mean(Total))
which gives me this 这给我这个
> 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
What I am now trying to do is plot the same data as the first one, but filled in the percentages of size : 我现在想做的是绘制与第一个相同的数据,但以size的百分比填充:
p <- ggplot(owner, aes(Ownership, Score, fill=Size))
p+geom_bar(stat="identity")
This plot is obviously wrong as I what I am expecting is breakdown of the original value, for eg. 该图显然是错误的,因为我期望的是原始值的细分,例如。 for HMO it is 78.84817, in terms of Size percentages.
对于HMO,按大小百分比计为78.84817。 Can someone help me with this please.
有人可以帮我吗
Try: 尝试:
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.