如何交换条形图(来自R中的ggplot)?

时间:2018-02-02 10:53:36

标签: r ggplot2 bar-chart summary

我的摘要数据集包含准确度(频率)和标准偏差( sd ),如下所示:

> sum = summarySE(df, measurevar="Freq", groupvars=c("Block","Reward", "Congruency"))
> sum
   Block Reward Congruency  N     Freq        sd       se       ci
1   CSRA      0          0 25 92.81094  5.696302 1.139260 2.351318
2   CSRA      0          1 25 89.15566  9.163108 1.832622 3.782345
3   CSRA      1          0 25 87.58630 13.034372 2.606874 5.380324
4   CSRA      1          1 25 84.92784 13.431737 2.686347 5.544349
5    MID      0          0 25 91.94928  7.562742 1.512548 3.121747
6    MID      0          1 25 83.93017  9.187526 1.837505 3.792424
7    MID      1          0 25 89.00725  6.790402 1.358080 2.802940  
8    MID      1          1 25 84.19499 12.045129 2.409026 4.971985
9   NEUT      0          0 25 87.97193  8.286820 1.657364 3.420631
10  NEUT      0          1 25 80.87517 10.945678 2.189136 4.518154
11  NEUT      1          0 25 87.97193  8.286820 1.657364 3.420631
12  NEUT      1          1 25 80.87517 10.945678 2.189136 4.518154
13   SRA      0          0 25 93.97101  6.312160 1.262432 2.605532
14   SRA      0          1 25 90.19947  8.873230 1.774646 3.662689
15   SRA      1          0 25 86.89789 10.910052 2.182010 4.503448
16   SRA      1          1 25 81.42151 12.470161 2.494032 5.147429

和它的条形图:

sum$Reward <- revalue(sum$Reward , c("1"="No Reward", "0"="Reward"))
sum$Block <- revalue(sum$Block , c("CSRA"="C-SRA"))
sum$Congruency <- revalue(sum$Congruency , c("1"="Incongruent", "0"="Congruent"))

ggplot(sum, 
   aes(x=Congruency, y=Freq, group=Block, 
       ymax=Freq+se, ymin=Freq-se))  +
 geom_bar(stat="identity", position = "dodge", aes(fill=Block)) +
 facet_wrap(  ~ Reward) +
 geom_errorbar(position=position_dodge(width=0.7), 
            width=0.0, size=0.5, color="black")  +
labs(x = "Block",
   y = "Acuracy [%]")  +
theme_bw()  +
theme(panel.grid.major.x = element_blank(),
    panel.grid.major.y = element_line(colour = "grey50"),
    plot.title = element_text(size = rel(1.5), 
                              face = "bold", vjust = 1.5),
    axis.title = element_text(face = "bold"),
    legend.key.size = unit(0.4, "cm"),
    legend.key = element_rect(fill = "black"),
    axis.title.y = element_text(vjust= 1.8),
    axis.title.x = element_text(vjust= -0.5)) +
coord_cartesian(ylim = c(70, 100))+
scale_fill_manual(name="Condition", # Legend label, use darker colors
                values=c("#E69F00", "#56B4E9", "#F0E442", "#009E73")) +
theme(panel.grid.major = element_blank(), text = element_text(size=15), panel.grid.minor = element_blank(),
    panel.background = element_blank(), axis.line = element_line(colour = "black"))

enter image description here

如你所见,我在一个较大的一个中实施了两个小条形图:奖励和无奖励。我想要做的是用地方改变它们,即交换它们(首先是无奖励,然后是奖励)。因此,我试图改变一个方面(奖励与无奖励)的顺序,而不是改变方面本身的顺序(C-SRA vs SRA vs MID vs Netral)。有没有办法在ggplot脚本中执行此操作,或者我应该在ggplot脚本之外执行此操作?

1 个答案:

答案 0 :(得分:2)

我们可以使用基础R中的factor执行此操作。使用mpg数据集的一个示例:

library(ggplot2)
library(patchwork)

p1 <- ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  facet_wrap(~class, nrow = 4) + 
  labs(title = "Default")

mpg$class <- factor(mpg$class, levels = c("compact",
                                          "2seater",
                                          "minivan",
                                          "midsize",
                                          "subcompact",
                                          "pickup",
                                          "suv"))

p2 <- ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  facet_wrap(~class, nrow = 4) +
  labs(title = "Modified")

p1 + p2

enter image description here