我有很多分類變數進入單個圖表,該圖表按特定狀態進行拆分。像這樣,但有更多的組
dat <- as.data.table(cbind(iris, Status = rep(c("High", "Low"), 75)))
dat <- rbind(dat, data.frame(Petal.Width = sample(iris$Petal.Width, 30, replace = T),
Species = "Control",
Status = "Control"), fill = T)
ggplot(dat, aes(x = Species,y = Petal.Width, fill = Status))
geom_boxplot(position = position_dodge(width = 0.9))
scale_fill_manual(values = c("red", "pink",
"red", "pink",
"blue", "slateblue", "grey"))
我正在嘗試獨立于我用來創建閃避箱線圖的填充狀態為箱線圖著色,但您可以在上面的代碼中看到,scale_fill_manual 只會采用 3 種顏色。
我想手動覆寫獨立于分組美學的顏色,同時保持箱線圖在“高”和“低”之間的分割。
假設 setosa 和 versicolor 有一些共同點(紅色和粉紅色),而 virginica 是它自己的類別(藍色和石板藍),而 control 是一個特例(灰色)。
有沒有辦法分別給每個條上色?
uj5u.com熱心網友回復:
我們可以使用引數,然后我們可以用interaction
為fill
每個箱形圖著色scale_fill_manual
。
library(ggplot2)
ggplot(dat, aes(x = Species, y = Petal.Width, fill = interaction(Status,Species)))
geom_boxplot(position = position_dodge(width = 0.9))
scale_fill_manual(values = c("red", "pink",
"red", "pink",
"blue", "slateblue", "grey"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471583.html
上一篇:當scale='free_y' ggplot2 r時,在facet_grid的一致位置添加文本注釋
下一篇:將匯總表添加到分面網格箱線圖