我使用ggplot2
创建了以下 Dataframe 和图表
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
我有一个额外的 Dataframe ,其中包含Key
中每个变量的点值,我想将其添加为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'))
然而,这给了我以下错误。
Error: Discrete value supplied to continuous scale
有什么办法吗?最终,我试图复制这个图表
1条答案
按热度按时间uujelgoq1#
使用
df2
时,还需要将as.numeric()
应用于Key