how to add dashed horizontal line with label in ggplot
17,366
to make the horizontal line dashed and red the following arguments should be included in the geom_hline function call:
linetype = 'dotted', col = 'red'
# Sample Data
library(tidyverse)
Month= c("Jan","Feb","Mar","Apr","May","Jun")
a = c(11,10,9,8,4,8)
test= data_frame(Month,a)
test$cum_total <- cumsum(test$a)
test$Month <- factor(test$Month, month.abb)
# ggplot
ggplot(data=test, aes(x=Month, y=cum_total, group=1)) +
geom_line()+
geom_point()+
geom_hline(yintercept=40, linetype='dotted', col = 'red')+
annotate("text", x = "Feb", y = 40, label = "Previous Level", vjust = -0.5)
Related videos on Youtube
Author by
Anandapadmanathan
Updated on June 04, 2022Comments
-
Anandapadmanathan almost 2 years
I have plotted a line plot. I have added a horizontal line on the plot. How to take horizontal line red dashed?
# Sample Data library(tidyverse) Month= c("Jan","Feb","Mar","Apr","May","Jun") a = c(11,10,9,8,4,8) test= data_frame(Month,a) test$cum_total <- cumsum(test$a) test$Month <- factor(test$Month, month.abb) # ggplot ggplot(data=test, aes(x=Month, y=cum_total, group=1)) + geom_line()+ geom_point()+ geom_hline(yintercept=40)+ annotate("text", x = "Feb", y = 40, label = "Previous Level", vjust = -0.5)
-
teunbrand almost 5 yearsReplace your
geom_hline()
withgeom_hline(yintercept = 40, linetype = 2, colour = "red")
and it should be dashed and red.
-
-
Rob almost 5 yearsWhy should he do that?
-
Kresten almost 5 yearsUpdated the answer
-
Rob almost 5 yearsI still don't see any explanation as to why he should do that
-
James Hirschorn over 2 yearsI would change it to
linetype = 'dashed'
.