ggplot:使用 2 个以上因素设置颜色

Mri*_*ayi 1 r fill ggplot2 geom-bar

我有一个包含 2 个因素的数据集,每个因素都有 2 个级别。我想制作一个有 4 种不同颜色的分组条形图。

数据如下:

TrialType Group yvar
TP         C     1
TP         E     2
TA         C     3
TA         E     4
Run Code Online (Sandbox Code Playgroud)

我用它来制作条形图

ggplot(data=k, aes(x=V2, y=V3, fill=V1)) + 
  geom_bar(stat="identity", position="dodge", color="#000000") + 
  scale_fill_manual(values=c("#d7191c", "#FFBB45"), breaks=c("TA", "TP"))
Run Code Online (Sandbox Code Playgroud)

我明白图。

有没有办法制作这个图表,使条形图从左到右变成深红色/浅红色/深黄色/浅黄色?那么组是根据红/黄来划分的,试验类型是根据黑暗来划分的吗?

多谢!
姆林马伊

Mak*_*212 5

基本上像这样,使用interaction为每个组合获取独特的颜色:

ggplot(data=k, aes(x=Group, y=yvar, fill=interaction(TrialType,Group))) + 
  geom_bar(stat="identity", position="dodge", color="#000000")+
  scale_fill_manual(values = c("dark red", "pink", "yellow", "light yellow"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述