简体   繁体   中英

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

I have created a ggplot with ggplot2. 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 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. I have measurements of my data each 30 minutes. 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. 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? 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. I have extended it to cover 2 months. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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