繁体   English   中英

如何在r的ggplot2中缩放x轴的时间(小时)?

[英]How can I scale the time (hours) of my x-axis in ggplot2 in r?

我用ggplot2创建了一个ggplot。 该图是一年的测量时间序列,而我已将其组织成几个月。 这是我的代码:

ggplot(data = mydataframe, aes(x = time)) +
  geom_point(aes(y = H_flux_6262_R3,  color = "6262-R3"),
             alpha = 0.5,
             shape = 19) +
  geom_point(aes(y = H_flux_7200_HS,  color = "7200-HS"),
             alpha = 0.5,
             shape = 5) +
  geom_point(
    aes(y = H_flux_dif_6262_R3_7200_HS,  color = "Difference"),
    alpha = 0.5,
    shape = 5
  ) +
  facet_wrap( ~ Month, nrow = 3) +
  theme(text = element_text(),
        axis.text.x = element_text(angle = 60, hjust = 1)) +
  theme(legend.position = "right", legend.title = element_blank()) +
  scale_color_manual(values = c("#56B4E9", "#E69F00", "palegreen4")) +
  labs(x = "time", y = "H flux")

在此处输入图片说明

我的时间格式为:%H:%M:%S,例如00:00:00。

H flux 6262_R3 H_flux_7200_HS Time
 100             500         02:00:00
 400             700         02:30:00
 400             700         03:00:00
 400             700         03:30:00
 400             700         04:00:00
 100             500         04:30:00
 400             700         05:00:00
 400             700         05:30:00
 400             700         06:30:00
 400             700         07:00:00

依此类推,直到00:00:00。 我每30分钟测量一次数据。 当我绘图时,我遇到的问题是它无法将其缩放到例如每4个小时,而没有秒,因为我不需要它们。 我已经尝试了许多不同的方法,但它根本无法正常工作。 我很绝望,对不起。 Mabye有人可以帮助我吗? 我将不胜感激!

你快到了。 您可以稍微修改代码以获得所需的输出。 所需的修改是:

 1.Convert `Time` column to `POSIXct` type 2.The `facet_wrap` should use month part of `Time` column. Hence it can be written as: facet_wrap(~format(Time,"%m"), scales = "free") 3.Use of `scale_x_datetime` to format x-axis to write label every 4 hours can be as: scale_x_datetime(date_breaks = "4 hours", date_labels = "%Y-%m-%d %H:%M") 

解决方案:

#First prepare the data to have Time column of type POSIXct and diff column
mydataframe$Time = as.POSIXct(mydataframe$Time, format = "%m-%d-%Y %H:%M:%S")
mydataframe$H_flux_dif_6262_R3_7200_HS <- mydataframe$H_flux_7200_HS -
                                                   mydataframe$H_flux_6262_R3


library(ggplot2)
ggplot(data = mydataframe, aes(x = Time)) +
  geom_point(aes(y = H_flux_6262_R3,  color = "6262-R3"),
             alpha = 0.5,
             shape = 19) +
  geom_point(aes(y = H_flux_7200_HS,  color = "7200-HS"),
             alpha = 0.5,
             shape = 5) +
  geom_point(
    aes(y = H_flux_dif_6262_R3_7200_HS,  color = "Difference"),
    alpha = 0.5,
    shape = 5
  ) +
  facet_wrap(~format(Time,"%m"), scales = "free") +
  scale_x_datetime(date_breaks = "4 hours", date_labels = "%Y-%m-%d %H:%M")+
  theme(text = element_text(),
        axis.text.x = element_text(angle = 60, hjust = 1)) +
  theme(legend.position = "right", legend.title = element_blank()) +
  scale_color_manual(values = c("#56B4E9", "#E69F00", "palegreen4")) +
  labs(x = "time", y = "H flux")

输出:

在此处输入图片说明

数据:数据与OP提供的数据相似。 我已将其延长至2个月。 此外,还包括日期的一部分时间。

mydataframe <- read.table(text = 
"H_flux_6262_R3 H_flux_7200_HS Time
100             500         '01-01-2018 02:00:00'
400             700         '01-01-2018 02:30:00'
400             700         '01-01-2018 03:00:00'
400             700         '01-01-2018 03:30:00'
400             700         '01-01-2018 04:00:00'
100             500         '01-01-2018 04:30:00'
400             700         '01-01-2018 05:00:00'
400             700         '01-01-2018 05:30:00'
400             700         '01-01-2018 06:00:00'
400             700         '01-01-2018 06:30:00'
400             700         '01-01-2018 07:00:00'
400             700         '01-01-2018 07:30:00'
400             700         '01-01-2018 08:00:00'
400             700         '01-01-2018 08:30:00'
400             700         '01-01-2018 09:00:00'
400             700         '01-01-2018 09:30:00'
400             700         '01-01-2018 10:00:00'
400             700         '01-01-2018 10:30:00'
400             700         '01-01-2018 11:00:00'
500             900         '01-01-2018 11:30:00'
500             900         '01-01-2018 12:00:00'
100             500         '02-01-2018 02:00:00'
400             700         '02-01-2018 02:30:00'
400             700         '02-01-2018 03:00:00'
400             700         '02-01-2018 03:30:00'
400             700         '02-01-2018 04:00:00'
100             500         '02-01-2018 04:30:00'
400             700         '02-01-2018 05:00:00'
400             700         '02-01-2018 05:30:00'
400             700         '02-01-2018 06:00:00'
400             700         '02-01-2018 06:30:00'
400             700         '02-01-2018 07:00:00'
400             700         '02-01-2018 07:30:00'
400             700         '02-01-2018 08:00:00'
400             700         '02-01-2018 08:30:00'
400             700         '02-01-2018 09:00:00'
400             700         '02-01-2018 09:30:00'
400             700         '02-01-2018 10:00:00'
400             700         '02-01-2018 10:30:00'
400             700         '02-01-2018 11:00:00'
500             900         '02-01-2018 11:30:00'
500             900         '02-01-2018 12:00:00'",
header = TRUE, stringsAsFactors = FALSE)

暂无
暂无

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

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