Is there a way I can speed up the following code to plot a line graph in R from a matrix? The matrix is quite big, around 3000 rows by 60 columns, sometimes the data is bigger.
I currently use matlines or ggplot for example:
library(ggplot2)
library(reshape2)
## using ggplot2
data <- matrix(rnorm(3000*60), ncol = 60)
df <- as.data.frame(data)
df$id <- rownames(df)
plot_data <- melt(df, id.var = "id")
ggplot(plot_data, aes(x=variable,y=value,group=id)) + geom_point() + geom_line()
## using matlines
plot(0, ylim = range(data), xlim = c(1, ncol(data)), ylab = "tmp",
type = "n", xaxt = "n", xlab = "")
axis(1, at = seq(ncol(data)), labels = colnames(data), las = 2)
matlines(t(data))
I used Sys.time() to get an idea of timings and it's pretty slow in both cases, around 22 seconds for ggplot and 24 seconds for matlines. I am building this into a Shiny app and it takes a very long time to render (similarly in the console).
Any help greatly appreciated. Thank you.
> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS High Sierra 10.13.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats4 parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] reshape2_1.4.4 ggplot2_3.3.1 circlize_0.4.9 shinyhelper_0.3.2 colorspace_1.4-1
[6] colourpicker_1.0 shinythemes_1.1.2 DT_0.13 shiny_1.4.0.2 dplyr_1.0.0
[11] pRoloc_1.29.0 BiocParallel_1.22.0 MLInterfaces_1.68.0 cluster_2.1.0 annotate_1.66.0
[16] XML_3.99-0.3 AnnotationDbi_1.50.0 IRanges_2.22.2 MSnbase_2.14.2 ProtGenerics_1.20.0
[21] S4Vectors_0.26.1 mzR_2.22.0 Rcpp_1.0.4.6 Biobase_2.48.0 BiocGenerics_0.34.0
You may be able to decrease your rendering time by reducing the number of colors and linetypes in your plot. For example, compare default matlines, which cycles through 5 linetypes and 6 colors:
system.time({
plot(0, ylim = range(data), xlim = c(1, ncol(data)), ylab = "tmp",
type = "n", xaxt = "n", xlab = "")
axis(1, at = seq(ncol(data)), labels = colnames(data), las = 2)
matlines(t(data))
})
# user system elapsed
# 0.66 3.50 4.15
to one where we only use one linetype and one color
system.time({
plot(0, ylim = range(data), xlim = c(1, ncol(data)), ylab = "tmp",
type = "n", xaxt = "n", xlab = "")
axis(1, at = seq(ncol(data)), labels = colnames(data), las = 2)
matlines(t(data), col = 1, lty = 1)
})
# user system elapsed
# 0.23 0.58 0.83
As others have mentioned, other than the performance aspect, simplifying the plot may be good in and of itself.
I just want to highlight how expensive drawing the points is if you have to draw a lot of them in ggplot2. Here are some different benchmarks with different types of points. If you omit the points you can draw the plot in 1/3rd of the time. Having periods . as points in 40% of the time. If you definitely need proper points, I recommend shape = 16.
library(ggplot2)
library(reshape2)
data <- matrix(rnorm(3000*60), ncol = 60)
df <- as.data.frame(data)
df$id <- rownames(df)
plot_data <- melt(df, id.var = "id")
bm <- microbenchmark::microbenchmark(
baseline = {
g <- ggplot(plot_data, aes(x=variable,y=value,group=id)) +
geom_point() +
geom_line()
print(g)
},
nopoint = {
g <- ggplot(plot_data, aes(x=variable,y=value,group=id)) +
geom_line()
print(g)
},
periodpoint = {
g <- ggplot(plot_data, aes(x=variable,y=value,group=id)) +
geom_point(shape = ".") +
geom_line()
print(g)
},
shape16point = {
g <- ggplot(plot_data, aes(x=variable,y=value,group=id)) +
geom_point(shape = 16) +
geom_line()
print(g)
},
times = 10
)
df <- data.frame(
x = bm$expr,
y = bm$time / 1e9 # because measured in nanoseconds
)
ggplot(df, aes(x, y)) +
geom_boxplot() +
scale_y_continuous(limits = c(0, NA), name = "Time (s)")
Created on 2020-06-27 by the reprex package (v0.3.0)
devtools::session_info()
#> - Session info ---------------------------------------------------------------
#> setting value
#> version R version 4.0.0 (2020-04-24)
#> os Windows 10 x64
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate English_United Kingdom.1252
#> ctype English_United Kingdom.1252
#> tz Europe/Berlin
#> date 2020-06-27
#>
#> - Packages -------------------------------------------------------------------
#> package * version date lib source
#> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.0)
#> backports 1.1.7 2020-05-13 [1] CRAN (R 4.0.0)
#> callr 3.4.3 2020-03-28 [1] CRAN (R 4.0.0)
#> cli 2.0.2 2020-02-28 [1] CRAN (R 4.0.0)
#> colorspace 1.4-1 2019-03-18 [1] CRAN (R 4.0.0)
#> crayon 1.3.4 2017-09-16 [1] CRAN (R 4.0.0)
#> curl 4.3 2019-12-02 [1] CRAN (R 4.0.0)
#> desc 1.2.0 2018-05-01 [1] CRAN (R 4.0.0)
#> devtools 2.3.0 2020-04-10 [1] CRAN (R 4.0.0)
#> digest 0.6.25 2020-02-23 [1] CRAN (R 4.0.0)
#> dplyr 0.8.5 2020-03-07 [1] CRAN (R 4.0.0)
#> ellipsis 0.3.1 2020-05-15 [1] CRAN (R 4.0.0)
#> evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.0)
#> fansi 0.4.1 2020-01-08 [1] CRAN (R 4.0.0)
#> farver 2.0.3 2020-01-16 [1] CRAN (R 4.0.0)
#> fs 1.4.1 2020-04-04 [1] CRAN (R 4.0.0)
#> ggplot2 * 3.3.0.9000 2020-05-31 [1] Github (tidyverse/ggplot2#2b03e47)
#> glue 1.4.1 2020-05-13 [1] CRAN (R 4.0.0)
#> gtable 0.3.0 2019-03-25 [1] CRAN (R 4.0.0)
#> highr 0.8 2019-03-20 [1] CRAN (R 4.0.0)
#> htmltools 0.5.0 2020-06-16 [1] CRAN (R 4.0.2)
#> httr 1.4.1 2019-08-05 [1] CRAN (R 4.0.0)
#> knitr 1.28 2020-02-06 [1] CRAN (R 4.0.0)
#> labeling 0.3 2014-08-23 [1] CRAN (R 4.0.0)
#> lifecycle 0.2.0 2020-03-06 [1] CRAN (R 4.0.0)
#> magrittr 1.5 2014-11-22 [1] CRAN (R 4.0.0)
#> memoise 1.1.0 2017-04-21 [1] CRAN (R 4.0.0)
#> microbenchmark 1.4-7 2019-09-24 [1] CRAN (R 4.0.2)
#> mime 0.9 2020-02-04 [1] CRAN (R 4.0.0)
#> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.0.0)
#> pillar 1.4.4 2020-05-05 [1] CRAN (R 4.0.0)
#> pkgbuild 1.0.8 2020-05-07 [1] CRAN (R 4.0.0)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.0.0)
#> pkgload 1.1.0 2020-05-29 [1] CRAN (R 4.0.0)
#> plyr 1.8.6 2020-03-03 [1] CRAN (R 4.0.0)
#> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.0)
#> processx 3.4.2 2020-02-09 [1] CRAN (R 4.0.0)
#> ps 1.3.3 2020-05-08 [1] CRAN (R 4.0.0)
#> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.0.0)
#> R6 2.4.1 2019-11-12 [1] CRAN (R 4.0.0)
#> Rcpp 1.0.4.6 2020-04-09 [1] CRAN (R 4.0.0)
#> remotes 2.1.1 2020-02-15 [1] CRAN (R 4.0.0)
#> reshape2 * 1.4.4 2020-04-09 [1] CRAN (R 4.0.0)
#> rlang 0.4.6 2020-05-02 [1] CRAN (R 4.0.0)
#> rmarkdown 2.1 2020-01-20 [1] CRAN (R 4.0.0)
#> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 4.0.0)
#> scales 1.1.1 2020-05-11 [1] CRAN (R 4.0.0)
#> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.0)
#> stringi 1.4.6 2020-02-17 [1] CRAN (R 4.0.0)
#> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.0.0)
#> testthat 2.3.2 2020-03-02 [1] CRAN (R 4.0.0)
#> tibble 3.0.1 2020-04-20 [1] CRAN (R 4.0.0)
#> tidyselect 1.1.0 2020-05-11 [1] CRAN (R 4.0.0)
#> usethis 1.6.1 2020-04-29 [1] CRAN (R 4.0.0)
#> vctrs 0.3.0 2020-05-11 [1] CRAN (R 4.0.0)
#> withr 2.2.0 2020-04-20 [1] CRAN (R 4.0.0)
#> xfun 0.13 2020-04-13 [1] CRAN (R 4.0.0)
#> xml2 1.3.2 2020-04-23 [1] CRAN (R 4.0.0)
#> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.0)
#>
#> [1] D:/Users/teunv/Documents/R/win-library/4.0
#> [2] C:/Program Files/R/R-4.0.0/library
Related
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’m attempting to add a circle using geom_polygon() to add a reference line to radial bar chart. (I know this is not the ideal way to present this data, but it’s not entirely up to me).
Following this issue, I can create a nice circle:
library(tidyverse)
lines <- tibble(
rating = rep(1:7, times = 3),
average = rep(1:3, each = 7)
)
ggplot(lines, aes(x = rating, y = average)) +
geom_polygon(aes(group = average), fill = NA, color = "black") +
scale_x_continuous(breaks = 1:7) +
scale_y_continuous(limits = c(0, 3), breaks = 0:3) +
coord_polar()
This is great. The problem is that 1 and 7 share the top spot. So when I add data for the bars, the circle flattens out.
set.seed(123)
bars <- tibble(rating = 1:7,
average = runif(7, min = 0, max = 3))
ggplot(lines, aes(x = rating, y = average)) +
geom_col(data = bars, aes(fill = factor(rating)), show.legend = FALSE) +
geom_polygon(aes(group = average), fill = NA, color = "black") +
scale_x_continuous(breaks = 1:7) +
scale_y_continuous(limits = c(0, 3), breaks = 0:3) +
coord_polar()
Is there any way keep the polygon a circle in this situation? Or to interpolate between the 7 and 1 to keep the original curve?
Created on 2021-07-26 by the reprex package (v2.0.0)
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/Chicago
#> date 2021-07-26
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date lib source
#> assertthat 0.2.1 2019-03-21 [1] standard (#0.2.1)
#> backports 1.2.1 2020-12-09 [1] standard (#1.2.1)
#> broom 0.7.8 2021-06-24 [1] CRAN (R 4.1.0)
#> cellranger 1.1.0 2016-07-27 [1] standard (#1.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] standard (#1.4.1)
#> curl 4.3.2 2021-06-23 [1] CRAN (R 4.1.0)
#> DBI 1.1.1 2021-01-15 [1] standard (#1.1.1)
#> dbplyr 2.1.1 2021-04-06 [1] standard (#2.1.1)
#> digest 0.6.27 2020-10-24 [1] standard (#0.6.27)
#> dplyr * 1.0.7 2021-06-18 [1] CRAN (R 4.1.0)
#> ellipsis 0.3.2 2021-04-29 [1] standard (#0.3.2)
#> evaluate 0.14 2019-05-28 [1] standard (#0.14)
#> fansi 0.5.0 2021-05-25 [1] standard (#0.5.0)
#> farver 2.1.0 2021-02-28 [1] standard (#2.1.0)
#> forcats * 0.5.1 2021-01-27 [1] standard (#0.5.1)
#> fs 1.5.0 2020-07-31 [1] standard (#1.5.0)
#> generics 0.1.0 2020-10-31 [1] standard (#0.1.0)
#> ggplot2 * 3.3.5 2021-06-25 [1] CRAN (R 4.1.0)
#> glue 1.4.2 2020-08-27 [1] standard (#1.4.2)
#> gtable 0.3.0 2019-03-25 [1] standard (#0.3.0)
#> haven 2.4.1 2021-04-23 [1] standard (#2.4.1)
#> highr 0.9 2021-04-16 [1] standard (#0.9)
#> hms 1.1.0 2021-05-17 [1] standard (#1.1.0)
#> htmltools 0.5.1.1 2021-01-22 [1] standard (#0.5.1.1)
#> httr 1.4.2 2020-07-20 [1] standard (#1.4.2)
#> jsonlite 1.7.2 2020-12-09 [1] standard (#1.7.2)
#> knitr 1.33 2021-04-24 [1] standard (#1.33)
#> lifecycle 1.0.0 2021-02-15 [1] standard (#1.0.0)
#> lubridate 1.7.10 2021-02-26 [1] standard (#1.7.10)
#> magrittr 2.0.1 2020-11-17 [1] standard (#2.0.1)
#> mime 0.11 2021-06-23 [1] CRAN (R 4.1.0)
#> modelr 0.1.8 2020-05-19 [1] standard (#0.1.8)
#> munsell 0.5.0 2018-06-12 [1] standard (#0.5.0)
#> pillar 1.6.1 2021-05-16 [1] standard (#1.6.1)
#> pkgconfig 2.0.3 2019-09-22 [1] standard (#2.0.3)
#> purrr * 0.3.4 2020-04-17 [1] standard (#0.3.4)
#> R6 2.5.0 2020-10-28 [1] standard (#2.5.0)
#> Rcpp 1.0.7 2021-07-07 [1] CRAN (R 4.1.0)
#> readr * 2.0.0 2021-07-20 [1] CRAN (R 4.1.0)
#> readxl 1.3.1 2019-03-13 [1] standard (#1.3.1)
#> reprex 2.0.0 2021-04-02 [1] standard (#2.0.0)
#> rlang 0.4.11 2021-04-30 [1] standard (#0.4.11)
#> rmarkdown 2.9 2021-06-15 [1] CRAN (R 4.1.0)
#> rstudioapi 0.13 2020-11-12 [1] standard (#0.13)
#> rvest 1.0.1 2021-07-26 [1] CRAN (R 4.1.0)
#> scales 1.1.1 2020-05-11 [1] standard (#1.1.1)
#> sessioninfo 1.1.1 2018-11-05 [1] standard (#1.1.1)
#> stringi 1.7.3 2021-07-16 [1] CRAN (R 4.1.0)
#> stringr * 1.4.0 2019-02-10 [1] standard (#1.4.0)
#> styler 1.5.1 2021-07-13 [1] CRAN (R 4.1.0)
#> tibble * 3.1.3 2021-07-23 [1] CRAN (R 4.1.0)
#> tidyr * 1.1.3 2021-03-03 [1] standard (#1.1.3)
#> tidyselect 1.1.1 2021-04-30 [1] standard (#1.1.1)
#> tidyverse * 1.3.1 2021-04-15 [1] standard (#1.3.1)
#> 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] standard (#0.3.8)
#> withr 2.4.2 2021-04-18 [1] standard (#2.4.2)
#> xfun 0.24 2021-06-15 [1] CRAN (R 4.1.0)
#> xml2 1.3.2 2020-04-23 [1] standard (#1.3.2)
#> yaml 2.2.1 2020-02-01 [1] standard (#2.2.1)
#>
#> [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library
One option would be to make use of geom_hline instead of geom_polygon:
library(tidyverse)
set.seed(123)
lines <- tibble(
rating = rep(1:7, times = 3),
average = rep(1:3, each = 7)
)
bars <- tibble(rating = 1:7,
average = runif(7, min = 0, max = 3))
ggplot(lines, aes(x = rating, y = average)) +
geom_col(data = bars, aes(fill = factor(rating)), show.legend = FALSE) +
geom_hline(aes(yintercept = average)) +
scale_x_continuous(breaks = 1:7) +
scale_y_continuous(limits = c(0, 3), breaks = 0:3) +
coord_polar()
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")
This is a direct follow up to How to interpret ggplot2::stat_density2d.
bins has been re-added as an argument see this thread and the corresponding github issue, but it remains a mistery to me how to interpret those bins.
This answer (answer 1) suggests a way to calculate contour lines based on probabilities, and this answer argues that the current use of kde2d in stat_density_2d would not mean that the bins can be interpreted as percentiles.
So the question.
When trying both approaches in order to get estimate quintile probabilities of the data, I get four lines as expected using the approach from answer 1, but only three lines with bins = 5 in stat_density_2d. (which, as I believe, would give 4 bins!)
The fifth bin could be this tiny little dot in the centre which appears (maybe the centroid??)???
Is one of the ways utterly wrong? Or both? Or just two ways of estimating probabilities with their very own imprecision?
library(ggplot2)
#modifying function from answer1
prob_contour <- function(data, n = 50, prob = 0.95, ...) {
post1 <- MASS::kde2d(data[[1]], data[[2]], n = n, ...)
dx <- diff(post1$x[1:2])
dy <- diff(post1$y[1:2])
sz <- sort(post1$z)
c1 <- cumsum(sz) * dx * dy
levels <- sapply(prob, function(x) {
approx(c1, sz, xout = 1 - x)$y
})
df <- as.data.frame(grDevices::contourLines(post1$x, post1$y, post1$z, levels = levels))
df$x <- round(df$x, 3)
df$y <- round(df$y, 3)
df$level <- round(df$level, 2)
df$prob <- as.character(prob)
df
}
set.seed(1)
n=100
foo <- data.frame(x=rnorm(n, 0, 1), y=rnorm(n, 0, 1))
df_contours <- dplyr::bind_rows(
purrr::map(seq(0.2, 0.8, 0.2), function(p) prob_contour(foo, prob = p))
)
ggplot() +
stat_density_2d(data = foo, aes(x, y), bins = 5, color = "black") +
geom_point(data = foo, aes(x = x, y = y)) +
geom_polygon(data = df_contours, aes(x = x, y = y, color = prob), fill = NA) +
scale_color_brewer(name = "Probs", palette = "Set1")
Created on 2020-05-15 by the reprex package (v0.3.0)
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.0.0 (2020-04-24)
#> os macOS Catalina 10.15.4
#> system x86_64, darwin17.0
#> ui X11
#> language (EN)
#> collate en_GB.UTF-8
#> ctype en_GB.UTF-8
#> tz Europe/London
#> date 2020-05-15
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date lib source
#> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.0)
#> backports 1.1.7 2020-05-13 [1] CRAN (R 4.0.0)
#> callr 3.4.3 2020-03-28 [1] CRAN (R 4.0.0)
#> cli 2.0.2 2020-02-28 [1] CRAN (R 4.0.0)
#> colorspace 1.4-1 2019-03-18 [1] CRAN (R 4.0.0)
#> crayon 1.3.4 2017-09-16 [1] CRAN (R 4.0.0)
#> curl 4.3 2019-12-02 [1] CRAN (R 4.0.0)
#> desc 1.2.0 2018-05-01 [1] CRAN (R 4.0.0)
#> devtools 2.3.0 2020-04-10 [1] CRAN (R 4.0.0)
#> digest 0.6.25 2020-02-23 [1] CRAN (R 4.0.0)
#> dplyr 0.8.5 2020-03-07 [1] CRAN (R 4.0.0)
#> ellipsis 0.3.0 2019-09-20 [1] CRAN (R 4.0.0)
#> evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.0)
#> fansi 0.4.1 2020-01-08 [1] CRAN (R 4.0.0)
#> farver 2.0.3 2020-01-16 [1] CRAN (R 4.0.0)
#> fs 1.4.1 2020-04-04 [1] CRAN (R 4.0.0)
#> ggplot2 * 3.3.0 2020-03-05 [1] CRAN (R 4.0.0)
#> glue 1.4.1 2020-05-13 [1] CRAN (R 4.0.0)
#> gtable 0.3.0 2019-03-25 [1] CRAN (R 4.0.0)
#> highr 0.8 2019-03-20 [1] CRAN (R 4.0.0)
#> htmltools 0.4.0 2019-10-04 [1] CRAN (R 4.0.0)
#> httr 1.4.1 2019-08-05 [1] CRAN (R 4.0.0)
#> isoband 0.2.1 2020-04-12 [1] CRAN (R 4.0.0)
#> knitr 1.28 2020-02-06 [1] CRAN (R 4.0.0)
#> labeling 0.3 2014-08-23 [1] CRAN (R 4.0.0)
#> lifecycle 0.2.0 2020-03-06 [1] CRAN (R 4.0.0)
#> magrittr 1.5 2014-11-22 [1] CRAN (R 4.0.0)
#> MASS 7.3-51.5 2019-12-20 [1] CRAN (R 4.0.0)
#> memoise 1.1.0 2017-04-21 [1] CRAN (R 4.0.0)
#> mime 0.9 2020-02-04 [1] CRAN (R 4.0.0)
#> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.0.0)
#> pillar 1.4.4 2020-05-05 [1] CRAN (R 4.0.0)
#> pkgbuild 1.0.8 2020-05-07 [1] CRAN (R 4.0.0)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.0.0)
#> pkgload 1.0.2 2018-10-29 [1] CRAN (R 4.0.0)
#> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.0)
#> processx 3.4.2 2020-02-09 [1] CRAN (R 4.0.0)
#> ps 1.3.3 2020-05-08 [1] CRAN (R 4.0.0)
#> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.0.0)
#> R6 2.4.1 2019-11-12 [1] CRAN (R 4.0.0)
#> RColorBrewer 1.1-2 2014-12-07 [1] CRAN (R 4.0.0)
#> Rcpp 1.0.4.6 2020-04-09 [1] CRAN (R 4.0.0)
#> remotes 2.1.1 2020-02-15 [1] CRAN (R 4.0.0)
#> rlang 0.4.6 2020-05-02 [1] CRAN (R 4.0.0)
#> rmarkdown 2.1 2020-01-20 [1] CRAN (R 4.0.0)
#> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 4.0.0)
#> scales 1.1.1 2020-05-11 [1] CRAN (R 4.0.0)
#> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.0)
#> stringi 1.4.6 2020-02-17 [1] CRAN (R 4.0.0)
#> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.0.0)
#> testthat 2.3.2 2020-03-02 [1] CRAN (R 4.0.0)
#> tibble 3.0.1 2020-04-20 [1] CRAN (R 4.0.0)
#> tidyselect 1.1.0 2020-05-11 [1] CRAN (R 4.0.0)
#> usethis 1.6.1 2020-04-29 [1] CRAN (R 4.0.0)
#> vctrs 0.3.0 2020-05-11 [1] CRAN (R 4.0.0)
#> withr 2.2.0 2020-04-20 [1] CRAN (R 4.0.0)
#> xfun 0.13 2020-04-13 [1] CRAN (R 4.0.0)
#> xml2 1.3.2 2020-04-23 [1] CRAN (R 4.0.0)
#> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.0)
#>
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
(Despite all the mystery, it is somewhat reassuring that the contours look vaguely similar)
I'm not sure this fully answers your question, but there has been a change in behaviour between ggplot v3.2.1 and v3.3.0 due to the way the contour bins are calculated. In the earlier version, the bins are calculated in StatContour$compute_group, whereas in the later version, StatContour$compute_group delegates this task to the unexported function contour_breaks. In contour_breaks, the bin widths are calculated by the density range divided by bins - 1, whereas in the earlier version they are calculated by the range divided by bins.
We can revert this behaviour by temporarily changing the contour_breaks function:
Before
ggplot() +
stat_density_2d(data = foo, aes(x, y), bins = 5, color = "black") +
geom_point(data = foo, aes(x = x, y = y)) +
geom_polygon(data = df_contours, aes(x = x, y = y, color = prob), fill = NA) +
scale_color_brewer(name = "Probs", palette = "Set1")
Now change the divisor in contour_breaks from bins - 1 to bins:
my_fun <- ggplot2:::contour_breaks
body(my_fun)[[4]][[3]][[2]][[3]][[3]] <- quote(bins)
assignInNamespace("contour_breaks", my_fun, ns = "ggplot2", pos = "package:ggplot2")
After
Using exactly the same code as produced the first plot:
ggplot() +
stat_density_2d(data = foo, aes(x, y), bins = 5, color = "black") +
geom_point(data = foo, aes(x = x, y = y)) +
geom_polygon(data = df_contours, aes(x = x, y = y, color = prob), fill = NA) +
scale_color_brewer(name = "Probs", palette = "Set1")
I am trying to use ggplot2 to plot some graphs with emojis shown as labels using the emo package. I've learned it from this post, but it's simply not working.
I have tried the emojifont package before, but it's a font type that renders emojis in black and white and it requires opening a new graphics device using e.g. quartz().
To go around the color problem, Tino has suggested (refer to the post above) using the gridSVG package, i.e. after creating a new graphics device and plotting with emojifont, save the graph ps = grid.export("emoji.svg", addClass=T) on local disk as a .svg file that renders emojis in a colorful style.
I would really appreciate a solution that (a) gives colorful emojis and (b) showing the graph directly, which is compatible with routine ggplot use cases.
library(ggplot2)
library(emo)
names = c("smile","school","office","blush","smirk","heart_eyes")
n = length(names):1
e = sapply(names, emo::ji)
dat = data.frame(emoji_name = names, n = n, emoji = e, stringsAsFactors = F)
ggplot(data=dat, aes(emoji_name, n)) +
geom_bar(stat = "identity") +
scale_x_discrete(breaks = dat$emoji_name, labels = dat$emoji) +
coord_flip()
My R version is
> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12
use the emojifont package :)
read more here
library(ggplot2)
library(emojifont)
names = c("smile","school","office","blush","smirk","heart_eyes")
n = length(names):1
e = sapply(names, emojifont::emoji)
dat = data.frame(emoji_name = names, n = n, emoji = e, stringsAsFactors = F)
ggplot(data=dat, aes(emoji_name, n)) +
geom_bar(stat = "identity") +
scale_x_discrete(breaks = dat$emoji_name, labels = dat$emoji) +
theme( axis.text.y =element_text( size=20 ) ) +
coord_flip()
> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
library(ggplot2)
library(dplyr)
library(purrr)
#remotes::install_github("hadley/emo")
library(emo)
dat <- tibble(names = c("smile","school","office","blush","smirk","heart_eyes"),
emoji = map_chr(names, emo::ji)) %>%
tibble::rowid_to_column("n")
dat %>% ggplot(aes(n, emoji)) +
geom_col(orientation="y") +
theme(axis.text.y = element_text(size = 18))
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 3.6.3 (2020-02-29)
#> os Ubuntu 18.04.4 LTS
#> system x86_64, linux-gnu
#> ui X11
#> language en_GB:en
#> collate en_GB.UTF-8
#> ctype en_GB.UTF-8
#> tz Europe/Oslo
#> date 2020-04-07
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date lib source
#> assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.0)
#> cli 2.0.2 2020-02-28 [1] CRAN (R 3.6.3)
#> colorspace 1.4-1 2019-03-18 [1] CRAN (R 3.6.0)
#> crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.0)
#> digest 0.6.25 2020-02-23 [1] CRAN (R 3.6.3)
#> dplyr * 0.8.5 2020-03-07 [1] CRAN (R 3.6.3)
#> ellipsis 0.3.0 2019-09-20 [1] CRAN (R 3.6.1)
#> emo * 0.0.0.9000 2020-04-07 [1] Github (hadley/emo#3f03b11)
#> evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.0)
#> fansi 0.4.1 2020-01-08 [1] CRAN (R 3.6.2)
#> farver 2.0.3 2020-01-16 [1] CRAN (R 3.6.2)
#> generics 0.0.2 2018-11-29 [1] CRAN (R 3.6.0)
#> ggplot2 * 3.3.0 2020-03-05 [1] CRAN (R 3.6.3)
#> glue 1.4.0 2020-04-03 [1] CRAN (R 3.6.3)
#> gtable 0.3.0 2019-03-25 [1] CRAN (R 3.6.0)
#> highr 0.8 2019-03-20 [1] CRAN (R 3.6.0)
#> htmltools 0.4.0 2019-10-04 [1] CRAN (R 3.6.1)
#> knitr 1.28 2020-02-06 [1] CRAN (R 3.6.3)
#> labeling 0.3 2014-08-23 [1] CRAN (R 3.6.0)
#> lifecycle 0.2.0 2020-03-06 [1] CRAN (R 3.6.3)
#> lubridate 1.7.8 2020-04-06 [1] CRAN (R 3.6.3)
#> magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.3)
#> munsell 0.5.0 2018-06-12 [1] CRAN (R 3.6.0)
#> pillar 1.4.3 2019-12-20 [1] CRAN (R 3.6.2)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.6.1)
#> purrr * 0.3.3 2019-10-18 [1] CRAN (R 3.6.1)
#> R6 2.4.1 2019-11-12 [1] CRAN (R 3.6.1)
#> Rcpp 1.0.4 2020-03-17 [1] CRAN (R 3.6.3)
#> rlang 0.4.5 2020-03-01 [1] CRAN (R 3.6.3)
#> rmarkdown 2.1 2020-01-20 [1] CRAN (R 3.6.3)
#> scales 1.1.0.9000 2020-04-03 [1] local
#> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.0)
#> stringi 1.4.6 2020-02-17 [1] CRAN (R 3.6.3)
#> stringr 1.4.0 2019-02-10 [1] CRAN (R 3.6.0)
#> tibble 3.0.0 2020-03-30 [1] CRAN (R 3.6.3)
#> tidyselect 1.0.0 2020-01-27 [1] CRAN (R 3.6.3)
#> vctrs 0.2.4 2020-03-10 [1] CRAN (R 3.6.3)
#> withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.0)
#> xfun 0.12 2020-01-13 [1] CRAN (R 3.6.2)
#> yaml 2.2.1 2020-02-01 [1] CRAN (R 3.6.2)
#>
#> [1] /home/dm0737pe/R/x86_64-pc-linux-gnu-library/3.6
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library
Created on 2020-04-07 by the reprex package (v0.3.0)