r - ggplot2:来自 alpha 的颜色不一致

标签 r plot ggplot2 alpha

我正在制作几个具有不同 x 轴限制的图,我想通过添加一个灰色框来突出显示感兴趣的区域。即使我在 ggplot2 中使用具有相同 alpha 值的相同 geom_rect() 命令,我得到的结果具有非常不同的灰色。我看过herehere但到目前为止还没有弄清楚如何使这些盒子具有相同的透明度。下面是一个可重现的例子(带有假数据)和它产生的数字。注意灰色矩形的不同颜色。我希望所有地 block 的灰色都相同。

Data<-structure(list(X = c(34L, 27L, 28L, 47L, 26L, 3L, 13L, 31L, 39L, 
16L, 45L, 5L, 49L, 17L, 29L, 43L, 1L, 35L, 41L, 10L, 48L, 24L, 
12L, 11L, 30L, 40L, 8L, 4L, 20L, 25L, 50L, 22L, 9L, 21L, 18L, 
7L, 15L, 44L, 6L, 36L, 46L, 33L, 2L, 37L, 23L, 14L, 42L, 38L, 
19L, 32L, 34L, 27L, 28L, 47L, 26L, 3L, 13L, 31L, 39L, 16L, 45L, 
5L, 49L, 17L, 29L, 43L, 1L, 35L, 41L, 10L, 48L, 24L, 12L, 11L, 
30L, 40L, 8L, 4L, 20L, 25L, 50L, 22L, 9L, 21L, 18L, 7L, 15L, 
44L, 6L, 36L, 46L, 33L, 2L, 37L, 23L, 14L, 42L, 38L, 19L, 32L
), Y = c(130L, 146L, 58L, 110L, 117L, 135L, 133L, 108L, 97L, 
61L, 71L, 64L, 103L, 142L, 125L, 104L, 100L, 147L, 111L, 78L, 
56L, 145L, 62L, 69L, 70L, 116L, 137L, 79L, 150L, 94L, 91L, 81L, 
65L, 118L, 129L, 83L, 98L, 84L, 85L, 148L, 93L, 73L, 59L, 87L, 
134L, 88L, 136L, 90L, 140L, 55L, 89L, 115L, 123L, 51L, 132L, 
126L, 66L, 80L, 60L, 120L, 109L, 76L, 74L, 57L, 149L, 121L, 138L, 
128L, 114L, 127L, 68L, 107L, 67L, 112L, 144L, 119L, 53L, 52L, 
54L, 96L, 131L, 106L, 113L, 72L, 95L, 63L, 92L, 86L, 75L, 105L, 
82L, 101L, 139L, 143L, 122L, 77L, 99L, 141L, 124L, 102L), B = structure(c(2L, 
2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 
1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 
2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 
2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
1L, 1L, 1L), class = "factor", .Label = c("no", "yes"))), .Names = c("X", 
"Y", "B"), row.names = c(NA, -100L), class = "data.frame")

Data2<-structure(list(variable = c(2.49676547444708, 0.67359598601097, 
0.674751772966082, 0.0317590441796792, 0.485143583939748, 1.08231639527806, 
0.0732344181040914, 1.62357048819912, 0.146833215667032, 0.823157103468943, 
0.240761579418538, 1.37540376416553), DOY_mid_month = c(15, 46, 
75, 106, 136, 167, 197, 228, 259, 289, 320, 350)), .Names = c("variable", 
"DOY_mid_month"), row.names = c(NA, -12L), class = "data.frame")

test<-ggplot(data=Data) +
  geom_rect(aes(xmin=5, xmax=30, ymin=1, ymax=40), alpha = 0.02) +
  geom_point(aes(x = X, y = X, colour= B), data =Data, size=2) +
  theme_bw()

test2 <-ggplot(data=Data2) +
  geom_rect(aes(xmin=5, xmax=30, ymin=-Inf, ymax=Inf), alpha = 0.02) +
  geom_point(aes(x = DOY_mid_month, y = variable), color="black", size=4) +
  scale_x_continuous("Day of Year", limits = c(0, 366)) + # Use this to add back X-axis label for the bottom plot in panel
  scale_y_continuous(expression(paste("Variable", sep=""))) +
  theme_bw()

绘制第一个例子的结果:

test

第二个例子的绘图结果:

test2

最佳答案

您当前正在为数据集的每一行绘制一个矩形。重叠的矩形越多,它们变得越暗,这就是为什么较长的数据集具有较暗的矩形。使用 annotate 而不是 geom_rect 绘制单个矩形。

annotate(geom = "rect", xmin=5, xmax=30, ymin=-Inf, ymax=Inf, alpha = 0.2)

如果您想坚持使用 geom_rect,您可以为该层提供一行 data.frame,这样每个矩形只绘制一次。这里我使用了一个假数据集,尽管您也可以将矩形限制放在 data.frame 中。

geom_rect(data = data.frame(fake = 1), 
            aes(xmin = 5, xmax= 30, ymin = -Inf, ymax = Inf), alpha = 0.2)

关于r - ggplot2:来自 alpha 的颜色不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39925947/

相关文章:

python - 更改函数中的变量以在同一轴上创建多个绘图

r - ggplot2_Error : geom_point requires the following missing aesthetics: y

r - 无法在 R 中加载 'Caret'

r - ricles 包中的knitr::include_graphics 不起作用

python - 使用 python 和 mayavi 创建 3D 流图

r - ggplot2:如何产生更小的点

r - ggplot 图层首先放置时会破坏顺序

r - 不同 facet_grids 的不同颜色

r - 如何将 R 数据框写入 Snowflake 数据库表

r - 使用跨越数年的大型数据集计算 R 中的月平均值