[英]plotting boxplots of multiple y variables using ggplot2, qplot or others
我正在寻找用x = season和function = event绘制许多y变量的箱形图的方法。 我想一起绘制很多图:
第一图:x =季节,y = var1(函数=事件)第二图:x =季节,y = var1,函数= event。
我的数据如下所示。 实际上,我有很多变量。
................................................... .......................
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
................................................... .......................
我发现了使用ggplot2或qplot绘制与功能结合的y变量的多种方法(例如事件),但找不到如何将多个y变量一起绘制多个图。
非常感谢你的帮助!!
瘦
这是用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.