如何在 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 小时,例如,没有秒数,因为我不需要它们。我试过很多不同的方法,但就是行不通。此刻我很绝望,对不起。也许有人可以帮助我?非常感谢!
你快到了。您可以稍微修改代码以获得所需的输出。需要修改的是:
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)
我用 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 小时,例如,没有秒数,因为我不需要它们。我试过很多不同的方法,但就是行不通。此刻我很绝望,对不起。也许有人可以帮助我?非常感谢!
你快到了。您可以稍微修改代码以获得所需的输出。需要修改的是:
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)