我有5列数值数据(设备,Hyiene.items等)和1列类别数据(A或D)。我想对按类别分组的数值数据进行分组箱线图绘制,但找不到一种方法:
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)
似乎是最合适的方法了吗?我喜欢ggplot2,但看起来好像我不能简单地做到这一点:
ggplot(sc, aes(y=sc)) +
geom_boxplot(aes(fill=Care))
编辑:我喜欢的外观:
我想我想要的是类似我很久以前在Matlab上制作的这样的东西:
或此处的第4个图形:密谋
到目前为止,我有:
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"))
问题
如何将“护理”标签从D,H,Me更改为其他?例如直接护理,客房清洁,药物治疗等。
固定的:
在这里找到答案:堆栈
我在ggplot命令中添加了以下内容
scale_fill_brewer(palette="Purples",
labels = c("Direct care", "Housekeeping","Medication round","Mealtimes","Miscellaneous care","Personal care"))
您的data.frame格式不正确。我将您的数据命名为“ A”。你需要
library(reshape2)
melt_A<-melt(A)
现在,您可以将“ Care”变量用作ID,并在适合于ggplot2的data.frame中使用具有值的变量
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
这是您可能想对数据做的一种可能的绘图
ggplot(melt_A,aes(x=Care,y=value,fill=Care))+
geom_boxplot()+
facet_wrap(~variable)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句