简体   繁体   English

问题:Plot alpha 值随 Y 轴缩放/ggplot 方面的观察数量

[英]Issue: Plot alpha values scaling with Y-Axis/number of observations in ggplot facets

I haven't found anyone else with this issue.我还没有找到其他人遇到这个问题。 Here is my plot: facet plot这是我的 plot: facet plot

Why are there different alpha values for each facet?为什么每个方面都有不同的 alpha 值?

As you can see, the alpha value of the geom_rect() elements seems to scale with the y-axis or number of observations, maybe because I have set these to "free_y" in the facet_wrap() argument.如您所见,geom_rect() 元素的 alpha 值似乎与 y 轴或观察数成比例,可能是因为我在 facet_wrap() 参数中将它们设置为“free_y”。 How can I prevent this from happening?我怎样才能防止这种情况发生?

Here is my code:这是我的代码:

plot_data %>%
ggplot(aes(Date, n)) +
geom_rect(data= plot_data, inherit.aes = FALSE,
            aes(xmin=current_date - lubridate::weeks(1), xmax=current_date, ymin=-Inf, ymax=+Inf), 
            fill='pink', alpha=0.2) +
geom_col() + 
facet_wrap(~Type, scales = "free_y") +
xlab("Date") +
ylab("Count") +
theme_bw() +
scale_y_continuous(breaks = integer_breaks()) +
scale_alpha_manual(values = 0.2) +
theme(axis.text.x=element_text(angle=90, hjust=1))

Cheers!干杯!

TL;DR - It seems this is probably due to overplotting. TL;DR - 看来这可能是由于过度绘制。 You have 5 rect geoms drawn in the facet, but probably more than 5 observations in your dataset.您在 facet 中绘制了 5 个 rect geoms,但数据集中可能有超过 5 个观察值。 The fix is to summarize your data and associate geom_rect() to plot with the summarized dataset.解决方法是汇总您的数据并将geom_rect()关联到 plot 与汇总数据集。

Since OP did not provide an example dataset, we can only guess at the reason, but likely what's happening here is due to overplotting.由于 OP 没有提供示例数据集,我们只能猜测原因,但这里发生的事情很可能是由于过度绘制。 geom_rect() behaves like all other geoms, which is to say that ggplot2 will draw or add to any geom layer with every observation (row) in the original dataset. geom_rect()的行为与所有其他 geom 一样,也就是说ggplot2将使用原始数据集中的每个观察(行)绘制或添加到任何 geom 层。 If the geoms are drawn across facets and overlap in position , then you'll get overplotting.如果 geom 跨面绘制并在 position 中重叠,那么您将过度绘制。 You can notice that this is happening based on:您可以注意到这是基于以下情况发生的:

  • Different alpha appearing on each facet, even though it should be constant based on the code, and每个面上出现不同的 alpha,即使根据代码它应该是常量,并且
  • The fact that in order to get the rectangles to look like "light red", OP had to use pink color and an alpha value of 0.2... which shouldn't look like that if there was only one rect drawn.事实上,为了让矩形看起来像“淡红色”,OP 必须使用pink和 0.2 的 alpha 值......如果只绘制一个矩形,它不应该看起来像那样。

Representative Example of the Issue问题的代表性例子

Here's an example that showcases the problem and how you can fix it using mtcars :这是一个展示问题以及如何使用mtcars修复它的示例:

library(ggplot2)
df <- mtcars
p <- ggplot(df, aes(disp, mpg)) + geom_point() +
  facet_wrap(~cyl) +
  theme_bw()
p + geom_rect(
  aes(xmin=200, xmax=300, ymin=-Inf, ymax=Inf),
  alpha=0.01, fill='red')

在此处输入图像描述

Like OP's case, we expect all rectangles to be the same alpha value, but they are not.与 OP 的情况一样,我们希望所有矩形都具有相同的 alpha 值,但事实并非如此。 Also, note the alpha value is ridiculously low ( 0.01 ) for the color you see there.另外,请注意您在此处看到的颜色的 alpha 值低得离谱 ( 0.01 )。 What's going on should be more obvious if we check number of observations in mtcars that falls within each facet:如果我们检查落在每个方面的mtcars中的观察数量,发生的事情应该会更加明显:

> library(dplyr)
> mtcars %>% group_by(cyl) %>% tally()
# A tibble: 3 x 2
    cyl     n
  <dbl> <int>
1     4    11
2     6     7
3     8    14

There's a lower number of observations where cyl==6 and cyl==4 has lower observations than cyl==8 . cyl==6cyl==4的观测值低于cyl==8的观测值较少。 This corresponds precisely to the alpha values we see for the geoms in the plot, so this is what's going on.这恰好对应于我们在 plot 中看到的 geom 的 alpha 值,所以这就是正在发生的事情。 For each observation, a rectangle is drawn over the same position and so there are 7 rectangles drawn in the middle facet, 14 on the right facet, and 11 on the left facet.对于每个观察,在相同的 position 上绘制了一个矩形,因此在中间面绘制了 7 个矩形,在右侧面绘制了 14 个,在左侧面绘制了 11 个。

Fixing the Issue: Summarize the Data解决问题:汇总数据

To fix the issue, you should summarize your data and use the summarized dataset for plotting the rectangles.要解决此问题,您应该汇总数据并使用汇总数据集绘制矩形。

summary_df <- df %>%
  group_by(cyl) %>%
  summarize(mean_d = mean(disp))

p + geom_rect(
  data = summary_df,
  aes(x=1, y=1, xmin=mean_d-50, xmax=mean_d+50, ymin=-Inf, ymax=Inf),
  alpha=0.2, fill='red')

在此处输入图像描述

Since summary_df has only 3 observations (one for each group of cyl ), the rectangles are drawn correctly and now alpha=0.2 with fill="red" gives the expected result.由于summary_df只有 3 个观察值(每组cyl一个),矩形被正确绘制,现在alpha=0.2fill="red"给出了预期的结果。 One thing to note here is that we still have to define x and y in the aes() .这里要注意的一件事是我们仍然必须在aes()中定义xy I set them both to 1 because although geom_rect() doesn't use them, ggplot2 still expects to find them in the dataset summary_df because we stated that they are assigned to that plot globally up in ggplot(df, aes(x=..., y=...)) .我将它们都设置为1 ,因为虽然geom_rect()不使用它们, ggplot2仍然希望在数据集summary_df中找到它们,因为我们声明它们在ggplot(df, aes(x=..., y=...)) The fix is to either move the aes() declaration into geom_point() or just assign both to be constant values in geom_rect() .解决方法是将aes()声明移至geom_point()或将两者都分配为 geom_rect geom_rect() ) 中的常量值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM