grouped boxplot r ggplot2

拈花ヽ惹草 提交于 2019-12-04 14:09:00

问题


I have 5 columns of numerical data (Equipment, Hyiene.items etc) and 1 column of categorical data (A or D). I'd like to make a grouped boxplot of the numerical data grouped by category but I cannot find a way:

 head(sc)
  Equipment Hygiene.items Patient Near.bed Far.bed Care
1         0             0       1        5       1    D
2         1             4       1        2       0    D
3         3             1       1        2       0    D
4         0             2       2        3       1    A
5         1             2       1        5       2    A
6         1             2       1        1       1    A

boxplot(sc~sc$Care) would seem like the most appropriate way right? I like ggplot2 but it doesn't look like i can simply do this:

ggplot(sc, aes(y=sc)) + 
  geom_boxplot(aes(fill=Care))

EDIT: What I like the look of:

I think what I'm after is something like this one I made in Matlab (a long time ago):

Or the 4th graph on here: Plotly

What I have so far:

library(ggplot2)
library(RColorBrewer)

ggplot(melt_A,aes(x=Care,y=value,fill=Care))+geom_boxplot(ylim=c(1,6,1))+facet_grid(~variable)+
labs(x = "Care", y = "Surface contacts",color="Care" )+
  scale_y_continuous(limits = c(-0, 6))+
  scale_fill_brewer(palette="Purples")+
  theme_bw()+
  theme(strip.background=element_rect(fill="black"))+
  theme(strip.text=element_text(color="white", face="bold"))

Question

How can I change the Care labels from D, H, Me, to something else? e.g. Direct Care, Housekeeping, Medication round, etc...

Fixed:

Found answer here : Stack

I added the following to my ggplot command

scale_fill_brewer(palette="Purples",
  labels = c("Direct care", "Housekeeping","Medication    round","Mealtimes","Miscellaneous care","Personal care"))


回答1:


Your data.frame is not correctly formatted. I named your data "A". You need

library(reshape2)
melt_A<-melt(A)

Now you have the "Care" variable working as ID and the variables with values in a data.frame suitable for ggplot2

melt_A
   Care      variable value
1     D     Equipment     0
2     D     Equipment     1
3     D     Equipment     3
4     A     Equipment     0
5     A     Equipment     1
6     A     Equipment     1
7     D Hygiene.items     0
8     D Hygiene.items     4
9     D Hygiene.items     1
10    A Hygiene.items     2
11    A Hygiene.items     2
12    A Hygiene.items     2
13    D       Patient     1
14    D       Patient     1
15    D       Patient     1
16    A       Patient     2
17    A       Patient     1
18    A       Patient     1
19    D      Near.bed     5
20    D      Near.bed     2
21    D      Near.bed     2
22    A      Near.bed     3
23    A      Near.bed     5
24    A      Near.bed     1
25    D       Far.bed     1
26    D       Far.bed     0
27    D       Far.bed     0
28    A       Far.bed     1
29    A       Far.bed     2
30    A       Far.bed     1

This is one possible plot you might want to do with your data

ggplot(melt_A,aes(x=Care,y=value,fill=Care))+
geom_boxplot()+
facet_wrap(~variable)



来源:https://stackoverflow.com/questions/36718485/grouped-boxplot-r-ggplot2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!