I have seen this question asked by a number of people with different issues, but I have not found a response that fixes my problem.
I am having a problem with the following error when executing any of the grouped_* commands in ggstatsplot.
Error in unique.default(x, nmax = nmax) : unique() applies only to vectors
I have tried reinstalling R, reinstalling RStudio, and reinstalling all packages. I have also brought this up to the developer of ggstatsplot, but he said he cannot reproduce my error. I expect ggstatsplot to produce grouped plots based on the grouping variable. Instead, I receive the error above.
Here is a reprex with my sessionInfo and the traceback of the problem I am encountering.
If anyone has any idea what is going on here I would greatly appreciate it. Thank you.
Reprex with sessionInfo:
library(ggstatsplot)
#> You can cite this package as:
#> Patil, I. (2021). Visualizations with statistical details: The 'ggstatsplot' approach.
#> Journal of Open Source Software, 6(61), 3167, doi:10.21105/joss.03167
# Error in ggstatsplot grouping function
grouped_ggscatterstats(data = movies_long,
x = budget,
y = rating,
grouping.var = genre)
#> Error in unique.default(x, nmax = nmax): unique() applies only to vectors
Created on 2023-02-07 with reprex v2.0.2
Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.0.3 (2020-10-10)
#> os Ubuntu 20.04.5 LTS
#> system x86_64, linux-gnu
#> ui X11
#> language en_CA:en
#> collate en_CA.UTF-8
#> ctype en_CA.UTF-8
#> tz America/Edmonton
#> date 2023-02-07
#> pandoc 2.19.2 # /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date (UTC) lib source
#> BayesFactor 0.9.12-4.4 2022-07-05 [1] CRAN (R 4.0.3)
#> bayestestR 0.13.0 2022-09-18 [1] CRAN (R 4.0.3)
#> cli 3.6.0 2023-01-09 [1] CRAN (R 4.0.3)
#> coda 0.19-4 2020-09-30 [1] CRAN (R 4.0.3)
#> codetools 0.2-18 2020-11-04 [4] CRAN (R 4.0.3)
#> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.0.3)
#> correlation 0.8.3 2022-10-09 [1] CRAN (R 4.0.3)
#> curl 5.0.0 2023-01-12 [1] CRAN (R 4.0.3)
#> datawizard 0.6.5 2022-12-14 [1] CRAN (R 4.0.3)
#> digest 0.6.31 2022-12-11 [1] CRAN (R 4.0.3)
#> dplyr 1.1.0 2023-01-29 [1] CRAN (R 4.0.3)
#> effectsize 0.8.3 2023-01-28 [1] CRAN (R 4.0.3)
#> emmeans 1.7.3 2022-03-27 [1] CRAN (R 4.0.3)
#> estimability 1.4.1 2022-08-05 [1] CRAN (R 4.0.3)
#> evaluate 0.20 2023-01-17 [1] CRAN (R 4.0.3)
#> fansi 1.0.4 2023-01-22 [1] CRAN (R 4.0.3)
#> farver 2.1.1 2022-07-06 [1] CRAN (R 4.0.3)
#> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.0.3)
#> fs 1.6.1 2023-02-06 [1] CRAN (R 4.0.3)
#> generics 0.1.3 2022-07-05 [1] CRAN (R 4.0.3)
#> ggplot2 3.4.0 2022-11-04 [1] CRAN (R 4.0.3)
#> ggside 0.2.2 2022-12-04 [1] CRAN (R 4.0.3)
#> ggstatsplot * 0.10.0 2022-11-27 [1] CRAN (R 4.0.3)
#> glue 1.6.2 2022-02-24 [1] CRAN (R 4.0.3)
#> gtable 0.3.1 2022-09-01 [1] CRAN (R 4.0.3)
#> highr 0.10 2022-12-22 [1] CRAN (R 4.0.3)
#> htmltools 0.5.4 2022-12-07 [1] CRAN (R 4.0.3)
#> httr 1.4.4 2022-08-17 [1] CRAN (R 4.0.3)
#> insight 0.19.0 2023-01-30 [1] CRAN (R 4.0.3)
#> knitr 1.42 2023-01-25 [1] CRAN (R 4.0.3)
#> labeling 0.4.2 2020-10-20 [1] CRAN (R 4.0.3)
#> lattice 0.20-45 2021-09-22 [4] CRAN (R 4.1.1)
#> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.0.3)
#> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.0.3)
#> MASS 7.3-58.1 2022-08-03 [4] CRAN (R 4.2.1)
#> Matrix 1.5-3 2022-11-11 [1] CRAN (R 4.0.3)
#> MatrixModels 0.5-1 2022-09-11 [1] CRAN (R 4.0.3)
#> mgcv 1.8-41 2022-10-21 [4] CRAN (R 4.2.1)
#> mime 0.12 2021-09-28 [1] CRAN (R 4.0.3)
#> multcomp 1.4-20 2022-08-07 [1] CRAN (R 4.0.3)
#> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.0.2)
#> mvtnorm 1.1-3 2021-10-08 [1] CRAN (R 4.0.3)
#> nlme 3.1-149 2020-08-23 [4] CRAN (R 4.0.2)
#> paletteer 1.5.0 2022-10-19 [1] CRAN (R 4.0.3)
#> parameters 0.20.2 2023-01-27 [1] CRAN (R 4.0.3)
#> patchwork 1.1.2 2022-08-19 [1] CRAN (R 4.0.3)
#> pbapply 1.7-0 2023-01-13 [1] CRAN (R 4.0.3)
#> performance 0.10.2 2023-01-12 [1] CRAN (R 4.0.3)
#> pillar 1.8.1 2022-08-19 [1] CRAN (R 4.0.3)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.0.2)
#> purrr 1.0.1 2023-01-10 [1] CRAN (R 4.0.3)
#> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.0.3)
#> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.0.3)
#> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.0.3)
#> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.0.3)
#> R6 2.5.1 2021-08-19 [1] CRAN (R 4.0.3)
#> Rcpp 1.0.10 2023-01-22 [1] CRAN (R 4.0.3)
#> rematch2 2.1.2 2020-05-01 [1] CRAN (R 4.0.2)
#> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.0.3)
#> rlang 1.0.6 2022-09-24 [1] CRAN (R 4.0.3)
#> rmarkdown 2.20 2023-01-19 [1] CRAN (R 4.0.3)
#> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.0.3)
#> sandwich 3.0-2 2022-06-15 [1] CRAN (R 4.0.3)
#> scales 1.2.1 2022-08-20 [1] CRAN (R 4.0.3)
#> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.0.3)
#> statsExpressions 1.4.0 2023-01-14 [1] CRAN (R 4.0.3)
#> stringi 1.7.12 2023-01-11 [1] CRAN (R 4.0.3)
#> stringr 1.5.0 2022-12-02 [1] CRAN (R 4.0.3)
#> styler 1.9.0 2023-01-15 [1] CRAN (R 4.0.3)
#> survival 3.4-0 2022-08-09 [4] CRAN (R 4.2.1)
#> TH.data 1.1-1 2022-04-26 [1] CRAN (R 4.0.3)
#> tibble 3.1.8 2022-07-22 [1] CRAN (R 4.0.3)
#> tidyr 1.3.0 2023-01-24 [1] CRAN (R 4.0.3)
#> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.0.3)
#> utf8 1.2.3 2023-01-31 [1] CRAN (R 4.0.3)
#> vctrs 0.5.2 2023-01-23 [1] CRAN (R 4.0.3)
#> withr 2.5.0 2022-03-03 [1] CRAN (R 4.0.3)
#> xfun 0.37 2023-01-31 [1] CRAN (R 4.0.3)
#> xml2 1.3.3 2021-11-30 [1] CRAN (R 4.0.3)
#> xtable 1.8-4 2019-04-21 [1] CRAN (R 4.0.2)
#> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.0.3)
#> zeallot 0.1.0 2018-01-28 [1] CRAN (R 4.0.3)
#> zoo 1.8-11 2022-09-17 [1] CRAN (R 4.0.3)
#>
#> [1] /media/user/4B/R/x86_64-pc-linux-gnu-library/4.0
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library
#>
#> ──────────────────────────────────────────────────────────────────────────────
Traceback:
13: unique.default(x, nmax = nmax)
12: unique(x, nmax = nmax)
11: factor(x)
10: as.factor(f)
9: split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...)
8: split(x = seq_len(nrow(x)), f = f, drop = drop, ...)
7: lapply(split(x = seq_len(nrow(x)), f = f, drop = drop, ...),
function(ind) x[ind, , drop = FALSE])
6: split.data.frame(., f = new_formula(NULL, enquo(grouping.var)),
drop = TRUE)
5: split(., f = new_formula(NULL, enquo(grouping.var)), drop = TRUE)
4: data %>% split(f = new_formula(NULL, enquo(grouping.var)), drop = TRUE)
3: .grouped_list(., {
{
grouping.var
}
})
2: data %<>% .grouped_list({
{
grouping.var
}
})
1: grouped_ggscatterstats(movies_long, budget, rating, grouping.var = genre)
Created on 2023-02-08 with reprex v2.0.2
Yes, I think this was the issue #Phil and #Behnam Hedayat. I tried reinstalling RStudio, I tried updating RStudio, I tried different versions of ggstatsplot to no avail. Finally, I decided to try to upgrade my version of Ubuntu, which did not work due to other issues, but at least it did seem to catch some new updates to the r-base and a few other libraries I could see, I think that solved the issue because the grouped_* commands are now working as they should.
During this upgrade process R updated to R v4.2.2 as #Phil and #Behnam Hedayat suggested. Thank you both for your help.
For anyone that might have this problem in the future, the commands I ran in terminal were the following and I found them here:
sudo apt update
sudo apt upgrade && sudo apt dist-upgrade
sudo update-manager -d
I have been using Claus Wilke's great ggtext package for markdown annotations for a while now, and until now it's worked perfectly. In the course of using ggtext to make multiple text annotations (mix of bold face and regular text on multiple lines), either with the ggtext::geom_richtext() or ggtext::geom_textbox(), I found the more geom_richtext()s I added increased the rendering time to the point where it would just churn on end (I gave up after 45 minutes the last time).
Of course, creating and saving the ggplot object with all of the geom_richtext() and element_markdown()s took little time. But when I tried to view the plot or use ggsave to save as a png/jpeg it again churned forever (again, I gave up after 30 minutes).
I've included a sample reprex below.
My questions for ggtext and ggplot2 power users:
Is there an upper limit to the number of ggtext objects you can use in a ggplot2 plot? I'm not well-versed in grid and the underlying mechanics of ggplot.
Are there alternatives to ggtext to mix font weights in annotations in ggplot2? I already tried the plotmath route after seeing this from #aosmith on SO: html - Embolden substring of object passed through geom_text() - Stack Overflow. The problem? atop() only works on one line, not multiple lines of text.
FWIW, the full non-reprex version of my code (and the viz I'm trying to recreate in ggplot2) are at https://github.com/kpivert/wsj/tree/main/01_R/06_cpi_again.
Any recommendations you may have would be greatly appreciated.
Please let me know if you need further clarification or if the reprex doesn't work.
Thanks for your time and consideration.
Kurtis
require(ggthemes)
#> Loading required package: ggthemes
require(ggtext)
#> Loading required package: ggtext
require(tictoc)
#> Loading required package: tictoc
require(tidyverse)
#> Loading required package: tidyverse
df <- data.frame(
DATE = seq(as.Date("1913-01-01"), as.Date("2020-12-01"), "month")
) %>%
mutate(
var = rnorm(n = nrow(.), mean = 0, sd = 0.5)
)
# Years for X Axis Labels
yr_labs <- seq(as.Date("1915-01-01"), as.Date("2020-01-01"), by = "5 years") %>%
str_sub(start = 1, end = 4)
yr_labs <- case_when(
yr_labs == "1915" ~ "1915",
yr_labs == "2020" ~ "2020",
TRUE ~ str_c("'", str_sub(yr_labs, 3, 4))
)
tic()
ggplot(
df,
aes(
x = DATE,
y = var
)
) +
geom_line(
lwd = 0.2,
color = "blue"
) +
scale_x_continuous(
breaks = seq(as.Date("1915-01-01"), as.Date("2020-01-01"), by = "5 years"),
labels = yr_labs
) +
theme_tufte() +
theme(
panel.grid.major.y = element_line(size = .1),
axis.ticks.y = element_blank(),
axis.text.y = element_text(
vjust = - 0.8,
margin = margin(l = 20, r = -20)
),
plot.title = element_text(size = 16),
plot.subtitle = element_text(size = 10),
plot.caption = element_text(hjust = 0)
) +
labs(
x = "",
y = "",
caption = "Source: Randomness",
title = "Reprex Title",
subtitle = "Reprex Subtitle"
) +
# Annotations ----
# 1. WW I ----
geom_segment(
aes(
x = as.Date("1914-09-01"),
y = 1.25,
xend = as.Date("1918-11-01"),
yend = 1.25
),
lwd = 1
) +
geom_segment(
aes(
x = as.Date("1916-08-01"),
y = 1.25,
xend = as.Date("1916-08-01"),
yend = 1.28
),
linetype = "dotted"
) +
geom_richtext(
x = as.Date("1916-01-01"),
y = 1.28,
label = "<b>1914-18</b><br>World<br>War I",
fill = NA,
label.color = NA
)
toc()
#> 20.668 sec elapsed
Created on 2021-12-07 by the reprex package (v2.0.1)
Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.1.0 (2021-05-18)
#> os macOS Big Sur 10.16
#> system x86_64, darwin17.0
#> ui X11
#> language (EN)
#> collate en_US.UTF-8
#> ctype en_US.UTF-8
#> tz America/New_York
#> date 2021-12-07
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date lib source
#> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.1.0)
#> backports 1.2.1 2020-12-09 [1] CRAN (R 4.1.0)
#> broom 0.7.9 2021-07-27 [1] CRAN (R 4.1.0)
#> cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.1.0)
#> cli 3.0.1 2021-07-17 [1] CRAN (R 4.1.0)
#> colorspace 2.0-2 2021-06-24 [1] CRAN (R 4.1.0)
#> crayon 1.4.1 2021-02-08 [1] CRAN (R 4.1.0)
#> curl 4.3.2 2021-06-23 [1] CRAN (R 4.1.0)
#> DBI 1.1.1 2021-01-15 [1] CRAN (R 4.1.0)
#> dbplyr 2.1.1 2021-04-06 [1] CRAN (R 4.1.0)
#> digest 0.6.28 2021-09-23 [1] CRAN (R 4.1.0)
#> dplyr * 1.0.7 2021-06-18 [1] CRAN (R 4.1.0)
#> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.0)
#> evaluate 0.14 2019-05-28 [1] CRAN (R 4.1.0)
#> fansi 0.5.0 2021-05-25 [1] CRAN (R 4.1.0)
#> farver 2.1.0 2021-02-28 [1] CRAN (R 4.1.0)
#> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.0)
#> forcats * 0.5.1 2021-01-27 [1] CRAN (R 4.1.0)
#> fs 1.5.0 2020-07-31 [1] CRAN (R 4.1.0)
#> generics 0.1.0 2020-10-31 [1] CRAN (R 4.1.0)
#> ggplot2 * 3.3.5 2021-06-25 [1] CRAN (R 4.1.0)
#> ggtext * 0.1.1 2020-12-17 [1] CRAN (R 4.1.0)
#> ggthemes * 4.2.4 2021-01-20 [1] CRAN (R 4.1.0)
#> glue 1.4.2 2020-08-27 [1] CRAN (R 4.1.0)
#> gridtext 0.1.4 2020-12-10 [1] CRAN (R 4.1.0)
#> gtable 0.3.0 2019-03-25 [1] CRAN (R 4.1.0)
#> haven 2.4.3 2021-08-04 [1] CRAN (R 4.1.0)
#> highr 0.9 2021-04-16 [1] CRAN (R 4.1.0)
#> hms 1.1.0 2021-05-17 [1] CRAN (R 4.1.0)
#> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.0)
#> httr 1.4.2 2020-07-20 [1] CRAN (R 4.1.0)
#> jsonlite 1.7.2 2020-12-09 [1] CRAN (R 4.1.0)
#> knitr 1.36 2021-09-29 [1] CRAN (R 4.1.0)
#> labeling 0.4.2 2020-10-20 [1] CRAN (R 4.1.0)
#> lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.1.0)
#> lubridate 1.7.10 2021-02-26 [1] CRAN (R 4.1.0)
#> magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.1.0)
#> markdown 1.1 2019-08-07 [1] CRAN (R 4.1.0)
#> mime 0.12 2021-09-28 [1] CRAN (R 4.1.0)
#> modelr 0.1.8 2020-05-19 [1] CRAN (R 4.1.0)
#> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.1.0)
#> pillar 1.6.3 2021-09-26 [1] CRAN (R 4.1.0)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.0)
#> purrr * 0.3.4 2020-04-17 [1] CRAN (R 4.1.0)
#> R6 2.5.1 2021-08-19 [1] CRAN (R 4.1.0)
#> Rcpp 1.0.7 2021-07-07 [1] CRAN (R 4.1.0)
#> readr * 2.0.1 2021-08-10 [1] CRAN (R 4.1.0)
#> readxl 1.3.1 2019-03-13 [1] CRAN (R 4.1.0)
#> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.1.0)
#> rlang 0.4.11 2021-04-30 [1] CRAN (R 4.1.0)
#> rmarkdown 2.11 2021-09-14 [1] CRAN (R 4.1.0)
#> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.1.0)
#> rvest 1.0.1 2021-07-26 [1] CRAN (R 4.1.0)
#> scales 1.1.1 2020-05-11 [1] CRAN (R 4.1.0)
#> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.1.0)
#> stringi 1.7.5 2021-10-04 [1] CRAN (R 4.1.0)
#> stringr * 1.4.0 2019-02-10 [1] CRAN (R 4.1.0)
#> styler 1.5.1 2021-07-13 [1] CRAN (R 4.1.0)
#> tibble * 3.1.5 2021-09-30 [1] CRAN (R 4.1.0)
#> tictoc * 1.0.1 2021-04-19 [1] CRAN (R 4.1.0)
#> tidyr * 1.1.3 2021-03-03 [1] CRAN (R 4.1.0)
#> tidyselect 1.1.1 2021-04-30 [1] CRAN (R 4.1.0)
#> tidyverse * 1.3.1 2021-04-15 [1] CRAN (R 4.1.0)
#> tzdb 0.1.2 2021-07-20 [1] CRAN (R 4.1.0)
#> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.1.0)
#> vctrs 0.3.8 2021-04-29 [1] CRAN (R 4.1.0)
#> withr 2.4.2 2021-04-18 [1] CRAN (R 4.1.0)
#> xfun 0.26 2021-09-14 [1] CRAN (R 4.1.0)
#> xml2 1.3.2 2020-04-23 [1] CRAN (R 4.1.0)
#> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.1.0)
#>
#> [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library
For the record, Jon Spring correctly noted the issue, which was the aesthetic mapping problem in the geom_segment() and geom_richtext() calls. In addition to his solution using annotate(), you can also pass a dataframe and use inherit.aes = FALSE so the text or segment isn't overplotted the same number of times as there are rows in the dataset. For example:
geom_richtext(
inherit.aes = FALSE,
data = tibble(
x = as.Date("1916-01-01"),
y = 1.28,
label = "<b>1914-18</b><br>World<br>War I"),
aes(
x = x,
y = y,
label = label),
fill = NA,
label.color = NA
)
Kara Woo's great presentation at the 2021 rstudio::global conference addressed this very issue: https://www.rstudio.com/resources/rstudioglobal-2021/always-look-on-the-bright-side-of-plots/.
I am trying to plot tectonic plate boundaries on the world map. However, the tectonic plate's polygons seem to stretch across the world map (looking like they're at the opposite ends of the world, when they're actually side to side as the globe is round).
Is there a way I can split the plate where it wraps around?
Data:
library(tibble)
# polygon of tectonic plates
plate <- tibble(lat = c(-42.059, -41.910, -41.756, -41.657, -41.500, -41.188,
-40.807, -40.424, -40.087, -39.685, -39.230, -38.889,
-38.538, -38.205, -37.476, -36.793, -36.179, -35.542,
-35.022, -34.706, -34.485, -34.241, -33.846, -33.580,
-33.191, -32.696, -32.203, -31.957, -31.793, -31.452,
-31.037, -30.678, -30.322, -29.881, -29.469, -29.065,
-28.697, -28.264, -27.784, -27.318, -26.857, -26.437,
-26.025, -25.730, -25.426, -25.142, -24.836, -24.472,
-24.070, -23.750, -23.750, -23.853, -23.952, -24.029,
-24.105, -24.105, -24.242, -24.720, -25.197, -25.976,
-26.767, -27.377, -27.985, -28.693, -29.498, -29.792,
-30.355, -30.997, -31.333, -31.751, -32.347, -33.019,
-33.602, -34.246, -34.786, -35.161, -35.917, -36.461,
-37.102, -37.485, -37.885, -38.287, -38.674, -38.762,
-39.230, -40.046, -40.311, -40.598, -40.791, -40.979,
-41.157, -41.566, -41.757, -42.059, -42.059),
lon = c(175.503, 176.081 , 176.673, 177.123, 177.607,
178.015, 178.284, 178.566, 178.792, 178.950,
179.125, 179.215, 179.366, 179.569, 179.838,
-179.811, -179.371, -179.044, -178.641, -178.539,
-178.413, -178.294, -178.208, -177.981, -177.810,
-177.666, -177.649, -177.538, -177.301, -177.108,
-176.919, -176.690, -176.547, -176.339, -176.153,
-176.078, -175.995, -175.885, -175.785, -175.545,
-175.400, -175.423, -175.415, -175.382, -175.297,
-175.261, -175.229, -175.209, -175.102, -174.985,
-174.985, -175.691, -176.356, -176.887, -177.419,
-177.419, -177.448, -177.552, -177.657, -177.793,
-178.017, -178.248, -178.482, -178.713, -178.966,
-179.074, -179.226, -179.393, -179.515, -179.740,
179.980, 179.657, 179.345, 178.996, 178.681,
178.403, 177.890, 177.507, 177.049, 176.770,
176.509, 176.241, 175.995, 175.932, 175.609,
176.074, 175.868, 175.537, 175.324, 175.012,
174.632, 174.763, 174.945, 175.503, 175.503)
)
Code:
library(ggplot2)
library(dplyr)
# world data to map world map
world <- map_data("world")
# world map with tectonic plate in green
world %>%
ggplot() +
geom_map(map = world,
aes(x = long, y = lat,
map_id = region)) +
geom_polygon(data = plate,
aes(x = lon,
y = lat),
fill = NA,
colour = "dark green")
#> Warning: Ignoring unknown aesthetics: x, y
Created on 2020-08-26 by the reprex package (v0.3.0)
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.0.2 (2020-06-22)
#> os macOS Catalina 10.15.6
#> system x86_64, darwin17.0
#> ui X11
#> language (EN)
#> collate en_AU.UTF-8
#> ctype en_AU.UTF-8
#> tz Australia/Melbourne
#> date 2020-08-26
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date lib source
#> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.2)
#> backports 1.1.8 2020-06-17 [1] CRAN (R 4.0.2)
#> blob 1.2.1 2020-01-20 [1] CRAN (R 4.0.2)
#> broom 0.7.0 2020-07-09 [1] CRAN (R 4.0.2)
#> callr 3.4.3 2020-03-28 [1] CRAN (R 4.0.2)
#> cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.0.2)
#> cli 2.0.2 2020-02-28 [1] CRAN (R 4.0.2)
#> colorspace 1.4-1 2019-03-18 [1] CRAN (R 4.0.2)
#> crayon 1.3.4 2017-09-16 [1] CRAN (R 4.0.2)
#> curl 4.3 2019-12-02 [1] CRAN (R 4.0.1)
#> DBI 1.1.0 2019-12-15 [1] CRAN (R 4.0.2)
#> dbplyr 1.4.4 2020-05-27 [1] CRAN (R 4.0.2)
#> desc 1.2.0 2018-05-01 [1] CRAN (R 4.0.2)
#> devtools 2.3.1 2020-07-21 [1] CRAN (R 4.0.2)
#> digest 0.6.25 2020-02-23 [1] CRAN (R 4.0.2)
#> dplyr * 1.0.1 2020-07-31 [1] CRAN (R 4.0.2)
#> ellipsis 0.3.1 2020-05-15 [1] CRAN (R 4.0.2)
#> evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.1)
#> fansi 0.4.1 2020-01-08 [1] CRAN (R 4.0.2)
#> farver 2.0.3 2020-01-16 [1] CRAN (R 4.0.2)
#> forcats * 0.5.0 2020-03-01 [1] CRAN (R 4.0.2)
#> fs 1.5.0 2020-07-31 [1] CRAN (R 4.0.2)
#> generics 0.0.2 2018-11-29 [1] CRAN (R 4.0.2)
#> ggplot2 * 3.3.2 2020-06-19 [1] CRAN (R 4.0.2)
#> glue 1.4.1 2020-05-13 [1] CRAN (R 4.0.2)
#> gtable 0.3.0 2019-03-25 [1] CRAN (R 4.0.2)
#> haven 2.3.1 2020-06-01 [1] CRAN (R 4.0.2)
#> highr 0.8 2019-03-20 [1] CRAN (R 4.0.2)
#> hms 0.5.3 2020-01-08 [1] CRAN (R 4.0.2)
#> htmltools 0.5.0 2020-06-16 [1] CRAN (R 4.0.2)
#> httr 1.4.2 2020-07-20 [1] CRAN (R 4.0.2)
#> jsonlite 1.7.0 2020-06-25 [1] CRAN (R 4.0.2)
#> knitr 1.29 2020-06-23 [1] CRAN (R 4.0.2)
#> labeling 0.3 2014-08-23 [1] CRAN (R 4.0.2)
#> lifecycle 0.2.0 2020-03-06 [1] CRAN (R 4.0.2)
#> lubridate 1.7.9 2020-06-08 [1] CRAN (R 4.0.2)
#> magrittr 1.5 2014-11-22 [1] CRAN (R 4.0.2)
#> maps 3.3.0 2018-04-03 [1] CRAN (R 4.0.2)
#> memoise 1.1.0 2017-04-21 [1] CRAN (R 4.0.2)
#> mime 0.9 2020-02-04 [1] CRAN (R 4.0.2)
#> modelr 0.1.8 2020-05-19 [1] CRAN (R 4.0.2)
#> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.0.2)
#> pillar 1.4.6 2020-07-10 [1] CRAN (R 4.0.2)
#> pkgbuild 1.1.0 2020-07-13 [1] CRAN (R 4.0.2)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.0.2)
#> pkgload 1.1.0 2020-05-29 [1] CRAN (R 4.0.2)
#> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.2)
#> processx 3.4.3 2020-07-05 [1] CRAN (R 4.0.2)
#> ps 1.3.3 2020-05-08 [1] CRAN (R 4.0.2)
#> purrr * 0.3.4 2020-04-17 [1] CRAN (R 4.0.2)
#> R6 2.4.1 2019-11-12 [1] CRAN (R 4.0.2)
#> Rcpp 1.0.5 2020-07-06 [1] CRAN (R 4.0.2)
#> readr * 1.3.1 2018-12-21 [1] CRAN (R 4.0.2)
#> readxl 1.3.1 2019-03-13 [1] CRAN (R 4.0.2)
#> remotes 2.2.0 2020-07-21 [1] CRAN (R 4.0.2)
#> reprex 0.3.0 2019-05-16 [1] CRAN (R 4.0.2)
#> rlang 0.4.7 2020-07-09 [1] CRAN (R 4.0.2)
#> rmarkdown 2.3 2020-06-18 [1] CRAN (R 4.0.2)
#> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 4.0.2)
#> rvest 0.3.6 2020-07-25 [1] CRAN (R 4.0.2)
#> scales 1.1.1 2020-05-11 [1] CRAN (R 4.0.2)
#> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.2)
#> stringi 1.4.6 2020-02-17 [1] CRAN (R 4.0.2)
#> stringr * 1.4.0 2019-02-10 [1] CRAN (R 4.0.2)
#> testthat 2.3.2 2020-03-02 [1] CRAN (R 4.0.2)
#> tibble * 3.0.3 2020-07-10 [1] CRAN (R 4.0.2)
#> tidyr * 1.1.1 2020-07-31 [1] CRAN (R 4.0.2)
#> tidyselect 1.1.0 2020-05-11 [1] CRAN (R 4.0.2)
#> tidyverse * 1.3.0 2019-11-21 [1] CRAN (R 4.0.2)
#> usethis 1.6.1 2020-04-29 [1] CRAN (R 4.0.2)
#> vctrs 0.3.2 2020-07-15 [1] CRAN (R 4.0.2)
#> withr 2.2.0 2020-04-20 [1] CRAN (R 4.0.2)
#> xfun 0.16 2020-07-24 [1] CRAN (R 4.0.2)
#> xml2 1.3.2 2020-04-23 [1] CRAN (R 4.0.2)
#> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.2)
#>
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
To perform the split, you can convert your data frame to a sf object & take advantage of the st_wrap_dateline function:
library(sf)
# convert to sf object & split into 2 polygons
plate.sf <- st_polygon(x = list(as.matrix(plate %>% select(lon, lat)))) %>%
st_wrap_dateline()
# plot using geom_sf
world %>%
ggplot() +
geom_map(map = world,
aes(x = long, y = lat,
map_id = region)) +
geom_sf(data = plate.sf, colour = "dark green")
If you want to stick with geom_polygon instead of geom_sf, convert the polygons back to dataframe:
plate2 <- lapply(seq_along(plate.sf),
function(i) as.data.frame(plate.sf[[i]][[1]]) %>%
rename(lon = V1, lat = V2) %>%
mutate(group = i)) %>%
data.table::rbindlist()
world %>%
ggplot() +
geom_map(map = world,
aes(x = long, y = lat,
map_id = region)) +
geom_polygon(data = plate2,
aes(x = lon, y = lat, group = group),
fill = NA, colour = "dark green")
Edit: expanded answer for dataframe with multiple plates
# mock up data frame with 2 distinct plates (mirror image of each other)
plates <- rbind(plate %>% mutate(plate = 1),
plate %>% mutate(lat = -lat, plate = 2)) %>%
select(plate, lat, lon)
# process data for geom_polygon approach
plates2 <- plates %>%
# split into separate data frame for each plate
split(.$plate) %>%
# convert to polygon & split along date line (as before)
lapply(function(d) d %>% select(lon, lat) %>%
as.matrix() %>%
list() %>%
st_polygon() %>%
st_wrap_dateline()) %>%
# convert each plate back to data frame (as before)
lapply(function(d) lapply(seq_along(d),
function(i) as.data.frame(d[[i]][[1]]) %>%
rename(lon = V1, lat = V2) %>%
mutate(group = i)) %>%
data.table::rbindlist()) %>%
# combine into one overall data frame
bind_rows(.id = "plate") %>%
mutate(group = paste(plate, group, sep = "."))
# result
world %>%
ggplot() +
geom_map(map = world,
aes(x = long, y = lat,
map_id = region)) +
geom_polygon(data = plates2,
aes(x = lon, y = lat, group = group),
fill = NA, colour = "dark green")