2014-11-09 54 views
0

我正在使用dplyr和ggplot2使我对医院的数据有所了解。我使用下面的代码来获得医院的所有权和他们从我整理的数据率性能(称为“最后一个数据帧):使用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

您正在寻找这样的事情? 'ggplot(所有者,aes(所有权,分数,填充=大小))+ geom_bar(stat =“identity”,position =“dodge”)'? – jazzurro 2014-11-09 05:52:24

+0

@jazzurro不,不是真的。这为每个所有权类别生成3个不同的栏。我所寻找的是这3个百分比的平均值作为单个酒吧,但满足个人的百分比。 – Dhiraj 2014-11-09 06:11:27

+1

请使用dput发布原始数据。 – 2014-11-09 07:03:37

回答

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") 

enter image description here

+0

这几乎就在那里,但不完全。虽然每个酒吧的大小已经细分为百分比组成部分,但每个酒吧的长度都是100.而我要找的是原始长度,如HMO 78.84817,政府84.33656,市政81.40438和半私人85.01617 。这些需要填写各自的百分比成分。我希望我能够解释。基本上,通过查看一个情节,我需要确定哪家医院有最高分,然后根据这个分数来分解这个分数。 – Dhiraj 2014-11-09 11:37:46

+0

我上面编辑了我的答案。我认为这是你想要的。 – rnso 2014-11-09 12:21:20

+0

绝对!非常感谢。欣赏。 – Dhiraj 2014-11-09 12:22:40