Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.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轴标签使用时间值_R_Ggplot2 - Fatal编程技术网

R 对x轴标签使用时间值

R 对x轴标签使用时间值,r,ggplot2,R,Ggplot2,我有一些温度和湿度的气候数据,还有一个时间戳,它被转换成%H:%M中的时间 当使用ggplot2进行可视化时,时间会被排序——因为第一次测量是在14:00(下午2点)进行的,最后一次测量是在第二天10:27(上午10:27)进行的 如何防止ggplot2对x值进行排序?(见图表) MVE: 库(tidyverse) df=读取csv('./气候统计数据包含时间.csv') colnames(df)[1]头(df) #一个tibble:6x5 样本时间戳温度湿度时间 1 0 158125

我有一些温度和湿度的气候数据,还有一个时间戳,它被转换成
%H:%M
中的时间

当使用ggplot2进行可视化时,时间会被排序——因为第一次测量是在14:00(下午2点)进行的,最后一次测量是在第二天10:27(上午10:27)进行的

如何防止ggplot2对x值进行排序?(见图表)

MVE:

库(tidyverse)
df=读取csv('./气候统计数据包含时间.csv')
colnames(df)[1]头(df)
#一个tibble:6x5
样本时间戳温度湿度时间
1      0 1581253210.        21.9     47.6 14:00 
2      1 1581253275.        21.7     47.8 14:01 
3      2 1581253336.        21.7     47.8 14:02 
4      3 1581253397.        21.8     47.8 14:03 
5      4 1581253457.        21.7     47.8 14:04 
6      5 1581253520.        21.8     47.8 14:05 
>尾部(df)
#一个tibble:6x5
样本时间戳温度湿度时间
1   1203 1581326567.        19.1     49.8 10:22 
2   1204 1581326628.        19.1     49.7 10:23 
3   1205 1581326688.        19.1     49.9 10:24 
4   1206 1581326749.        19.1     49.9 10:25 
5   1207 1581326812.        19.1     49.7 10:26 
6   1208 1581326873.        19.1     49.8 10:27

解决方法

df %>%
    mutate(orig_seq = seq(1,nrow(df),1)) %>%
    ggplot(mapping=aes(x=reorder(time, orig_seq)) +
    geom_line(aes(y=temperature, color='red')) +
    geom_line(aes(y=humidity, color='blue'))

将时间戳格式化为适当的日期时间(假设原点为1970):


df$date\u time您能否提供一个可复制的示例?您是否可以在
时间戳
列中添加“日期”信息?我的建议是不要试图避免ggplot2排序行为,而是清理您的时间数据,使其符合简单排序的要求manipulation@Paul实际上,格式化时间戳是有效的,我可以保持ggplot2排序
> head(df)
# A tibble: 6 x 5
  sample   timestamp temperature humidity time  
   <dbl>       <dbl>       <dbl>    <dbl> <drtn>
1      0 1581253210.        21.9     47.6 14:00 
2      1 1581253275.        21.7     47.8 14:01 
3      2 1581253336.        21.7     47.8 14:02 
4      3 1581253397.        21.8     47.8 14:03 
5      4 1581253457.        21.7     47.8 14:04 
6      5 1581253520.        21.8     47.8 14:05 

> tail(df)
# A tibble: 6 x 5
  sample   timestamp temperature humidity time  
   <dbl>       <dbl>       <dbl>    <dbl> <drtn>
1   1203 1581326567.        19.1     49.8 10:22 
2   1204 1581326628.        19.1     49.7 10:23 
3   1205 1581326688.        19.1     49.9 10:24 
4   1206 1581326749.        19.1     49.9 10:25 
5   1207 1581326812.        19.1     49.7 10:26 
6   1208 1581326873.        19.1     49.8 10:27
df %>%
    mutate(orig_seq = seq(1,nrow(df),1)) %>%
    ggplot(mapping=aes(x=reorder(time, orig_seq)) +
    geom_line(aes(y=temperature, color='red')) +
    geom_line(aes(y=humidity, color='blue'))
df$date_time <- as.POSIXct(df$timestamp, origin="1970-01-01", tz = "GMT")