I’m trying to plot a geom_ribbon
object that is disconnected from other geom_ribbon
objects. This perfectly works for the RStudio internal display and saving png files, but the geom_ribbon
objects are connected with very thin lines when using a PDF.
This is the plot:
ggplot(data = b,
aes(x = time,
y = fitted)) +
geom_ribbon(aes(ymin = cf_lwr, ymax = cf_upr, fill = "red",
group = .data$name),na.rm = FALSE) +
facet_wrap(~id) -> test
test
But when I now try to save it as a PDF, I get very thin lines connecting the ribbon items (zoomed in) in the resulting PDF:
I have tried playing around with setting na.rm = TRUE
and na.rm = FALSE
and have also removed the group
argument, but to no avail.
Similarly, I’ve tried to use geom_polygon()
but there I’m struggling as well:
b %>%
select(-fitted) %>%
pivot_longer(-c(time,name,id), names_to = "type", values_to = "value") %>%
ggplot(aes(x = time,
y = value,
group = name)) +
geom_polygon(fill = "red") +
facet_wrap(~id)
Any ideas? Thanks for your help!!
Data:
b <- structure(list(time = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18),
fitted = c(9.82399931488347,
9.83890547549997, 9.96835827682772, 10.0015563715667, 10.0195513075349,
10.0263620267794, 10.0281679132439, 10.0402091409343, 10.0258148087331,
9.98957105642021, 9.98927729804064, 9.98360672736602, 9.93408407605121,
10.0055066926455, 9.99807441172596, 10.0103947595883, 10.0307184111839,
10.0341349325091, 10.037783284132, 12.0002972837409, 12.0165016808134,
12.0186694303305, 11.9094084266171, 11.9169384086629, 11.9142773254232,
11.9152467227305, 11.9196906398965, 11.9019674827009, 11.8605280898209,
11.8717838389661, 11.873563656638, 11.911828082864, 11.9084315759843,
11.9097428414989, 11.9254137346769, 11.9445590081518, 11.9455459935057,
11.9416905527665),
cf_lwr = c(9.82399931488347, 9.83890547549997,
9.90572544776573, 9.93892354250466, 9.95691847847286, 9.9637291977174,
9.9655350841819, 9.97757631187226, 10.0258148087331, 9.98957105642021,
9.98927729804064, 9.98360672736602, 9.93408407605121, 9.96308021671443,
9.95564793579493, 9.96796828365724, 9.98829193525292, 9.99170845657809,
9.99535680820095, 12.0002972837409, 12.0165016808134, 12.0186694303305,
12.0893867811635, 12.0969167632093, 12.0942556799696, 12.0952250772769,
12.0996689944429, 12.0819458372473, 11.8605280898209, 11.8717838389661,
11.873563656638, 11.8641739911823, 11.8607774843026, 11.8620887498172,
11.8777596429952, 11.8969049164701, 11.897891901824, 11.9416905527665
),
cf_upr = c(9.82399931488347, 9.83890547549997, 9.79995528174166,
9.83315337648059, 9.85114831244879, 9.85795903169334, 9.85976491815783,
9.8718061458482, 10.0258148087331, 9.98957105642021, 9.98927729804064,
9.98360672736602, 9.93408407605121, 9.89219474605896, 9.88476246513946,
9.89708281300177, 9.91740646459744, 9.92082298592262, 9.92447133754548,
12.0002972837409, 12.0165016808134, 12.0186694303305, 11.9953346595386,
12.0028646415844, 12.0002035583447, 12.001172955652, 12.005616872818,
11.9878937156224, 11.8605280898209, 11.8717838389661, 11.873563656638,
11.7806377756053, 11.7772412687256, 11.7785525342402, 11.7942234274183,
11.8133687008931, 11.8143556862471, 11.9416905527665),
name = c(NA,
"groupB2002", "groupB2002", "groupB2002", "groupB2002", "groupB2002",
"groupB2002", "groupB2002", NA, NA, NA, NA, "groupB2013", "groupB2013",
"groupB2013", "groupB2013", "groupB2013", "groupB2013", "groupB2013",
NA, NA, "groupB2003", "groupB2003", "groupB2003", "groupB2003",
"groupB2003", "groupB2003", "groupB2003", NA, NA, "groupB2012",
"groupB2012", "groupB2012", "groupB2012", "groupB2012", "groupB2012",
"groupB2012", NA),
id = c("A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "B")),
row.names = c(NA, -38L), class = "data.frame")
Session Info:
R version 4.3.0 (2023-04-21 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22631)
Matrix products: default
locale:
[1] LC_COLLATE=English_Austria.utf8 LC_CTYPE=English_Austria.utf8 LC_MONETARY=English_Austria.utf8 LC_NUMERIC=C
[5] LC_TIME=English_Austria.utf8
time zone: Europe/Vienna
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] forcats_1.0.0 stringr_1.5.0 purrr_1.0.2 readr_2.1.4 tidyr_1.3.0 tibble_3.2.1 tidyverse_2.0.0 getspanel_0.2.0 padr_0.6.2
[10] lubridate_1.9.2 dplyr_1.1.2 ggplot2_3.4.4 testthat_3.1.8
loaded via a namespace (and not attached):
[1] Rdpack_2.5 remotes_2.4.2 sandwich_3.0-2 rlang_1.1.1 magrittr_2.0.3 compiler_4.3.0 systemfonts_1.0.4 callr_3.7.3
[9] vctrs_0.6.3 profvis_0.3.8 pkgconfig_2.0.3 shape_1.4.6 crayon_1.5.2 fastmap_1.1.1 ellipsis_0.3.2 labeling_0.4.2
[17] utf8_1.2.3 promises_1.2.0.1 tzdb_0.4.0 sessioninfo_1.2.2 ps_1.7.5 miscTools_0.6-28 ragg_1.2.5 glmnet_4.1-8
[25] cachem_1.0.8 collapse_1.9.6 later_1.3.1 parallel_4.3.0 prettyunits_1.1.1 R6_2.5.1 stringi_1.7.12 pkgload_1.3.4
[33] brio_1.1.3 lmtest_0.9-40 Rcpp_1.0.10 iterators_1.0.14 usethis_2.2.0 zoo_1.8-12 timechange_0.2.0 httpuv_1.6.11
[41] Matrix_1.6-5 splines_4.3.0 tidyselect_1.2.0 rstudioapi_0.14 maxLik_1.5-2 codetools_0.2-19 miniUI_0.1.1.1 processx_3.8.1
[49] pkgbuild_1.4.0 lattice_0.21-8 shiny_1.7.4 withr_2.5.0 gets_0.37 fastDummies_1.6.3 desc_1.4.2 survival_3.5-5
[57] urlchecker_1.0.1 pillar_1.9.0 rsconnect_1.1.0 foreach_1.5.2 generics_0.1.3 rprojroot_2.0.3 hms_1.1.3 munsell_0.5.0
[65] scales_1.2.1 xtable_1.8-4 glue_1.6.2 tools_4.3.0 data.table_1.14.8 fs_1.6.2 mvtnorm_1.2-2 grid_4.3.0
[73] rbibutils_2.2.15 bdsmatrix_1.3-6 devtools_2.4.5 colorspace_2.1-0 nlme_3.1-162 Formula_1.2-5 cli_3.6.1 textshaping_0.3.6
[81] plm_2.6-3 fansi_1.0.4 gtable_0.3.3 digest_0.6.31 htmlwidgets_1.6.2 farver_2.1.1 memoise_2.0.1 htmltools_0.5.5
[89] lifecycle_1.0.3 mime_0.12 MASS_7.3-58.4