如何在不更改 y 轴刻度的情况下将矩形添加到具有记录 y 轴的 ggplot 图形

问题描述 投票:0回答:0

我正在尝试使用 annotate() 或 geom_rect() 将矩形添加到具有记录的 y 轴的 ggplot 图。该矩形以两个 x 轴值(日期)为界,需要跨越整个 y 轴。但是,每当我添加矩形时,它都会改变 y 轴的比例。

例如,这是我添加矩形之前的图形:

这是我使用 annotate() 添加矩形后的图表:

这是我的数据的一个子集:

mydata <- structure(list(SampleDate = structure(c(17721, 17715, 17450, 
17721, 17504, 17725, 17714, 17721, 17684, 17721, 17684, 17442, 
17777, 17848, 17631, 17721, 17714, 17716, 17884, 17726, 17684, 
17728, 17752, 17754, 17677, 17752, 17746, 17777, 17714, 17751, 
17717, 17504, 17714, 17508, 17730, 17754, 17652, 17749, 17661, 
17611, 17767, 17744, 17745, 17717, 17871, 17683, 17714, 17903, 
17684, 17638, 17623, 17715, 17567, 17506, 17744, 17786, 17683, 
17849, 17725, 17591, 17722, 17722, 17918, 17849, 17554, 17652, 
17871, 17714, 17726, 17716, 17623, 17722, 17749, 17851, 17751, 
17744, 17619, 17744, 17562, 17502, 17695, 17730, 17560, 17752, 
17687, 17744, 17623, 17731, 17746, 17532, 17744, 17848, 17729, 
17745, 17784, 17777, 17715, 17725, 17721, 17669, 17666, 17766, 
17724, 17730, 17848, 17871, 17871, 17628, 17714, 17852, 17759, 
17744, 17627, 17591, 17751, 17725, 17848, 17746, 17666, 17715, 
17501, 17521, 17730, 17745, 17684, 17848, 17770, 17715, 17918, 
17721, 17487, 17807, 17567, 17759, 17721, 17460, 17905, 17715, 
17444, 17654, 17666, 17721, 17722, 17477, 17899, 17624, 17744, 
17714, 17459, 17444, 17744, 17487, 17690, 17442, 17871, 17745, 
17906, 17745, 17721, 17686, 17786, 17613, 17754, 17654, 17487, 
17509, 17781, 17661, 17504, 17884, 17532, 17721, 17849, 17768, 
17744, 17654, 17844, 17693, 17645, 17442, 17631, 17507, 17899, 
17723, 17766, 17628, 17520, 17442, 17729, 17523, 17744, 17715, 
17687, 17721, 17623, 17744, 17744, 17511, 17784, 17521, 17568, 
17542, 17477, 17817, 17767, 17883, 17683, 17714, 17728, 17631, 
17739, 17687, 17460, 17806, 17784, 17624, 17442, 17852, 17899, 
17884, 17847, 17715, 17850, 17494, 17652, 17784, 17918, 17714, 
17848, 17744, 17817, 17613, 17781, 17904, 17544, 17752, 17850, 
17760, 17631, 17918, 17533, 17624, 17731, 17725, 17624, 17624, 
17623, 17721, 17781, 17618, 17609, 17725, 17626, 17669, 17624, 
17774, 17717, 17649, 17729, 17624, 17576, 17884, 17591, 17627, 
17563, 17567, 17807, 17714, 17519, 17871, 17686, 17592, 17745, 
17571, 17749, 17637, 17871, 17714, 17509, 17624, 17781, 17871, 
17726, 17631, 17715, 17684, 17508, 17728, 17844, 17739, 17558, 
17725, 17714, 17627, 17618, 17442, 17683, 17808, 17668, 17745, 
17478, 17687, 17871, 17502, 17542, 17619, 17849, 17745, 17578, 
17715, 17681, 17687, 17661, 17567, 17521, 17504, 17666, 17494, 
17631, 17653, 17684, 17628, 17686, 17638, 17722, 17653, 17652, 
17623, 17502, 17652, 17613, 17624, 17493, 17687, 17502, 17744, 
17669, 17654, 17546, 17675, 17661, 17627, 17745, 17904, 17652, 
17614, 17918, 17661, 17623, 17641, 17442, 17627, 17847, 17786, 
17726, 17744, 17501, 17652, 17726, 17848, 17725, 17725, 17900, 
17809, 17844, 17572, 17730, 17731, 17442, 17666, 17686, 17632, 
17850, 17780, 17792, 17623, 17617, 17884, 17865, 17501, 17809, 
17609, 17619, 17871, 17918, 17661, 17631, 17557, 17627, 17560, 
17850, 17889, 17805, 17813, 17689, 17628, 17591, 17591, 17624, 
17686, 17871, 17842, 17448, 17623, 17619, 17505, 17809, 17533, 
17543, 17751, 17502, 17593, 17520, 17623, 17501, 17505, 17581, 
17654, 17627, 17806, 17809, 17624, 17591, 17553, 17654, 17844, 
17791, 17661, 17683, 17714, 17744, 17899, 17624, 17553, 17722, 
17495, 17666, 17502, 17445, 17507, 17619, 17632, 17744, 17745, 
17809, 17687, 17627, 17445, 17721, 17623, 17568, 17532, 17547, 
17674, 17918, 17721, 17683, 17871, 17780, 17591, 17616, 17757, 
17777, 17624, 17693, 17652, 17521, 17668, 17653, 17844, 17693, 
17553, 17918, 17730, 17519, 17745, 17683, 17532, 17805, 17759, 
17623, 17717, 17806, 17847, 17724, 17640, 17786, 17901, 17627, 
17715, 17868, 17686, 17687, 17623, 17767, 17631, 17715, 17723, 
17624, 17632, 17780, 17686, 17844, 17684, 17683, 17899, 17638, 
17591, 17627, 17744, 17532, 17662, 17611, 17742, 17533, 17661, 
17593, 17613, 17808, 17563, 17847, 17744, 17684, 17627, 17626, 
17842, 17750, 17623, 17502, 17786, 17623, 17508, 17645, 17842, 
17653, 17624, 17844, 17662, 17729, 17619, 17543, 17623, 17627, 
17641, 17624, 17623, 17631, 17918, 17675, 17746, 17641, 17638, 
17626, 17623, 17641, 17495, 17805, 17780, 17499, 17872, 17684, 
17640, 17725, 17499, 17805, 17533, 17499, 17532, 17623, 17623, 
17730, 17568, 17809, 17591, 17627, 17847, 17445, 17654, 17523, 
17686, 17632, 17655, 17714, 17627, 17652, 17623, 17669, 17809, 
17572, 17627, 17683, 17904, 17869, 17843, 17502, 17613, 17593, 
17667, 17669, 17444), class = "Date"), RelativeAbundance = c(1.28, 
3.64, 0.46, 0.21, 2.91, 1.71, 1.01, 1.87, 0.3, 1.44, 2.45, 2.9, 
1.5, 0.16, 7.78, 0.17, 0.54, 2.28, 1.86, 0.74, 5.03, 1.26, 1.63, 
1.16, 0.63, 1.38, 1.12, 1.14, 0.82, 1.45, 1.9, 1.39, 0.85, 1.18, 
0.86, 1.68, 0.83, 1.31, 0.94, 0.9, 2.16, 0.62, 0.19, 0.55, 0.07, 
0.99, 1.67, 0.68, 0.95, 0.26, 0.61, 1.08, 0.77, 1.26, 0.71, 0.94, 
0.53, 0.4, 0.13, 0.94, 0.9, 2.46, 0.51, 0.35, 0.47, 1.23, 1, 
0.89, 2.14, 0.79, 0.57, 1.53, 1.13, 0.36, 0.29, 0.93, 0.3, 0.48, 
0.36, 0.76, 1.13, 0.44, 1.44, 0.17, 0.2, 0.12, 1.26, 0.38, 0.36, 
0.46, 0.47, 0.22, 0.27, 0.16, 0.53, 0.8, 0.28, 0.7, 1.12, 0.96, 
0.4, 0.16, 0.22, 0.24, 0.75, 0.18, 0.15, 0.91, 0.35, 0.07, 1.36, 
0.21, 0.57, 0.28, 0.38, 0.15, 0.12, 0.04, 0.24, 0.27, 0.42, 0.27, 
1.04, 0.5, 0.34, 0.06, 0.53, 0.43, 0.23, 0.77, 0.2, 0.56, 0.22, 
1.37, 0.21, 0.22, 0.87, 0.19, 0.15, 0.2, 0.23, 0.13, 0.18, 0.16, 
0.18, 0.27, 0.34, 0.7, 0.29, 0.23, 0.44, 0.24, 0.14, 0.24, 0.12, 
0.16, 0.27, 0.18, 0.12, 0.17, 0.16, 0.22, 0.12, 0.12, 0.22, 0.46, 
0.13, 0.71, 0.15, 0.13, 0.13, 1.68, 0.09, 0.54, 0.22, 0.15, 0.16, 
0.24, 0.21, 0.04, 0.54, 0.08, 0.14, 1.46, 0.31, 0.19, 0.06, 0.12, 
0.31, 0.27, 0.13, 0.4, 0.14, 0.12, 0.31, 0.33, 0.08, 0.16, 0.15, 
0.15, 0.16, 0.37, 0.36, 0.1, 0.24, 0.17, 0.07, 0.11, 0.21, 0.25, 
0.13, 0.09, 0.11, 0.06, 0.15, 0.25, 0.14, 0.03, 0.21, 0.11, 0.08, 
0.38, 0.05, 1.14, 0.09, 0.72, 0.24, 0.15, 0.05, 0.14, 0.15, 0.14, 
0.2, 0.29, 0.09, 0.11, 0.1, 0.67, 0.12, 0.39, 0.1, 0.09, 0.33, 
0.46, 0.21, 0.09, 0.1, 0.36, 0.09, 0.03, 0.18, 0.27, 0.32, 0.29, 
0.17, 0.12, 0.28, 0.09, 0.4, 0.14, 0.45, 0.07, 0.23, 0.51, 0.19, 
0.11, 0.24, 0.06, 0.05, 0.15, 0.07, 0.25, 0.22, 0.15, 0.12, 0.21, 
0.24, 0.49, 0.25, 0.39, 0.17, 0.1, 0.1, 0.59, 0.12, 0.24, 0.18, 
0.07, 0.12, 0.38, 0.15, 0.08, 0.08, 0.09, 0.21, 0.1, 0.06, 0.07, 
0.09, 0.14, 0.08, 0.05, 0.22, 0.07, 0.22, 0.44, 0.3, 0.11, 0.17, 
0.13, 0.14, 0.09, 0.28, 0.43, 0.3, 0.16, 0.09, 0.09, 0.1, 0.09, 
0.15, 0.22, 0.26, 0.13, 0.06, 0.11, 0.06, 0.05, 0.18, 0.05, 0.17, 
0.17, 0.16, 0.2, 0.14, 0.06, 0.19, 0.05, 0.07, 0.07, 0.26, 0.25, 
0.07, 0.13, 0.04, 0.15, 0.09, 0.13, 0.05, 0.12, 0.1, 0.06, 0.08, 
0.08, 0.21, 0.04, 0.11, 0.17, 0.05, 0.03, 0.2, 0.1, 0.07, 0.04, 
0.12, 0.12, 0.17, 0.15, 0.07, 0.07, 0.03, 0.17, 0.02, 0.09, 0.1, 
0.07, 0.06, 0.04, 0.07, 0.09, 0.09, 0.19, 0.07, 0.31, 0.05, 0.14, 
0.27, 0.09, 0.06, 0.06, 0.1, 0.11, 0.05, 0.05, 0.2, 0.15, 0.12, 
0.06, 0.08, 0.04, 0.13, 0.05, 0.41, 0.12, 0.08, 0.06, 0.11, 0.06, 
0.08, 0.68, 0.05, 0.2, 0.09, 0.08, 0.09, 0.05, 0.05, 0.03, 0.13, 
0.12, 0.05, 0.13, 0.11, 0.19, 0.03, 0.06, 0.05, 0.08, 0.12, 0.04, 
0.12, 0.15, 0.06, 0.03, 0.08, 0.07, 0.06, 0.15, 0.07, 0.02, 0.07, 
0.08, 0.19, 0.07, 0.09, 0.03, 0.1, 0.04, 0.12, 0.03, 0.04, 0.03, 
0.05, 0.09, 0.03, 0.04, 0.06, 0.09, 0.06, 0.04, 0.09, 0.07, 0.74, 
0.1, 0.08, 0.08, 0.03, 0.03, 0.13, 0.05, 0.08, 0.03, 0.05, 0.18, 
0.01, 0.04, 0.09, 0.08, 0.11, 0.21, 0.03, 0.13, 0.14, 0.05, 0.07, 
0.07, 0.1, 0.25, 0.08, 0.07, 0.1, 0.02, 0.01, 0.03, 0.19, 0.07, 
0.11, 0.07, 0.04, 0.08, 0.07, 0.02, 0.05, 0.03, 0.09, 0.02, 0.05, 
0.03, 0.06, 0.09, 0.16, 0.07, 0.06, 0.08, 0.25, 0.14, 0.15, 0.08, 
0.07, 0.08, 0.01, 0.13, 0.05, 0.08, 0.07, 0.15, 0.07, 0.04, 0.02, 
0.08, 0.02, 0.05, 0.03, 0.11, 0.02, 0.06, 0.1, 0.05, 0.26, 0.11, 
0.04, 0.13, 0.07, 0.07, 0.06, 0.04, 0.12, 0.12, 0.02, 0.04, 0.04, 
0.03, 0.04, 0.05, 0.06, 0.1, 0.1, 0.04, 0.06, 0.07, 0.06, 0.04, 
0.08, 0.06, 0.11, 0.04, 0.25, 0.07, 0.03, 0.05, 0.04, 0.12, 0.04, 
0.04, 0.1, 0.02, 0.11, 0.06, 0.03, 0.01, 0.09, 0.07, 0.27, 0.13, 
0.07, 0.03, 0.04, 0.03, 0.03, 0.04, 0.04, 0.03, 0.13, 0.03, 0.1, 
0.05, 0.06, 0.04, 0.05, 0.1), Taxa = c("A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "B", "A", "A", "A", "A", "A", "A", 
"A", "B", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"B", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "A", 
"A", "A", "A", "A", "B", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "B", "A", "A", "B", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "B", "A", "A", "B", "A", "A", "A", "A", "A", "A", "A", 
"B", "A", "B", "A", "A", "A", "A", "B", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "B", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "B", "A", "A", "A", "A", "A", 
"A", "B", "A", "A", "A", "B", "B", "A", "B", "A", "A", "A", "A", 
"A", "A", "A", "B", "A", "A", "A", "A", "A", "A", "A", "A", "B", 
"B", "A", "B", "B", "A", "A", "B", "A", "B", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "B", "A", "A", "A", "A", "A", "A", "B", "A", "B", "A", "A", 
"A", "A", "A", "B", "A", "A", "A", "A", "A", "B", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "B", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "B", "B", "A", "A", "A", "A", "A", "A", 
"A", "A", "B", "A", "A", "A", "A", "B", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "B", "A", "B", "B", "A", "A", "A", "B", "A", 
"A", "B", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "B", "B", "A", "A", "A", "A", "A", "B", 
"A", "A", "B", "A", "A", "B", "B", "A", "B", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "B", "A", "B", "B", "A", 
"A", "A", "A", "B", "A", "B", "A", "B", "A", "A", "A", "A", "A", 
"A", "B", "B", "A", "A", "A", "B", "A", "A", "A", "B", "A", "A", 
"B", "A", "A", "A", "A", "A", "A", "B", "A", "A", "B", "A", "A", 
"A", "A", "A", "A", "B", "A", "A", "A", "A", "A", "A", "B", "A", 
"A", "B", "B", "A", "A", "A", "A", "A", "B", "A", "B", "B", "B", 
"B", "B", "A", "A", "A", "A", "A", "A", "A", "A")), row.names = c(NA, 
600L), class = "data.frame")

我用子集数据创建修改后的基础图的代码:

p <- ggplot(mydata, aes(x=SampleDate, y=RelativeAbundance+0.001, colour = Taxa)) + 
  scale_y_log10() +
  geom_smooth(method="gam",se=F)

要添加矩形,我尝试了多种不同的方法,但都会影响 y 轴的比例或不起作用,因为它们会创建零:

  1. 使用 y min 和 y max 的未记录值与
    annotate()
p + annotate("rect", xmin = as.Date("2018-02-01"), xmax = as.Date("2018-06-01"), ymin = 0.001, ymax = 7.8, alpha = .2, fill = "#fc8d59") 
  1. 使用 annotation_custom,ymin 为 0,ymax 为无穷大(这不会运行,因为我认为它会创建零)
p + annotation_custom("rect", xmin = as.Date("2017-10-01"), xmax = as.Date("2018-02-01"), ymin = 0, ymax = Inf)
  1. 使用
    geom_rect
    并设置 ymin ymax 值
y_min <- (min(mydata$RelativeAbundance)) + .0001
y_max <- max(mydata$RelativeAbundance)

p + geom_rect(aes(xmin = as.Date("2017-10-01"), xmax = as.Date("2018-02-01"), ymin = y_min, ymax = y_max))

感谢您提供的任何帮助。

ggplot2 scale annotate geom
© www.soinside.com 2019 - 2024. All rights reserved.