Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在x轴上显示所有30分钟的标签中断?_R_Ggplot2_Label - Fatal编程技术网

R 如何在x轴上显示所有30分钟的标签中断?

R 如何在x轴上显示所有30分钟的标签中断?,r,ggplot2,label,R,Ggplot2,Label,我有一些每隔30分钟记录的数据: df <- structure(list(from = c("2019-11-24T23:30Z", "2019-11-25T00:00Z", "2019-11-25T00:30Z", "2019-11-25T01:00Z", "2019-11-25T01:30Z", "2019-11-25T02:00Z", "2019-11-25T02:30Z", "2019-11-25T03:00Z", "2019-11-25T03:30Z", "2019-11

我有一些每隔30分钟记录的数据:

df <- structure(list(from = c("2019-11-24T23:30Z", "2019-11-25T00:00Z", 
"2019-11-25T00:30Z", "2019-11-25T01:00Z", "2019-11-25T01:30Z", 
"2019-11-25T02:00Z", "2019-11-25T02:30Z", "2019-11-25T03:00Z", 
"2019-11-25T03:30Z", "2019-11-25T04:00Z", "2019-11-25T04:30Z", 
"2019-11-25T05:00Z", "2019-11-25T05:30Z", "2019-11-25T06:00Z", 
"2019-11-25T06:30Z", "2019-11-25T07:00Z", "2019-11-25T07:30Z", 
"2019-11-25T08:00Z", "2019-11-25T08:30Z", "2019-11-25T09:00Z", 
"2019-11-25T09:30Z", "2019-11-25T10:00Z", "2019-11-25T10:30Z", 
"2019-11-25T11:00Z", "2019-11-25T11:30Z"), to = c("2019-11-25T00:00Z", 
"2019-11-25T00:30Z", "2019-11-25T01:00Z", "2019-11-25T01:30Z", 
"2019-11-25T02:00Z", "2019-11-25T02:30Z", "2019-11-25T03:00Z", 
"2019-11-25T03:30Z", "2019-11-25T04:00Z", "2019-11-25T04:30Z", 
"2019-11-25T05:00Z", "2019-11-25T05:30Z", "2019-11-25T06:00Z", 
"2019-11-25T06:30Z", "2019-11-25T07:00Z", "2019-11-25T07:30Z", 
"2019-11-25T08:00Z", "2019-11-25T08:30Z", "2019-11-25T09:00Z", 
"2019-11-25T09:30Z", "2019-11-25T10:00Z", "2019-11-25T10:30Z", 
"2019-11-25T11:00Z", "2019-11-25T11:30Z", "2019-11-25T12:00Z"
), forecast = c(210L, 199L, 200L, 204L, 199L, 192L, 191L, 194L, 
197L, 192L, 193L, 194L, 204L, 210L, 242L, 272L, 293L, 290L, 275L, 
270L, 272L, 272L, 269L, 268L, 266L), actual = c(200L, 200L, 198L, 
189L, 191L, 193L, 197L, 193L, 193L, 194L, 199L, 207L, 230L, 255L, 
272L, 283L, 283L, 278L, 277L, 278L, 273L, 270L, 270L, 272L, 283L
), index = c("moderate", "moderate", "moderate", "moderate", 
"moderate", "moderate", "moderate", "moderate", "moderate", "moderate", 
"moderate", "moderate", "moderate", "moderate", "high", "high", 
"high", "high", "high", "high", "high", "high", "high", "high", 
"high")), row.names = c(NA, 25L), class = "data.frame")
绘制后,如何在x轴上显示每30分钟一次的间隔,而不是默认的打断

library(ggplot2)

ggplot(df) +
  geom_line(aes(from, forecast),
             colour = "grey",
            linetype = "dashed") +
  geom_line(aes(from, actual),
             colour = "blue") +
  theme(axis.text.x = element_text(angle = 90, hjust = 0))
产生:

> df
                from                to forecast actual    index
1  2019-11-24T23:30Z 2019-11-25T00:00Z      210    200 moderate
2  2019-11-25T00:00Z 2019-11-25T00:30Z      199    200 moderate
3  2019-11-25T00:30Z 2019-11-25T01:00Z      200    198 moderate
4  2019-11-25T01:00Z 2019-11-25T01:30Z      204    189 moderate
5  2019-11-25T01:30Z 2019-11-25T02:00Z      199    191 moderate
6  2019-11-25T02:00Z 2019-11-25T02:30Z      192    193 moderate
7  2019-11-25T02:30Z 2019-11-25T03:00Z      191    197 moderate
8  2019-11-25T03:00Z 2019-11-25T03:30Z      194    193 moderate
9  2019-11-25T03:30Z 2019-11-25T04:00Z      197    193 moderate
10 2019-11-25T04:00Z 2019-11-25T04:30Z      192    194 moderate
11 2019-11-25T04:30Z 2019-11-25T05:00Z      193    199 moderate
12 2019-11-25T05:00Z 2019-11-25T05:30Z      194    207 moderate
13 2019-11-25T05:30Z 2019-11-25T06:00Z      204    230 moderate
14 2019-11-25T06:00Z 2019-11-25T06:30Z      210    255 moderate
15 2019-11-25T06:30Z 2019-11-25T07:00Z      242    272     high
16 2019-11-25T07:00Z 2019-11-25T07:30Z      272    283     high
17 2019-11-25T07:30Z 2019-11-25T08:00Z      293    283     high
18 2019-11-25T08:00Z 2019-11-25T08:30Z      290    278     high
19 2019-11-25T08:30Z 2019-11-25T09:00Z      275    277     high
20 2019-11-25T09:00Z 2019-11-25T09:30Z      270    278     high
21 2019-11-25T09:30Z 2019-11-25T10:00Z      272    273     high
22 2019-11-25T10:00Z 2019-11-25T10:30Z      272    270     high
23 2019-11-25T10:30Z 2019-11-25T11:00Z      269    270     high
24 2019-11-25T11:00Z 2019-11-25T11:30Z      268    272     high
25 2019-11-25T11:30Z 2019-11-25T12:00Z      266    283     high

确保x轴具有适当的格式,然后您可以使用
缩放x\u日期时间(date\u breaks=“30分钟”)
设置中断

df %>%
  mutate(from = as.POSIXct(from, format="%Y-%m-%dT%H:%M")) %>%
  ggplot() +
  geom_line(aes(from, forecast),
            colour = "grey",
            linetype = "dashed") +
  geom_line(aes(from, actual),
            colour = "blue") +
  theme(axis.text.x = element_text(angle = 90, hjust = 0)) +
  scale_x_datetime(date_breaks = "30 min")

我离得太近了!我对这个解有一个非常接近的近似值。我的是
format=“%Y-%m-%d%H:%m”
,而你的(正确的)是
format=“%Y-%m-%dT%H:%m”