使用 ggplot2 制作箱线图时出错

Error when making a boxplot with ggplot2

我是将 ggplot2 与 R 一起使用的新手。当我 运行 这个脚本

var<-schz.[1,]
values<-schz.[,-1]
ggplot(data=schz., aes(var, values)) + geom_boxplot()

我收到此错误消息:

Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous. Error: Aesthetics must be either length 1 or the same as the data (80): x, y

数据集如下: [https://drive.google.com/file/d/0B7tO-O0lx79FZERvcHJUSmxNSTQ/view?usp=sharing]

谁能告诉我怎么了?我知道它与 ggplot2 函数中 x 和 y 的定义有关,但我无法修复它!

您需要将数据重塑为长格式而不是宽格式。我使用 reshape2 包中的 melt 函数,但您也可以使用 tidyr 包中的 gather。

尝试:

 library(reshape2)
 ggplot(data=melt(schz.), aes(variable, value)) + geom_boxplot()

您需要将 data.frame 更改为长格式,例如dplyr::gather

schz. <- schz. %>% gather(type, value, -SITE)
ggplot(schz., aes(x=SITE, y=value, colour=type)) + geom_boxplot()