【问题标题】:geom_jitter box-plot with two different point symbols in R在R中具有两个不同点符号的geom_jitter箱线图
【发布时间】:2021-10-01 09:09:29
【问题描述】:

我正在尝试使用point 数据绘制box 图以显示两个不同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...

当前结果

想要的情节

【问题讨论】:

  • Condition 是二进制的。仅使用分类变量绘制箱线图是否正确?
  • @Park,思路是在conditionstatus的基础上看到两个不同fruitscluster
  • ggplot,在color = Fruit之后,写shape = Fruit

标签: r ggplot2


【解决方案1】:

不幸的是,我不相信您可以在 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 月 1 日创建

如果您有条件的数字“排名”,例如从 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 创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 2019-02-23
    相关资源
    最近更新 更多