我使用 ggplot2 绘制了以下数据
library(ggplot2)
Data = data.frame('Date' = as.Date("2028-01-01") + 1:7,
'y' = c(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0),
'yLow' = c(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0) - 2.2,
'yUp' = c(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0) + 2.2,
'Pt1' = c(-1, 1.0, NA, NA, 4.0, 5.0, NA) + 3.2,
'Pt2' = c(NA, 1.0, 2, NA, 4.0, 5.0, NA) + 4.2)
Data
Variable_Pt = c('Pt1', 'Pt2')
Variable_Pt_Col = c('Red', 'Blue')
Plot = ggplot(Data, aes(x = Date, y = y)) +
geom_line(size = 1, alpha = 0.8, color = "black")
for (i in 1:2) {
print(Data[, Variable_Pt[i]])
Plot = Plot + geom_point(aes(x = Date, y = Data[, Variable_Pt[i]]), color = Variable_Pt_Col[i])
}
Plot
然而令人惊讶的是,我没有看到变量 Pt1
的点数。附件是我所见的快照。
你能帮我理解我的代码有什么不正确吗?
以下是我的会话信息:
> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
locale:
[1] C/UTF-8/C/C/C/C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_3.3.3
loaded via a namespace (and not attached):
[1] digest_0.6.27 withr_2.4.1 dplyr_1.0.4 crayon_1.4.1
[5] grid_4.0.3 R6_2.5.0 lifecycle_1.0.0 gtable_0.3.0
[9] magrittr_2.0.1 scales_1.1.1 pillar_1.4.7 rlang_0.4.10
[13] farver_2.1.0 generics_0.1.0 vctrs_0.3.6 ellipsis_0.3.1
[17] labeling_0.4.2 glue_1.4.2 purrr_0.3.4 munsell_0.5.0
[21] compiler_4.0.3 pkgconfig_2.0.3 colorspace_2.0-1 tidyselect_1.1.0
[25] tibble_3.0.6