繁体   English   中英

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

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

我有一个盒子 plot,其中包含如下所示的抖动数据点。 x-axis具有三个不同y-axis具有薪水。

在此处输入图像描述

我用下面这段代码给plot。

 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)

我想做的另一件事是根据不同的种族为数据点添加不同的形状。 例如,男性和女性都被分为caucasianafrican-american群体。 我的 data.table 中有一个Ethnicity列。我想在 plot 中将对应于caucasian人到三角形和african-american的数据点做成圆圈。

data.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

 
   

我只是想知道是否可以在盒子 plot 中使用。我只是尝试了以下内容,但它不起作用。

 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM