简体   繁体   English

R:如何绘制垂直线随时间变化?

[英]R : How to plot with vertical lines break by time?

I'm a new R user. 我是R新用户。 I want to get vertical lines-break by many levels on the plot like : 我想在情节上让垂直线突破许多层次,例如:

  • breaking by hour uses lines with very very light grey. 每小时中断使用的线条非常浅灰色。

  • breaking by day uses lines with light grey. 日间休息使用浅灰色线条。

  • breaking by week uses lines with grey. 每周中断使用灰色线条。

在此处输入图片说明

library(ggplot2)
library(lubridate)
library(scales)
ggplot(df,aes(x=df$DateTime,y=df$Val,ymin=0),xaxt="n") + 
  scale_x_datetime(labels = date_format("%m-%d"), breaks = date_breaks("days"), expand=c(0,0)) + 
  theme(text=element_text(size=10),
        legend.text=element_text(size=10),
        panel.background = element_rect(fill = "white", colour = "black")) +
  geom_line(lwd=1)

here is example df : 这是df的示例:

df <- read.table(text="
           DateTime,      Val
                 2017-06-13 00:00:00,5651.000
                 2017-06-13 01:00:00,5937.000
                 2017-06-13 02:00:00,5910.000
                 2017-06-13 03:00:00,5778.000
                 2017-06-13 04:00:00,5784.000
                 2017-06-13 05:00:00,5368.000
                 2017-06-13 06:00:00,5507.000
                 2017-06-13 07:00:00,5720.000
                 2017-06-13 08:00:00,6310.000
                 2017-06-13 09:00:00,6497.000
                 2017-06-13 10:00:00,6662.000
                 2017-06-13 11:00:00,6502.000
                 2017-06-13 12:00:00,6400.000
                 2017-06-13 13:00:00,6823.000
                 2017-06-13 14:00:00,6920.000
                 2017-06-13 15:00:00,6542.000
                 2017-06-13 16:00:00,6546.000
                 2017-06-13 17:00:00,5677.000
                 2017-06-13 18:00:00,5771.000
                 2017-06-13 19:00:00,5982.000
                 2017-06-13 20:00:00,6008.000
                 2017-06-13 21:00:00,6152.000
                 2017-06-13 22:00:00,6080.000
                 2017-06-13 23:00:00,5879.000
                 2017-06-14 00:00:00,5760.000
                 2017-06-14 01:00:00,5872.000
                 2017-06-14 02:00:00,5961.000
                 2017-06-14 03:00:00,5782.000
                 2017-06-14 04:00:00,5860.000
                 2017-06-14 05:00:00,5361.000
                 2017-06-14 06:00:00,5567.000
                 2017-06-14 07:00:00,5864.000
                 2017-06-14 08:00:00,6349.000
                 2017-06-14 09:00:00,6556.000
                 2017-06-14 10:00:00,6646.000
                 2017-06-14 11:00:00,6572.000
                 2017-06-14 12:00:00,6610.000
                 2017-06-14 13:00:00,6612.000
                 2017-06-14 14:00:00,6844.000
                 2017-06-14 15:00:00,6717.000
                 2017-06-14 16:00:00,6919.500
                 2017-06-14 17:00:00,6294.500
                 2017-06-14 18:00:00,5573.000
                 2017-06-14 19:00:00,5993.000
                 2017-06-14 20:00:00,6053.000
                 2017-06-14 21:00:00,6102.000
                 2017-06-14 22:00:00,6159.000
                 2017-06-14 23:00:00,6047.000
                 2017-06-15 00:00:00,5882.000
                 2017-06-15 01:00:00,5973.000
                 2017-06-15 02:00:00,6082.000
                 2017-06-15 03:00:00,5994.000
                 2017-06-15 04:00:00,5904.000
                 2017-06-15 05:00:00,5409.000
                 2017-06-15 06:00:00,5511.000
                 2017-06-15 07:00:00,5771.000
                 2017-06-15 08:00:00,6363.000
                 2017-06-15 09:00:00,6559.000
                 2017-06-15 10:00:00,6838.000
                 2017-06-15 11:00:00,6659.000
                 2017-06-15 12:00:00,6426.000
                 2017-06-15 13:00:00,6909.000
                 2017-06-15 14:00:00,6879.000
                 2017-06-15 15:00:00,6837.000
                 2017-06-15 16:00:00,6813.000
                 2017-06-15 17:00:00,6007.000
                 2017-06-15 18:00:00,6046.000
                 2017-06-15 19:00:00,6088.000
                 2017-06-15 20:00:00,6281.000
                 2017-06-15 21:00:00,6345.000
                 2017-06-15 22:00:00,6319.000
                 2017-06-15 23:00:00,6063.000
                 2017-06-16 00:00:00,5897.000
                 2017-06-16 01:00:00,6113.000
                 2017-06-16 02:00:00,6198.000
                 2017-06-16 03:00:00,6029.000
                 2017-06-16 04:00:00,5970.000
                 2017-06-16 05:00:00,5486.000
                 2017-06-16 06:00:00,5621.000
                 2017-06-16 07:00:00,5885.000
                 2017-06-16 08:00:00,6440.000
                 2017-06-16 09:00:00,6604.000
                 2017-06-16 10:00:00,6731.000
                 2017-06-16 11:00:00,6523.000
                 2017-06-16 12:00:00,6411.000
                 2017-06-16 13:00:00,6882.000
                 2017-06-16 14:00:00,6908.000
                 2017-06-16 15:00:00,6843.000
                 2017-06-16 16:00:00,6791.000
                 2017-06-16 17:00:00,5324.000
                 2017-06-16 18:00:00,5207.000
                 2017-06-16 19:00:00,5323.000
                 2017-06-16 20:00:00,6186.000
                 2017-06-16 21:00:00,6278.000
                 2017-06-16 22:00:00,6156.000
                 2017-06-16 23:00:00,6001.000
                 2017-06-17 00:00:00,5884.000
                 2017-06-17 01:00:00,6016.000
                 2017-06-17 02:00:00,5959.000
                 2017-06-17 03:00:00,5804.000
                 2017-06-17 04:00:00,5669.000
                 2017-06-17 05:00:00,4760.000
                 2017-06-17 06:00:00,4588.000
                 2017-06-17 07:00:00,4428.000
                 2017-06-17 08:00:00,4602.000
                 2017-06-17 09:00:00,4762.000
                 2017-06-17 10:00:00,4912.000
                 2017-06-17 11:00:00,4828.000
                 2017-06-17 12:00:00,4828.000
                 2017-06-17 13:00:00,4970.250
                 2017-06-17 14:00:00,4997.250
                 2017-06-17 15:00:00,4914.250
                 2017-06-17 16:00:00,4933.250
                 2017-06-17 17:00:00,4753.000
                 2017-06-17 18:00:00,4712.000
                 2017-06-17 19:00:00,3910.000
                 2017-06-17 20:00:00,3625.000
                 2017-06-17 21:00:00,3398.000
                 2017-06-17 22:00:00,3379.000
                 2017-06-17 23:00:00,3309.000
                 2017-06-18 00:00:00,3313.000
                 2017-06-18 01:00:00,3291.000
                 2017-06-18 02:00:00,3289.000
                 2017-06-18 03:00:00,3287.000
                 2017-06-18 04:00:00,3263.000
                 2017-06-18 05:00:00,3220.000
                 2017-06-18 06:00:00,3215.000
                 2017-06-18 07:00:00,2311.000
                 2017-06-18 08:00:00,1804.000
                 2017-06-18 09:00:00,1126.000
                 2017-06-18 10:00:00,931.000
                 2017-06-18 11:00:00,944.500
                 2017-06-18 12:00:00,965.500
                 2017-06-18 13:00:00,951.000
                 2017-06-18 14:00:00,972.000
                 2017-06-18 15:00:00,963.000
                 2017-06-18 16:00:00,949.000
                 2017-06-18 17:00:00,948.000
                 2017-06-18 18:00:00,941.000
                 2017-06-18 19:00:00,938.000
                 2017-06-18 20:00:00,932.000
                 2017-06-18 21:00:00,922.000
                 2017-06-18 22:00:00,935.000
                 2017-06-18 23:00:00,1011.231
                 2017-06-19 00:00:00,1022.231
                 2017-06-19 01:00:00,1017.231
                 2017-06-19 02:00:00,1020.231
                 2017-06-19 03:00:00,1020.231
                 2017-06-19 04:00:00,1101.231
                 2017-06-19 05:00:00,1912.231
                 2017-06-19 06:00:00,3530.231
                 2017-06-19 07:00:00,4894.231
                 2017-06-19 08:00:00,6238.231
                 2017-06-19 09:00:00,6523.231
                 2017-06-19 10:00:00,6502.231
                 2017-06-19 11:00:00,6408.231
                 2017-06-19 12:00:00,6275.000
                 2017-06-19 13:00:00,6760.000
                 2017-06-19 14:00:00,6804.000
                 2017-06-19 15:00:00,6943.000
                 2017-06-19 16:00:00,6935.000
                 2017-06-19 17:00:00,5984.000
                 2017-06-19 18:00:00,5754.000
                 2017-06-19 19:00:00,6086.000
                 2017-06-19 20:00:00,6219.000
                 2017-06-19 21:00:00,6206.000
                 2017-06-19 22:00:00,6172.000
                 2017-06-19 23:00:00,6029.000
                 2017-06-20 00:00:00,5836.000",sep=",",header=TRUE,stringsAsFactors=FALSE)
df$DateTime <- as.POSIXct(df$DateTime)

The output should be like this : 输出应该是这样的: 在此处输入图片说明

I just edited it in Paint haha. 我只是在Paint haha​​中编辑过它。 You can see that I put very light lines with 3 different levels (starts at weekday breaks every 'Monday', day and hour). 可以看到,我将线条轻描淡写地分为3个不同的级别(从每个“星期一”,日和小时的工作日休息开始)。

Thank you so much for your helps. 非常感谢您的帮助。

First of all, never use df$.. inside aes , it will mess up ggplot2 non standard evaluation . 首先,从来没有使用df$..里面aes ,它会弄乱ggplot2 非标准评价

We can use the major and minor panel.grid to display different vertical lines. 我们可以使用主要和次要panel.grid显示不同的垂直线。 However this can be done only for two 'levels'. 但是,只能对两个“级别”执行此操作。

But we can also add a geom_vline() which will add some vertical lines: 但是我们也可以添加geom_vline() ,它会添加一些垂直线:

library(ggplot2)
library(scales)

# Create the dataframe with the weeks interval
weeks <- data.frame(w = seq(min(df$DateTime), max(df$DateTime), 'week'))

ggplot(df) + 
    # plot line
    geom_line(aes(x = DateTime,y=Val)) +
    # plot vertical weekly lines
    geom_vline(data = weeks, 
               aes(xintercept = as.numeric(w)),
               color = 'grey55', size = 2) +
    scale_x_datetime(labels = date_format("%m-%d"),
                     breaks = date_breaks("days"),
                     # add minor breaks
                     minor_breaks = date_breaks("hours"),
                     expand = c(0,0)) +
    theme(text = element_text(size=10),
          legend.text = element_text(size=10),
          panel.background = element_rect(fill = "white", colour = "black"),
          # display daily grid
          panel.grid.major.x = element_line(color = 'grey75', size = 1),
          # display hourly grid
          panel.grid.minor.x = element_line(color = 'grey85', size = .1)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R:ggplot:图显示垂直线而不是时程 - R: ggplot: plot shows vertical lines instead of time course R 中的 plot() - 如何对两条垂直线之间的区域进行着色? - plot() in R - how to shade an area between two vertical lines? 如何使用风险表 (R) 为生存 plot 添加垂直线和注释 - How to add vertical lines and annotation to survival plot with risk table (R) R plot_ly():根据时间数据向绘图添加多条垂直线 - R plot_ly(): adding multiple vertical lines to a plot based on time data 如何在 r 中 plot 2 条时间序列线及其趋势线 - How to plot 2 time series lines and their trend lines in r 在时间序列图中添加垂直线 - Add vertical lines to time-series plot 如何使用时间序列 plot 在 r plotly 中添加虚线垂直线 - How to add a dash vertical line in r plotly with time series plot 如何绘制直方图,中间的垂直线被删除? - How to plot a histogram with vertical lines in between removed? 如何命名X轴上由R图中垂直线分隔的部分(包ggplot2)? - How to name sections on x axis that are separated by vertical lines in an R plot (package ggplot2)? 如何在ggplot2 / R的绘图轴限制之外添加垂直线和水平文本? - How can I add vertical lines and horizontal text out of the plot axis limits in ggplot2/R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM