Reputation: 223
I'm a new R user. 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 <- 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. You can see that I put very light lines with 3 different levels (starts at weekday breaks every 'Monday', day and hour).
Thank you so much for your helps.
Upvotes: 0
Views: 1162
Reputation: 13680
First of all, never use df$..
inside aes
, it will mess up ggplot2
non standard evaluation.
We can use the major and minor panel.grid
to display different vertical lines. However this can be done only for two 'levels'.
But we can also add a geom_vline()
which will add some vertical lines:
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))
Upvotes: 2