如何使用ggplot绘制条形图,该条形图的x轴使用多列?

时间:2019-04-07 19:56:53

标签: r ggplot2 aesthetics

我正在尝试使用多个列名作为小节中的x轴。因此,每个列名称都将成为“因子”,并且其中包含的数据就是该计数。

我尝试过这样的迭代:

 ggplot(aes( x = names, y = count)) + geom_bar()

我尝试将要显示的x值与aes(c(col1, col2))串联 但美学长度不匹配,也行不通。

library(dplyr)
library(ggplot2)
head(dat)

  Sample Week Response_1 Response_2 Response_3 Response_4 Vaccine_Type
1      1    1        300          0       2000        100            1
2      2    1        305          0        320         15            1
3      3    1        310          0        400         35            1
4      4    1        400          1        410         35            1
5      5    1        405          0        180         35            2
6      6    1        410          2        800         75            2


 dat %>%
  group_by(Week) %>%
  ggplot(aes(c(Response_1, Response_2, Response_3, Response_4)) +
  geom_boxplot() +
  facet_grid(.~Week)

dat %>%
  group_by(Week) %>%
  ggplot(aes(Response_1, Response_2, Response_3, Response_4)) +
  geom_boxplot() +
  facet_grid(.~Week)

> Error: Aesthetics must be either length 1 or the same as the data
> (24): x

这两种方法都失败了(基于aes长度错误代码,这是期望值),但是希望您知道我的目标方向并且可以提供帮助。

目标是有4个独立的组,每个组都有自己的箱形图(每个响应1个)。并在一周内安排他们。

1 个答案:

答案 0 :(得分:0)

使用下面的简单代码主要是我想要的。不幸的是,我不认为像ggplot一样容易将点和其他特征包括到图中。

boxplot(dat[,3:6], use.cols = TRUE)

我可以很容易地按不同的星期进行过滤,并使用mfrow进行构面。不如ggplot提供更多信息,但是可以完成工作。如果还有其他解决方法,我将很感兴趣。

enter image description here

相关问题