有序和排序的ggplot条形图

Ordered and Sorted ggplot Bar Graph

我正在尝试根据我的数据集创建 'Treatment' 和 'Day' 之间交互的条形图。我希望 'Treatment' 按以下顺序排序:'Before'、'During' 和 'After' 我还希望 'Day' 1 和 2 'Before'、'During' 和 'After' 彼此相邻,而不是将每个 'Day' 组合在一起。这是我的数据集:

structure(list(Name = structure(c(9L, 9L, 5L, 5L, 14L, 14L, 15L, 
15L, 1L, 1L, 7L, 7L, 12L, 12L, 2L, 2L, 11L, 11L, 16L, 16L, 13L, 
13L, 6L, 6L, 8L, 8L, 3L, 3L, 4L, 4L, 10L, 10L, 9L, 9L, 5L, 5L, 
14L, 14L, 15L, 15L, 1L, 1L, 7L, 7L, 12L, 12L, 2L, 2L, 11L, 11L, 
16L, 16L, 13L, 13L, 6L, 6L, 8L, 8L, 3L, 3L, 4L, 4L, 10L, 10L, 
9L, 9L, 5L, 5L, 14L, 14L, 15L, 15L, 1L, 1L, 7L, 7L, 12L, 12L, 
2L, 2L, 11L, 11L, 16L, 16L, 13L, 13L, 6L, 6L, 8L, 8L, 3L, 3L, 
4L, 4L, 10L, 10L), .Label = c("DSGW", "DSOR", "DSYB", "GSWP", 
"LSGL", "LSLL", "OSLL", "PSYP", "PSYR", "RSBB", "RSBP", "RSYY", 
"WSGG", "WSPB", "WSRR", "WSRW"), class = "factor"), Day = c(1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), 
Treatment = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("After", 
"Before", "During"), class = "factor"), Behavior = c(3040L, 
3219L, 2378L, 3529L, 3135L, 3702L, 2868L, 3035L, 1614L, 833L, 
1255L, 1224L, 1569L, 1433L, 1567L, 1448L, 1197L, 1619L, 1543L, 
1309L, 1738L, 1378L, 1362L, 1603L, 1109L, 1073L, 570L, 822L, 
815L, 1166L, 0L, 835L, 3060L, 2815L, 2875L, 3394L, 3009L, 
3274L, 2350L, 2455L, 972L, 815L, 983L, 1033L, 1520L, 1477L, 
1461L, 1395L, 1242L, 1368L, 1637L, 1275L, 1646L, 1546L, 1064L, 
971L, 596L, 1461L, 1276L, 1272L, 872L, 1026L, 1249L, 592L, 
2277L, 3022L, 2997L, 3495L, 2591L, 3447L, 2479L, 2701L, 175L, 
452L, 1287L, 1355L, 1252L, 1501L, 1371L, 1147L, 1465L, 1003L, 
1510L, 0L, 1744L, 1026L, 959L, 898L, 1529L, 1092L, 417L, 
289L, 40L, 762L, 1495L, 896L)), .Names = c("Name", "Day", 
"Treatment", "Behavior"), class = "data.frame", row.names = c(NA, 
-96L))

这是我用来生成当前图形的脚本:

   ggplot(perch1, aes(x = interaction(Treatment,Day), y = Behavior, fill =  factor(Day)))

一个选项是向 data.frame 添加一个新列并手动指定级别的顺序,而不是乱用 interaction

library(ggplot2)
##
df$Group <- factor(
  paste0(df$Day," : ",df$Treatment),
  paste0(rep(1:2,3)," : ",
         rep(c("Before","During","After"),each=2)))
##
R> ggplot(
    df, 
    aes(x=Group, 
        y=Behavior, 
        fill=factor(Day)))+
    geom_bar(stat='identity')+
    scale_x_discrete(
      labels=paste0("Day ",levels(df$Group)))

另一种方法是使用小平面包裹在单独的面板中绘制每个处理。

ggplot(df, aes(x = factor(Day), y = Behavior, fill = factor(Day))) + 
       geom_bar(stat = 'identity') + facet_wrap(~Treatment, nrow = 1) 

这不会在 X-axis 上标记您的互动,而是将其放在面板标题中。