我想使用调色板比例为连续变量为盒装图中的盒子上色。
示例数据:
Sample <- c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I')
Counts <- c(8, 10, 7, 15, 19, 16, 11, 13, 14)
Group <- c('Group1', 'Group1', 'Group1', 'Group2', 'Group2', 'Group2', 'Group3', 'Group3', 'Group3')
Factor <- c('positive', 'positive', 'negative', 'negative', 'negative', 'negative', 'positive', 'negative', 'negative')
df = data.frame(Sample, Counts, Group, Factor)
df
bxplt <- ggplot(df, aes(x=Group, y=Counts)) +
geom_boxplot()
bxplt
我想在同一图中包括'Factor'变量的表示。由于我实际数据的复杂性,我想通过使用反映“因素”列中正负比率的色标为箱形图的框上色来简化消息。我像这样使用dplyr得出比率:
df %>%
group_by(Factor , Group) %>%
summarise(n = n()) %>%
group_by(Group) %>%
summarise(
pos_Percent = paste0((n[Factor == "positive"] / sum(n)) * 100 , "%") ,
neg_Percent = paste0((n[Factor == "negative"] / sum(n) * 100) , "%")
)
输出:
# A tibble: 3 x 3
Group pos_Percent neg_Percent
<fct> <chr> <chr>
1 Group1 66.6666666666667% 33.3333333333333%
2 Group2 % 100%
3 Group3 33.3333333333333% 66.6666666666667%
我希望根据'pos_Percent'值对箱线图中的框进行着色,即'Group1'为67%,Group2为零,Group3为33%。我想使用这些变量设置连续的调色板,例如来自Rcolorbrewer的“ Blues”,但我不确定如何进行。
更新 对我有用的解决方案(来自iod)-
summary<-df %>%
group_by(Group) %>%
summarise(
pos_Percent = sum(Factor == "positive") / n(),
neg_Percent = sum(Factor == "negative") / n())
newdf<-left_join(df, summary)
ggplot(newdf, aes(x=Group, y=Counts)) +
geom_boxplot(aes(fill=pos_Percent))
答案 0 :(得分:0)
实际上,考虑一下,有一种简单的方法,因为没有理由进行总结(如Camille在上面的评论中所述):
df<- df %>%
group_by(Group) %>%
mutate(pos_Percent=sum(Factor == "positive") / n(),
neg_Percent = sum(Factor == "negative") / n())
ggplot(df, aes(x=Group, y=Counts)) +
geom_boxplot(aes(fill=pos_Percent))