我正在使用 ggplot2 包在 R 中制作一个简单的条形图。我不想使用默认的灰色,而是想将背景分为五个区域,每个区域都有不同的(但同样低调的)颜色。我该怎么做呢?
更具体地说,我希望五个颜色区域的范围为 0-25、25-45、45-65、65-85 和 85-100,其中颜色代表比青铜色、青铜色、银色、金色和铂金更差的颜色。分别。也非常欢迎对配色方案的建议。
下面是一个帮助您入门的示例:
#Fake data
dat <- data.frame(x = 1:100, y = cumsum(rnorm(100)))
#Breaks for background rectangles
rects <- data.frame(xstart = seq(0,80,20), xend = seq(20,100,20), col = letters[1:5])
#As Baptiste points out, the order of the geom's matters, so putting your data as last will
#make sure that it is plotted "on top" of the background rectangles. Updated code, but
#did not update the JPEG...I think you'll get the point.
ggplot() +
geom_rect(data = rects, aes(xmin = xstart, xmax = xend, ymin = -Inf, ymax = Inf, fill = col), alpha = 0.4) +
geom_line(data = dat, aes(x,y))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)