我有一個帶有抖動資料點的箱形圖,如下所示。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
組。我的資料表中有一個列Ethnicity
。我想讓資料點對應于caucasian
三角形并african-american
在圖中圈出。
資料表是這樣的
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)
現在看起來像這樣
我將不勝感激任何幫助。
uj5u.com熱心網友回復:
將種族添加到 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/436072.html
上一篇:Rggplot2如何分離圖例元素