0

I am using dplyr and ggplot2 to make some sense out of data that I have on hospitals. 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!

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 :

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. Can someone help me with this please.

3
  • Are you looking for something like this? ggplot(owner, aes(Ownership, Score, fill=Size)) + geom_bar(stat="identity", position = "dodge")? Commented Nov 9, 2014 at 5:52
  • @jazzurro No, not really. This produces 3 different bars for each category of ownership. What I am looking for is the average of these 3 percentages as single bar but filled with individual size percentages. Commented Nov 9, 2014 at 6:11
  • 1
    Please post the original data using dput. Commented Nov 9, 2014 at 7:03

1 Answer 1

2

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

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

this is almost there but not quite. While each bar has been broken down to its percent constituents in terms of size, the length of each is 100. Whereas, what I'm looking for is the original length, as in HMO 78.84817, governmental 84.33656, municipal 81.40438 and semi private 85.01617. These need to be filled with their respective percent constituents. I hope I have been able to explain. Basically, by looking at one plot, I need to determine which hospital has the maximum score and then the break up of that score in terms of the size.
I have edited my answer above. I think this is what you want.
Absolutely! Thanks so much. Appreciate.
Instead of using library(data.table) I have made the following change to the code owner <- owner%>%mutate(Percent=mean(Score)*Score/sum(Score))
Good. With datatable you can do this with: owner[,percentscore:=mean(Score)*Score/sum(Score),by=Ownership][]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.