简体   繁体   English

使用ggplot2,qplot或其他方法绘制多个y变量的箱线图

[英]plotting boxplots of multiple y variables using ggplot2, qplot or others

I am searching the way to draw boxplots of many y variables with x=season and function=event. 我正在寻找用x = season和function = event绘制许多y变量的箱形图的方法。 I want to draw many plots all together like: 我想一起绘制很多图:

fist plot: x=season, y=var1 by function=event second plot: x=season, y=var1, by fuction=event .. 第一图:x =季节,y = var1(函数=事件)第二图:x =季节,y = var1,函数= event。

My data looks like as below. 我的数据如下所示。 Actually, I have a plenty of variables. 实际上,我有很多变量。

......................................................................... ................................................... .......................

    event     season      var1      var2     var3
1  event_free wet.summer 14.193489 16.786347 22.65968
2  event_free       fall  3.209114  9.948187 15.44799
3       event       fall  4.564315 10.647883 34.24658
4       event       fall 20.152646 31.114422 33.04899
5  event_free       fall  3.944427  6.431695 10.27153
6  event_free       fall  9.994351 16.110569 22.73702
7  event_free       fall  3.100501  6.507310 14.37157
8  event_free     winter  2.631117        NA 13.88889
9       event     winter 20.745972 22.629357 29.27042
10      event     winter 15.929737 21.355657 36.45409
11 event_free     winter  7.383920  7.418910 11.85094
12      event     winter 17.011810 20.320714 44.18071
13      event     spring 12.501078 14.260404 39.08531
14      event     spring 26.224773 32.536549 46.90560

......................................................................... ................................................... .......................

I found many ways to plot one y variable combining with a fuction (ex: event, here) using ggplot2 or qplot, but cannot find how to draw multiple plots all together for multiple y variables. 我发现了使用ggplot2或qplot绘制与功能结合的y变量的多种方法(例如事件),但找不到如何将多个y变量一起绘制多个图。
Thanks a lot for your help!! 非常感谢你的帮助!!

slim

Here's a way to do it with ggplot2 : 这是用ggplot2做到的一种方法:

library(reshape2)
library(ggplot2)

# Assume your data frame is named dat
dat.m = melt(dat, id.var=c("event","season"))
dat.m$season = factor(dat.m$season, levels=c("winter", "spring","wet.summer","fall"))

# If you want the two levels of event plotted side by side
ggplot(dat.m, aes(season, value, colour=event)) +
  facet_grid(. ~ variable) +
  geom_boxplot(width=0.7)

在此处输入图片说明

# If you want the levels of event to be faceted (plotted in separate panels)
ggplot(dat.m, aes(season, value)) +
  facet_grid(event ~ variable) +
  geom_boxplot()

在此处输入图片说明

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

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