Home > Software design >  Scale linetype per individual variable ggplot R
Scale linetype per individual variable ggplot R

Time:04-27

I'm plotting a figure with 2 lines, one for Value 1 and one for Value 2. Adding the scale_colour_manual works, however, adding scale_linetype_manual does not. I am trying to set Value 1 to dashed and Value 2 to twodash.

Would you be able to help me add a linetype to the lines?

structure(list(Date = structure(c(16801, 16832, 16861, 16892, 
16922, 16953, 16983, 17014, 17045, 17075, 17106, 17136, 17167, 
17198, 17226, 17257, 17287, 17318, 17348, 17379, 17410, 17440, 
17471, 17501, 17532, 17563, 17591, 17622, 17652, 17683, 17713, 
17744, 17775, 17805, 17836, 17866, 17897, 17928, 17956, 17987, 
18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231, 18262, 
18293, 18322, 18353, 18383, 18414, 18444, 18475, 18506, 18536, 
18567, 18597, 18628, 18659, 18687, 18718, 18748, 18779, 18809, 
18840, 18871, 18901, 18932, 18962), class = "Date"), `Value 1` = c(20.2820285962056, 
20.1656446783417, 20.1688905436013, 20.2421245198425, 20.3262152504194, 
20.2795155085514, 20.2726004595672, 20.3140265649524, 20.3293861068442, 
20.3738399424877, 20.3107991411253, 20.4308509146563, 20.4723974934767, 
20.4599242988163, 20.4861004817467, 20.4738989241297, 20.4913098610169, 
20.4630264740941, 20.4808708640221, 20.4998454834076, 20.4881131250879, 
20.5878140515117, 20.5840897258987, 20.623541664943, 20.6164801258481, 
20.6663314702947, 20.6303376871471, 20.6477187254335, 20.6607477829638, 
20.6557343789061, 20.6766932523984, 20.7050662428808, 20.7493243890245, 
20.7200678918589, 20.5571237237406, 20.576069484868, 20.4616812747077, 
20.5673118582694, 20.6150163500714, 20.5742834893148, 20.5968661409312, 
20.4214813407422, 20.47242477387, 20.4886463911621, 20.4122358035502, 
20.4332890140939, 20.458611442025, 20.5079434805371, 20.5473812163456, 
20.4768502514293, 20.3836301933903, 20.0687058983601, 20.223450202593, 
20.2709161852589, 20.3266867010993, 20.3182701149762, 20.387793425489, 
20.3399687362029, 20.3882025799041, 20.5982993492042, 20.6912374278537, 
20.7787871526229, 20.879054673808, 20.912422427002, 20.9227371143247, 
20.9264326058792, 20.92286546477, 20.8639777659847, 20.9012211366426, 
20.9034308680551, 20.9085654549109, 20.8662463645066), `Value 2` = c(21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 
22L, 22L, 22L, 22L, 22L, 22L)), row.names = c(NA, -72L), class = c("tbl_df", 
"tbl", "data.frame"))

The code I've usde to get to the following picture:

  geom_line(aes(y = `Value 1`, colour = "Value 1"))   
  geom_line(aes(y = `Value 2`, colour = "Value 2"))   
  scale_linetype_manual("", values =c( "Value 1" = "dashed", "Value 2" = "twodash"))   
  scale_colour_manual("", values = c("Value 1" = "red", "Value 2" = "black")) 
  ggpubr::theme_pubr()   
  theme(legend.position =c(.5,.9), legend.direction='horizontal')  
  theme(panel.background = element_rect(colour = "black", size=0.5))   
  scale_x_date(date_breaks = "1 years", date_labels = "%Y", limits = as.Date(c("2016-01-01","2021-12-01")), expand=c(0,0))   
  labs(x = "Date", y = "Value")

Example

CodePudding user response:

You should assign the variables in linetype in the aes and then assign those to scale_linetype_manual. You can use the following code:

df %>%
  ggplot(aes(x = Date))  
  geom_line(aes(y = `Value 1`, colour = "Value 1", linetype = "Value 1"))   
  geom_line(aes(y = `Value 2`, colour = "Value 2", linetype = "Value 2"))   
  scale_linetype_manual("", values =c("Value 1" = "dashed", "Value 2" = "twodash"))   
  scale_colour_manual("", values = c("Value 1" = "red", "Value 2" = "black")) 
  ggpubr::theme_pubr()   
  theme(legend.position =c(.5,.9), legend.direction='horizontal')  
  theme(panel.background = element_rect(colour = "black", size=0.5))   
  scale_x_date(date_breaks = "1 years", date_labels = "%Y", limits = as.Date(c("2016-01-01","2021-12-01")), expand=c(0,0))   
  labs(x = "Date", y = "Value")

Output:

enter image description here

  • Related