2016-10-07 58 views
2

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

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

回答

2

您当前的绘制数据集的每一行一个矩形。你重叠的矩形越多,他们就越暗,这就是为什么较长的数据集有一个较深的矩形。使用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) 
+0

在调用geom_point()后使用annotate()效果很好。 – user2860703