r - 如何按不同组绘制数据?
问题描述
我有一个如下所示的数据框,
structure(list(X = 4L, day = structure(1546556400, class = c("POSIXct",
"POSIXt"), tzone = ""), Dew.Point..S.THB.10990050.10979038.3....C..IGN.Brandbjerg.Met =
3.89458333333333,
PAR..S.LIA.10990050.10977527.1...uE..IGN.Brandbjerg.Met.2.m = 36.9756944444444,
RH..S.THB.10990050.10979038.2......IGN.Brandbjerg.Met.2.m = 93.8670138888889,
Solar.Radiation..S.LIB.10990050.10981132.1...W.m.2..IGN.Brandbjerg.Met.2.m = 17.5173611111111,
Rain = 0, Pressure = 1021.72534722222, Temperature.Air.T.2.m = 4.79805555555556,
Temperature.0.5.cm.Calluna = 4.26552083333333, Temperature..0.5.cm.Deschampsia = 4.26229166666667,
SWC.0.10.Calluna = 0.143915277777778, SWC.0.10.Deschampsia = 0.145047569444444,
Tsoil_11 = 3.93118055555556, Tsoil_12 = 3.31326388888889,
Tsoil_13 = 3.39402777777778, Tsoil_14 = 4.02045138888889,
Tsoil_21 = 4.40982638888889, Tsoil_22 = 4.49684027777778,
Tsoil_23 = 4.47291666666667, Tsoil_24 = 4.18284722222222,
Tsoil_31 = 4.1540625, Tsoil_32 = 4.10364583333333, Tsoil_33 = 3.84708333333333,
Tsoil_34 = 3.57652777777778, SWC_11 = 0.196202777777778,
SWC_12 = 0.265491666666667, SWC_13 = 0.273163194444444, SWC_14 = 0.206322222222222,
SWC_21 = 0.243164583333333, SWC_22 = 0.2235375, SWC_23 = 0.212259027777778,
SWC_24 = 0.217371875, SWC_31 = 0.131544791666667, SWC_32 = 0.167493055555556,
SWC_33 = 0.188492708333333, SWC_34 = 0.226024305555556, Tsoil_41 = 4.624375,
Tsoil_42 = 4.38045138888889, Tsoil_43 = 3.55361111111111,
Tsoil_44 = 4.21378472222222, Tsoil_51 = 3.92625, Tsoil_52 = 3.76736111111111,
Tsoil_53 = 3.780625, Tsoil_54 = 3.99416666666667, Tsoil_61 = 4.50618055555556,
Tsoil_62 = 4.16097222222222, Tsoil_63 = 4.48430555555556,
Tsoil_64 = 4.18666666666667, SWC_41 = 0.184280555555556,
SWC_42 = 0.241078472222222, SWC_43 = 0.248336111111111, SWC_44 = 0.214388888888889,
SWC_51 = 0.247181597222222, SWC_52 = 0.253651736111111, SWC_53 = 0.2355375,
SWC_54 = 0.232023611111111, SWC_61 = 0.255844097222222, SWC_62 = 0.216786805555556,
SWC_63 = 0.247850347222222, SWC_64 = 0.213776041666667), row.names = 4L, class = "data.frame")
首先,我只是想绘制 SWC_plots 和一天之间的关系。我的代码是
daily_avedata$day <- as.POSIXct(daily_avedata$day,format = "%Y-%m-%d ") #tz="GMT"
Time<-daily_avedata$day
# transform the data from wide to long using gather function to plot multiple y with single x diagram.
swc<- daily_avedata[, grep("SWC_",names(daily_avedata))]
subdat<-cbind(Time,swc)
sub_long<- gather(subdat, SWC_Plots, swc, -Time)
ggplot(sub_long, aes(Time, swc, color=SWC_Plots))+
geom_point()
这个粗略的代码有效。
然后我想对 SWC_plots 数据进行分组,然后用日期/时间绘制它们。我的意思是我要将 SWC_plots 设置为不同的处理(A,B)然后进行绘图,就像将 swc_11/12/21/22/33/34/53/54/61/62 分组到 A,而其他地块到 B 组和然后在一个图中绘制天和处理之间的关系。
希望有人能帮忙。谢谢!
解决方案
也许你正在寻找这个。重塑后,您可以基于SWC
. 之后,您可以使用facet_wrap()
以查看跨组的行为。我使用了您分享的数据:
library(tidyverse)
#Code
sub_long<- gather(subdat, SWC_Plots, swc, -Time)
#Separate
sub_long %>% mutate(Dup=SWC_Plots) %>%
separate(Dup,c('V1','V2'),sep = '_') %>% select(-V1) %>%
mutate(Group=ifelse(as.numeric(V2) %in% c(11,12,21,22,33,34,53,54,61,62),'A','B')) %>%
ggplot(aes(Time, swc, color=SWC_Plots,group=Group))+
geom_point()+
facet_wrap(.~Group,nrow = 1,scales='free')
输出:
推荐阅读
- google-cloud-platform - GCP cloudbuild.yaml 条件步骤错误
- java - 需要帮助将用户界面添加到 docx4j 代码
- excel - 类未检测到日期成员
- excel - 如何在输入验证码之前暂停我的代码?
- mybatis - 将 iBatis 迁移到 MyBatis 到 spring 4 后面临已弃用的问题
- java - 分离的数字签名验证失败,但没有此类算法:提供者 SUN 的 SHA256WITHECDSA
- php - php 数组计数返回 1
- julia - 如何将对象数组传递给Julia中的函数?
- ffmpeg - Premiere 中的音频漂移(不是 VFR)
- regex - matlab regexprep:替换多个实例但在第一次替换后停止