簡體   English   中英

分組箱線圖 r ggplot2

[英]grouped boxplot r ggplot2

我有 5 列數值數據(Equipment、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 中制作的這個東西(很久以前):

在此處輸入圖片說明

或者這里的第四張圖: Plotly

在此處輸入圖片說明

到目前為止我所擁有的:

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)

現在,您擁有用作 ID 的“Care”變量和具有適用於 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)

在此處輸入圖片說明

您需要將所有列收集到一個單獨的列中,然后將它們映射到 x,並將它們的計數映射到 y。 然后,您只需要將顏色映射到此列中的每個因素,並為每種類型的護理手動設置 alpha。

---
title: "Boxplots"
output: html_document
---

```{r setup, include=FALSE}
library(tidyverse)
library(ggplot2)


```

```{r base-data}
a <- tibble(Equipment     = sample(1:10, 50, replace = T),
            Hygiene.items = sample(1:10, 50, replace = T),
            Patient       = sample(1:10, 50, replace = T),
            Near.bed      = sample(1:10, 50, replace = T),
            Far.bed       = sample(1:10, 50, replace = T),
            Care          = sample(c("A", "D"), 50, replace = T)) %>% 
  gather(key = "Context", value = "Count", -Care)



```


```{r boxplot, echo=FALSE}

ggplot(data = a) +

  geom_boxplot(aes(x = Context,
                   y     = Count,
                   fill  = Context,
                   alpha = Care)) +

  scale_alpha_manual(values = c(0.7, 1))

```

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM