如何防止ggplot在每个方面重复多次重复某些元素
如何防止GGPLOT在每个方面重复多次重复所有地理?
想象一下,我想创建一个图块,以跨多个方面显示沿X轴的温度。为了增加效果,我创建了两个Geom_Rects(),它们显示温度在冰点上方还是低于温度。
在“ A”组中,GEOM_RECT绘制一次。 在“ B”组中,GEOM_RECT绘制了两次。 在“ C”组中,GEOM_RECT绘制了三遍。
因为GEOM_RECT重复不同的时间,所以小平面的alpha值变为不同(请注意从上到下的差异)。
我该如何避免这种情况?
library(tidyverse)
set.seed(1)
df <- tibble(
facet_var = c("A", "B", "B", "C", "C", "C"),
celcius = rnorm(n = 6),
y = as.factor(c(1, 1, 2, 1, 2, 3)))
df %>%
ggplot(aes(x = celcius, y = y))+
geom_point()+
geom_rect(xmin = -2.5, xmax=0.0,
ymax=3.5 , ymin=0,
fill = "blue", alpha =0.2)+
geom_rect(xmin = 0, xmax=2,
ymax=3.5, ymin=0,
fill = "red", alpha =0.2)+
facet_grid(rows = vars(facet_var), scales = "free_y", space = "free_y")
在2022-06-06-30创建的 reprex软件包(v2.0.1)
How can I prevent ggplot from repeating all geoms multiple times in each facet?
Imagine I want to create a plot that shows the temperature along the x-axis across multiple facets. For added effect, I create two geom_rects() that show if the temperature is above or below freezing.
In group "A" geom_rect is drawn once.
In group "B" geom_rect is drawn twice.
In group "C" geom_rect is drawn three times.
Because geom_rect is repeated different times the alpha value of the facets becomes different (please note the difference from top to bottom).
How can I avoid this?
library(tidyverse)
set.seed(1)
df <- tibble(
facet_var = c("A", "B", "B", "C", "C", "C"),
celcius = rnorm(n = 6),
y = as.factor(c(1, 1, 2, 1, 2, 3)))
df %>%
ggplot(aes(x = celcius, y = y))+
geom_point()+
geom_rect(xmin = -2.5, xmax=0.0,
ymax=3.5 , ymin=0,
fill = "blue", alpha =0.2)+
geom_rect(xmin = 0, xmax=2,
ymax=3.5, ymin=0,
fill = "red", alpha =0.2)+
facet_grid(rows = vars(facet_var), scales = "free_y", space = "free_y")
Created on 2022-06-30 by the reprex package (v2.0.1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
Annotate
与GEOMRECT
:(SET
ymin
to-Inf
和ymax
> toinf
保留“ free_y”
间隔。)由
You could use
annotate
with geomrect
:(Set
ymin
to-Inf
andymax
toInf
to retain the"free_y"
spacing.)Created on 2022-06-30 by the reprex package (v2.0.1)