Our youtube channel has lots of videos on data visualisation in r.
Visit our youtube channel https://www.youtube.com/c/TechAnswers88
library(dplyr)
library(ggplot2)
library(scales)
df <- data.frame(date = seq(as.Date('2021-01-01')
, as.Date('2021-12-31')
, by = "day")
, patients = as.integer(runif(365
,min = 1000
, max = 1200
))
)
df
hist(df$patients)
pl <- ggplot(data = df,aes(x= date, y = patients))
pl <- pl + geom_line()
pl
pl <- ggplot(data = df,aes(x= date, y = patients))
pl <- pl + geom_line()
pl <- pl + geom_smooth(se = FALSE, color = "red")
pl <- pl + theme_bw()
pl
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
We have changed the date_breaks to ‘1 week’ and minor_breaks to ‘1 day’
pl <- ggplot(data = df,aes(x= date, y = patients))
pl <- pl + geom_line()
pl <- pl + geom_smooth(se = FALSE, color = "red")
pl <- pl + theme_bw()
pl <- pl + scale_x_date(name = "Dates"
, date_labels = "%d %b"
, date_breaks = "1 week"
, minor_breaks = "1 day"
, sec.axis = dup_axis(name = "Week number"
,labels = scales::date_format('%W')))
pl <- pl + theme(axis.text.x = element_text(angle = 90,vjust = 0.5))
pl <- pl + labs(title = "Daily patients")
pl <- pl + labs(subtitle = "Blue line shows mean")
pl <- pl + labs(caption = "@techanswerts88")
pl <- pl + geom_hline(yintercept = mean(df$patients), color = "blue")
pl
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
We have changed the date_breaks to ‘1 month’ and minor_breaks to ‘1 week’
Try experimenting with different numbers.
For example ‘1 month’, ‘3 months’ , ‘1 year’ , ‘6 weeks’ and so on. Try and see the results.
pl <- ggplot(data = df,aes(x= date, y = patients))
pl <- pl + geom_line()
pl <- pl + geom_smooth(se = FALSE, color = "red")
pl <- pl + theme_bw()
pl <- pl + scale_x_date(name = "Dates"
, date_labels = "%d %b"
, date_breaks = "1 month"
, minor_breaks = "1 week"
, sec.axis = dup_axis(name = "Week number"
,labels = scales::date_format('%W')))
pl <- pl + theme(axis.text.x = element_text(angle = 90,vjust = 0.5))
pl <- pl + labs(title = "Daily patients")
pl <- pl + labs(subtitle = "Blue line shows mean")
pl <- pl + labs(caption = "@techanswerts88")
pl <- pl + geom_hline(yintercept = mean(df$patients), color = "blue")
pl
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'