简体   繁体   English

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

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

I have created a ggplot with ggplot2. 我用ggplot2创建了一个ggplot。 The plot is a timeseries of measurements for a year and I have organized it in months. 该图是一年的测量时间序列,而我已将其组织成几个月。 This is my code: 这是我的代码:

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")

在此处输入图片说明

The format of my time is: %H:%M:%S, so 00:00:00 for example. 我的时间格式为:%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

and so on til 00:00:00. 依此类推,直到00:00:00。 I have measurements of my data each 30 minutes. 我每30分钟测量一次数据。 When I plot I have the problem that it doesn't scale it to each 4 hours for example, without the seconds since I don't need them. 当我绘图时,我遇到的问题是它无法将其缩放到例如每4个小时,而没有秒,因为我不需要它们。 I have tried it with so many different methods and it just won't work. 我已经尝试了许多不同的方法,但它根本无法正常工作。 I am desperate at this point, sorry. 我很绝望,对不起。 Mabye someone can help me? Mabye有人可以帮助我吗? I'd appreciate it! 我将不胜感激!

You are almost there. 你快到了。 You can modify your code a bit to get desired output. 您可以稍微修改代码以获得所需的输出。 The modification which are needed are: 所需的修改是:

 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") 

The solution: 解决方案:

#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")

Output: 输出:

在此处输入图片说明

Data: The data is in similar line of that provided by OP. 数据:数据与OP提供的数据相似。 I have extended it to cover 2 months. 我已将其延长至2个月。 Also, to include Date part of time. 此外,还包括日期的一部分时间。

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