有序和排序的ggplot条形图

时间:2015-01-29 23:39:56

标签: r ggplot2

我正在尝试创建“治疗”之间互动的条形图。和' Day'从我的数据集。我想要'治疗'按以下顺序排序:'之前','期间'期间'之后'而且我也想要'Day' 1&2'之前','期间'期间'之后'彼此相邻,而不是每个日子都在一起。组合在一起。这是我的数据集:

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)))

2 个答案:

答案 0 :(得分:1)

一种选择是只需向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)))

enter image description here

答案 1 :(得分:1)

另一种方法是使用小面包裹在单独的面板中绘制每种治疗方案。

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

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