【问题标题】:How to plot the data by different group?如何按不同组绘制数据?
【发布时间】:2021-01-13 12:27:40
【问题描述】:

我有一个如下所示的数据框,

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 组和然后将日期与处理之间的关系绘制在一个图中。

希望有人可以提供帮助。谢谢!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    也许你正在寻找这个。重塑后,您可以基于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')
    

    输出:

    【讨论】:

      猜你喜欢
      • 2018-05-17
      • 2023-02-21
      • 2023-03-11
      • 2019-03-03
      • 2019-10-15
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      相关资源
      最近更新 更多