簡體   English   中英

R 中帶有兩個不同點符號的 geom_jitter 箱線圖

[英]geom_jitter box-plot with two different point symbols in R

我正在嘗試用point數據 plot 一個box plot 來顯示兩種不同fruits的數量,因此每個水果都有兩個箱線圖。 我想根據它們的status更改點的符號。

我怎樣才能根據status有兩個不同的點形狀?

樣本數據

Fruit Condition Status
Apple Ripe      Yes
Mango Unripe    Maybe
Apple Ripe      Yes
Apple Unripe    Maybe
Mango Ripe      Maybe
Mango Unripe    Yes
Mango Ripe      Yes
Apple Ripe      Yes
Apple Unripe    Yes
Apple Unripe    Maybe
Mango Ripe      Yes
Mango Ripe      Yes
Apple Ripe      Yes

代碼

library(tidyverse)

# 1st approach
ggplot(df = Food, aes(x= Fruit,
                      y = Condition,
                      color = Fruit)) +
  geom_boxplot()  +
  geom_jitter(shape=16, position=position_jitter(0.2)) # stuck...

當前結果

在此處輸入圖像描述

所需 plot

在此處輸入圖像描述

不幸的是,我不相信你可以 plot 在你的 y 軸上有一個二元因子的箱線圖,例如

library(tidyverse)

Food <- read.table(text = "Fruit Condition Status
Apple Ripe      Yes
Mango Unripe    Maybe
Apple Ripe      Yes
Apple Unripe    Maybe
Mango Ripe      Maybe
Mango Unripe    Yes
Mango Ripe      Yes
Apple Ripe      Yes
Apple Unripe    Yes
Apple Unripe    Maybe
Mango Ripe      Yes
Mango Ripe      Yes
Apple Ripe      Yes",
header = TRUE)

# 1st approach
ggplot(Food, aes(x= Fruit,
                 y = Condition,
                 color = Fruit)) +
  geom_boxplot()  +
  geom_jitter(aes(shape=Status),
              position=position_jitter(width = 0.2,
                                       height = 0.2))

reprex package (v2.0.1) 創建於 2021-10-01

如果您有條件的數字“排名”,例如從 1(未成熟)到 10(成熟),您的代碼有效:

library(tidyverse)

Food2 <- read.table(text = "Fruit Condition Status
Apple 9      Yes
Mango 3    Maybe
Apple 7      Yes
Apple 2    Maybe
Mango 9      Maybe
Mango 1    Yes
Mango 10      Yes
Apple 8      Yes
Apple 4    Yes
Apple 2    Maybe
Mango 8      Yes
Mango 9      Yes
Apple 7      Yes",
header = TRUE)

ggplot(Food2, aes(x= Fruit,
                 y = Condition,
                 color = Fruit)) +
  geom_boxplot()  +
  geom_jitter(aes(shape=Status),
              position=position_jitter(width = 0.2,
                                       height = 0.2))

reprex package (v2.0.1) 創建於 2021-10-01

暫無
暫無

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

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