Home > Net >  adding geom_point to a geom_rect chart in ggplot2
adding geom_point to a geom_rect chart in ggplot2

Time:11-28

I have the following dataframe and chart created using ggplot2

library(tidyverse)

df <- structure(list(Key = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 
6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 
10L, 10L, 11L, 11L, 11L, 11L), levels = c("GC23", "GC24", "GC25", 
"GC26", "GC27", "GC28", "GC30", "GC35", "GC45", "GC48", "GC50"
), class = "factor"), Quartile = structure(c(1L, 2L, 3L, 4L, 
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), levels = c("1", "2", "3", "4"
), class = "factor"), min = c(0.800000000000001, 70.2, 102.9, 
124.4, -108.1, -0.200000000000067, 63.2, 124.4, -70.9999999999999, 
5.29999999999999, 67.0999999999999, 144.3, -52.1000000000001, 
16, 37.1, 51.6999999999999, -19.2, 75.0999999999999, 92.7999999999999, 
161.8, -45.5, -3.80000000000003, 12.7000000000001, 31.3000000000001, 
3.30000000000013, 107.9, 120.2, 143.4, 29.7000000000001, 102.1, 
138.3, 172.3, 83.9, 183.6, 216.6, 240.3, 202.1, 258.6, 290.9, 
321.9, 107.5, 201.1, 247.1, 290.1), max = c(70.1, 102.8, 124.3, 
342.6, -0.200000000000067, 63.1, 124.4, 190.2, 4.79999999999992, 
67.0999999999999, 144.2, 209.7, 16, 37.1, 51.3999999999999, 131.7, 
75, 92.7000000000001, 161.3, 250.3, -4.70000000000006, 12.5999999999999, 
30.1, 62.9, 107.8, 119.8, 143.2, 192.3, 102, 138.2, 172.3, 258, 
183.5, 216.6, 240.3, 349.4, 258.5, 290.9, 321.9, 374.5, 201.1, 
247, 289.6, 400.9)), row.names = c(NA, -44L), class = c("tbl_df", 
"tbl", "data.frame"))

p <- ggplot()  
  geom_rect(data = df, aes(xmin = (as.numeric(Key) -0.25), xmax = (as.numeric(Key)   0.25), ymin = min, ymax = max, fill = Quartile))  
  scale_x_continuous(breaks = seq(from = min(as.numeric(df$Key)), to = max(as.numeric(df$Key))), labels = unique(df$Key))  
  theme_bw()  
  labs(y = 'Basis Points')  
  scale_fill_manual(values = c('red','orange', 'yellow', 'green'))

p

enter image description here

I have an additional dataframe with point values for each of the variables in Key that I would like to add as a geom_point

df2 <- structure(list(Key = structure(1:11, levels = c("GC23", "GC24", 
"GC25", "GC26", "GC27", "GC28", "GC30", "GC35", "GC45", "GC48", 
"GC50"), class = "factor"), max = c(131.8, -47.2, -12.2, 36.1000000000001, 
67.3, 27.4999999999999, 119, 133, 235.3, 287.6, 303.3), Quartile = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = "Today", class = "factor")), row.names = c(NA, 
-11L), class = c("tbl_df", "tbl", "data.frame"))

p   geom_point(data = df2, aes(x = Key, y = max, col = 'black'))

However that gives me the following error.

Error: Discrete value supplied to continuous scale

Any way around this? Ultimately, I'm trying to replicate this chart

enter image description here

CodePudding user response:

You need to also apply as.numeric() to Key when using df2

p   geom_point(data = df2, aes(x = as.numeric(Key), y = max), col = 'black')

enter image description here

  • Related