使用 R 和 ggplot2 在箱形图中抖动点的不同形状

different shapes for jittered points in box plots using R and ggplot2

我有一个包含抖动数据点的箱线图,如下所示。 x-axis 有三个不同的地区,y-axis 有工资。

我用下面这段代码来作图。

ggplot(df1, aes(x = Region, y = Income, fill = Sex, color = Sex), size = 10) + 
  geom_boxplot(outlier.shape = NA)+
  geom_point(position = position_jitterdodge(0.5), alpha=0.3)

我想做的另一件事是根据不同的种族为数据点添加不同的形状。比如男女都分为caucasian组和african-american组。我的数据 table 中有一列 Ethnicity。我想将对应于 caucasian 的数据点设为三角形,将 african-american 对应的数据点设为圆圈。

数据table看起来像这样

   Region Income Sex    Ethnicity       
   <chr>   <dbl> <chr>  <chr>           
 1 Area1    2000 male   African-american
 2 Area1    3000 female African-american
 3 Area1    2000 male   African-american
 4 Area1    4000 male   African-american
 5 Area1   40050 female African-american
 6 Area1   60000 male   African-american
 7 Area1    2000 male   Caucasian       
 8 Area1    4562 female Caucasian       
 9 Area1    4568 male   Caucasian       
10 Area1    6573 male   Caucasian

 
   

我只是想知道在箱形图中是否可行。我刚刚尝试了以下方法,但它不起作用。

 ggplot(df1, aes(x = Region, y = Income, fill = Sex, color = Sex, shape = Ethnicity), size = 10)

现在看起来像这样

如有任何帮助,我将不胜感激。

将种族添加到 geom_point 的 aes() 而不是你拥有它的地方。

df %>%
    ggplot(aes(x = Region, y = Income, fill = Sex, color = Sex), size = 10) +
    geom_boxplot(outlier.shape = NA)+
    geom_point(aes(shape = Ethnicity), position = position_jitterdodge(0.5), alpha=0.3)