简体   繁体   English

使用ggplot2制作箱线图时出错

[英]Error when making a boxplot with ggplot2

I'm newbie at using ggplot2 with R. When I run this script 我是ggplot2与R的新手。运行此脚本时

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

I obtained this error message: 我收到此错误消息:

Don't know how to automatically pick scale for object of type data.frame. 不知道如何为data.frame类型的对象自动选择比例。 Defaulting to continuous. 默认为连续。 Error: Aesthetics must be either length 1 or the same as the data (80): x, y 错误:美学的长度必须为1或与数据(80)相同:x,y

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

Someone can tell me what's wrong? 有人可以告诉我怎么了? I understand it's something with the definition of x and y in the ggplot2 function, but I can't fix it! 我了解这与ggplot2函数中x和y的定义有关,但我无法解决!

You need to change your data.frame into a long format eg with dplyr::gather 您需要将data.frame更改为长格式,例如使用dplyr::gather

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

在此处输入图片说明

You need to reshape your data into long format instead of wide. 您需要将数据重整为长格式而不是宽格式。 I use the melt function from the reshape2 package, but you can also use gather from the tidyr package. 我使用reshape2包中的melt函数,但是您也可以使用tidyr包中的collect。

Try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM