如何防止ggplot在每个方面重复多次重复某些元素

发布于 2025-02-12 01:09:47 字数 1062 浏览 1 评论 0原文

如何防止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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花间憩 2025-02-19 01:09:47

您可以使用Annotate与GEOM RECT :(

SET ymin to -Infymax > to inf保留“ free_y”间隔。)

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(celcius, y)) +
  geom_point() +
  annotate("rect", xmin = -2.5, xmax = 0.0, ymin = -Inf, ymax = Inf, fill = "blue", alpha = 0.2) +
  annotate("rect", xmin = 0, xmax = 2, ymin = -Inf, ymax = Inf, fill = "red", alpha = 0.2) +
  facet_grid(rows = vars(facet_var), scales = "free_y", space = "free_y")

“

You could use annotate with geom rect:

(Set ymin to -Inf and ymax to Inf to retain the "free_y" spacing.)

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(celcius, y)) +
  geom_point() +
  annotate("rect", xmin = -2.5, xmax = 0.0, ymin = -Inf, ymax = Inf, fill = "blue", alpha = 0.2) +
  annotate("rect", xmin = 0, xmax = 2, ymin = -Inf, ymax = Inf, 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文