我已经被困在一个特定问题上超过一天了,我希望你们可以帮助我。我想用ggplot2创建单独的箱图,用于每个特定位置(loc_nr)的一组环境变量(并且每个位置具有不同数量的数据点)。我只设法在x轴上为所有位置创建一个带有许多箱图(表示环境变量)的大图。我想用箱形图(每个位置一个)生成多个小数字。
我的数据集(小部分):所有变量名都向左移,loc_nr应从01开始,so_temp在0.230等等。
loc_nr so_temp so_spcond so_ph so_turbid so_chl1 so_o2 depth current water_fluc substrate silt org_matter wood_nr connect veg_shore veg_water0 veg_water1 shade water_colour bycatch
1 01 0.230 -0.670 1.096 -0.386 -0.585 1.428 -0.468 -0.492 -1.008 -1.010 -0.863 3.933 -0.131 -0.706 -0.343 -0.277 -0.157 -0.291 0.639 3.318
2 01 0.178 -1.065 0.663 -0.315 -0.608 1.428 -0.406 -0.492 -1.008 -0.386 -0.863 3.933 -0.131 -0.706 -0.343 1.094 -0.157 -0.291 -1.481 -0.410
3 01 0.645 -0.670 0.969 0.185 -0.314 1.206 -0.220 -0.492 -1.008 -0.386 0.031 2.510 -0.131 -0.706 -0.343 -0.277 -0.157 -0.291 -1.481 -0.410
4 01 0.075 -0.276 0.383 0.224 -0.314 1.157 -0.096 -0.492 -1.008 -1.010 0.031 2.510 -0.131 -0.706 -0.343 -0.277 -0.157 -0.291 -1.481 -0.410
5 01 0.807 -0.276 0.332 1.779 -0.224 0.115 -0.468 -0.492 -1.008 -1.010 1.818 5.357 -0.033 -0.706 -0.343 -0.277 -0.157 -0.291 -0.774 -0.410
6 01 0.184 -0.276 0.816 0.363 -0.269 0.401 -0.406 -0.492 -1.008 -0.386 1.818 5.357 -0.033 -0.706 -0.343 -0.277 -0.157 -0.291 -0.774 -0.410
7 01 0.052 -1.065 1.452 0.839 -0.066 -0.117 -0.406 -0.492 -1.008 -0.386 0.031 -0.337 -0.131 -0.706 -0.343 -0.277 -0.157 -0.291 -1.481 -0.410
8 01 0.553 -0.276 0.561 0.576 -0.201 0.963 -0.282 -0.492 -1.008 -0.386 0.925 -0.337 -0.131 -0.706 -0.343 -0.277 -0.157 -0.291 0.639 -0.410
9 01 0.173 -0.407 0.791 -0.085 -0.269 0.634 -0.592 -0.492 -1.008 1.484 0.031 -0.337 -0.131 -0.706 -0.343 -0.277 -0.157 0.981 -0.774 -0.410
10 02 2.565 -0.144 1.223 -0.262 -0.698 2.039 -0.096 -0.492 0.990 -0.386 -0.863 2.510 0.261 0.471 -0.343 -0.277 -0.157 -0.291 0.639 -0.410
11 02 2.565 -0.144 1.223 -0.262 -0.698 2.039 -0.220 -0.492 0.990 -0.386 0.031 3.933 -0.033 0.471 -0.343 -0.277 -0.157 -0.291 0.639 1.454
12 02 2.565 -0.144 1.223 -0.262 -0.698 2.039 0.028 -0.492 0.990 -0.386 0.031 1.086 -0.033 0.471 -0.343 -0.277 -0.157 -0.291 0.639 1.454
13 02 2.565 -0.144 1.223 -0.262 -0.698 2.039 -0.220 -0.492 0.990 -0.386 -0.863 1.086 -0.131 0.471 -0.343 -0.277 -0.157 -0.291 0.639 -0.410
14 02 2.565 -0.144 1.223 -0.262 -0.698 2.039 -0.530 -0.492 0.990 1.484 -0.863 -0.337 -0.131 0.471 -0.343 -0.277 -0.157 -0.291 0.639 -0.410
15 02 2.565 -0.144 1.223 -0.262 -0.698 2.039 -0.406 -0.492 0.990 1.484 -0.863 -0.337 -0.131 0.471 -0.343 -0.277 -0.157 -0.291 -0.068 -0.410
16 02 2.565 -0.144 1.223 -0.262 -0.698 2.039 -0.592 -0.492 0.990 0.237 -0.863 -0.337 -0.131 0.471 -0.343 -0.277 -0.157 2.253 -0.774 -0.410
17 02 2.565 -0.144 1.223 -0.262 -0.698 2.039 -0.158 -0.492 0.990 0.237 -0.863 -0.337 -0.033 0.471 -0.343 -0.277 -0.157 -0.291 0.639 -0.410
18 02 2.565 -0.144 1.223 -0.262 -0.698 2.039 -0.406 -0.492 0.990 -1.010 -0.863 -0.337 -0.131 0.471 -0.343 -0.277 -0.157 0.981 0.639 -0.410
19 02 2.565 -0.144 1.223 -0.262 -0.698 2.039 -0.654 -0.492 0.990 -1.010 -0.863 -0.337 -0.131 0.471 -0.343 -0.277 -0.157 -0.291 -0.774 -0.410
20 02 2.565 -0.144 1.223 -0.262 -0.698 2.039 -0.592 -0.492 0.990 -1.010 -0.863 -0.337 -0.131 0.471 -0.343 -0.277 -0.157 -0.291 -0.774 -0.410
22 02 2.565 -0.144 1.223 -0.262 -0.698 2.039 -0.406 -0.492 0.990 -1.010 -0.863 -0.337 -0.131 0.471 -0.343 -0.277 -0.157 0.981 0.639 -0.410
23 02 2.565 -0.144 1.223 -0.262 -0.698 2.039 -0.654 -0.492 0.990 -1.010 -0.863 -0.337 -0.131 0.471 -0.343 -0.277 -0.157 -0.291 -0.774 -0.410
24 02 2.565 -0.144 1.223 -0.262 -0.698 2.039 -0.592 -0.492 0.990 -1.010 -0.863 -0.337 -0.131 0.471 -0.343 -0.277 -0.157 -0.291 -0.774 -0.410
25 03 0.818 -0.144 -0.966 -0.472 -0.641 -0.582 -0.220 2.026 0.990 0.237 -0.863 -0.337 -0.131 1.648 -0.343 -0.277 -0.157 -0.291 0.639 -0.410
27 03 0.818 -0.144 -0.966 -0.472 -0.641 -0.582 -0.592 2.026 0.990 1.484 -0.863 -0.337 -0.131 1.648 -0.343 -0.277 -0.157 -0.291 0.639 -0.410
28 03 0.818 -0.144 -0.966 -0.472 -0.641 -0.582 -0.530 2.026 0.990 1.484 -0.863 -0.337 -0.131 1.648 -0.343 -0.277 -0.157 -0.291 0.639 -0.410
29 05 1.706 -0.013 0.154 -0.405 -0.134 0.159 -0.096 -0.492 0.990 -0.386 1.818 -0.337 -0.131 -0.706 -0.343 -0.277 -0.157 -0.291 0.639 1.454
30 05 1.706 -0.013 0.154 -0.405 -0.134 0.159 -0.468 -0.492 0.990 -0.386 1.818 -0.337 -0.131 -0.706 -0.343 -0.277 -0.157 -0.291 0.639 -0.410
31 05 1.706 -0.013 0.154 -0.405 -0.134 0.159 -0.096 -0.492 0.990 0.237 -0.863 -0.337 -0.131 -0.706 -0.343 -0.277 -0.157 -0.291 -1.481 -0.410
32 05 1.706 -0.013 0.154 -0.405 -0.134 0.159 -0.530 -0.492 0.990 1.484 -0.863 -0.337 -0.131 -0.706 -0.343 -0.277 -0.157 -0.291 -1.481 -0.410
33 05 1.706 -0.013 0.154 -0.405 -0.134 0.159 -0.530 -0.492 0.990 1.484 -0.863 -0.337 -0.131 -0.706 -0.343 -0.277 -0.157 -0.291 -1.481 -0.410
44 07 -0.202 -0.013 0.561 2.957 4.310 -0.432 -0.220 -0.492 -1.008 -1.010 0.925 -0.337 0.359 -1.884 -0.343 -0.277 -0.157 -0.291 0.639 -0.410
45 07 -0.162 1.039 0.205 -0.104 0.047 -0.267 0.401 -0.492 -1.008 -1.010 1.818 -0.337 -0.131 -1.884 -0.343 -0.277 -0.157 -0.291 0.639 -0.410
46 07 0.132 1.039 0.154 -0.124 0.250 -0.325 -0.530 -0.492 -1.008 -1.010 2.712 -0.337 -0.131 -1.884 -0.343 -0.277 -0.157 -0.291 0.639 -0.410
我正在使用此代码生成图:
library(ggplot2)
dat.m <- reshape2::melt(env_alles,id.vars='loc_nr', measure.vars=c("so_temp", "so_spcond", "so_ph", "so_turbid", "so_chl1", "so_o2", "depth", "current",
"water_fluc", "substrate", "silt","org_matter", "wood_nr", "connect", "veg_shore",
"veg_water0", "veg_water1","shade", "water_colour", "bycatch"))
p <- ggplot(dat.m) + geom_boxplot(aes(x=loc_nr, y=value, color=variable))
我想在网格中显示每个位置(在这个较小的数据集的情况下:位置01,02,03,05,07)的(一组环境变量)箱图的单独数字。
希望你们能帮我解决这个问题。非常感谢提前!
答案 0 :(得分:-1)
当您继续学习和探索R
包facet_wrap
时,您会发现ggplot2
功能非常有用!
这是你的想法吗?
ggplot(dat.m) +
geom_boxplot(aes(x = loc_nr, y=value, color=variable))+
facet_wrap(~loc_nr, scales = 'free_x')
由于每variable
有多个loc_nr
s,我们使用position = 'dodge'
来调整variable
的水平位置。