仅在图形的一个区域上更改背景颜色 [英] Change background color on just one region of the graph
问题描述
我只想更改图形的一部分的背景色.有可能吗?
I would like to change the background color for just a portion of a graph. Is that possible?
例如,使用下面的图表,我可能想表明我对重量在2-4吨之间的汽车特别感兴趣,因此希望使用粉红色背景突出显示该区域.
For example, using the following chart I might want to indicate that I am particularly interested in cars that have a weight between 2 and 4 tons and thus would want to highlight this region using a pink background.
更具体地说,我想覆盖一个透明的粉红色矩形,该矩形在x轴上从2延伸到4,并覆盖整个y轴区域.
More specifically I'd like to overlay a transparent pink rectangle which stretches from 2 to 4 on the x axis and covers the entire y axis region.
我该如何编码?
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point()
推荐答案
使用geom_rect (文档).您只需要在对geom_rect的es调用中指定尺寸即可.要更改透明度,请在下面的代码中更改alpha.
The rectangle is easy using geom_rect (documentation). You just need to specify it's dimensions in the aes call to geom_rect. To change the transparency alter alpha in the code below.
require(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point() +
geom_rect(aes(xmin = 2, xmax = 4, ymin = -Inf, ymax = Inf),
fill = "pink", alpha = 0.03))
会产生类似您所追求的东西吗?
Does that produce something like what you're after?
这篇关于仅在图形的一个区域上更改背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!