I am trying to create a flowchart using this excellent tutorial: https://dannyjnwong.github.io/STROBE-CONSORT-Diagrams-in-R/
library(DiagrammeR)
library(DiagrammeRsvg) #Needed if you want to export the image
library(rsvg) #Needed if you want to export the image
#Set the values which will go into each label.
a1 <- 'Total available patients\n(n = x)'
b1 <- ''
c1 <- ''
d1 <- 'Included for analysis\n(n = x - y - z)'
e1 <- 'Data linked with\nexternal dataset'
a2 <- ''
b2 <- 'Excluded because of\nexclusion criteria (n = y)'
c2 <- 'Excluded because of\nmissing values (n = z)'
d2 <- ''
e2 <- ''
#Create a node dataframe
ndf <- create_node_df(
n = 10,
label = c(a1, b1, c1, d1, e1, #Column 1
a2, b2, c2, d2, e2), #Column 2
style = c('solid', 'invis', 'invis', 'solid', 'solid', #Column 1
'invis', 'solid', 'solid', 'invis', 'invis'), #Column 2
shape = c('box', 'point', 'point', 'box', 'box', #Column 1
'plaintext', 'box', 'box', 'point', 'point'), #Column 2
width = c(3, 0.001, 0.001, 3, 3, #Column 1
2, 2.5, 2.5, 0.001, 0.001), #Column 2
height = c(1, 0.001, 0.001, 1, 1, #Column 1
1, 1, 1, 0.001, 0.001), #Column 2
fontsize = c(rep(14, 10)),
fontname = c(rep('Helvetica', 10)),
penwidth = 1.5,
fixedsize = 'true')
#Create an edge dataframe
edf <- create_edge_df(
from = c(1, 2, 3, 4, #Column 1
6, 7, 8, 9, #Column 2
2, 3 #Horizontals
),
to = c(2, 3, 4, 5, #Column 1
7, 8, 9, 10, #Column 2
7, 8 #Horizontals
),
arrowhead = c('none', 'none', 'normal', 'normal', #Column 1
'none', 'none', 'none', 'none', #Column 2
'normal', 'normal' #Horizontals
),
color = c('black', 'black', 'black', 'black', #Column 1
'#00000000', '#00000000', '#00000000', '#00000000', #Column 2
'black', 'black' #Horizontals
),
constraint = c(rep('true', 8), #Columns
rep('false', 2) #Horizontals
)
)
g <- create_graph(ndf,
edf,
attr_theme = NULL)
render_graph(g)
The result should look like this:
But instead it looks likes this when i run the code on my computer
Anyone eager to help?
Best regards,
H
Maybe a bit late, but better than never.
You have to create two nodes with width = 0 and height = 0, from where the arrows can origin horizontally. They need to be defined as subgraphs in order to be on a horizontal axis.
DiagrammeR::grViz("digraph prisma{
node [shape=box, fontsize = 12, fontname = 'Helvetica', width = 2];
a; b; c; d; e
# create 2 nodes without box around
node [shape = point, width = 0, height = 0]
x1; x2
graph [splines=ortho, nodesep=1, dpi = 72]
# Labels
a [label = 'Total available patients\n(n = x)'];
b [label = 'Excluded because of\nexclusion criteria (n = y)'];
c [label = 'Excluded because of\nmissing values (n = z)'];
d [label = 'Included for analysis\n(n = x - y - z)']
e [label = 'Data linked with\nexternal dataset']
# Edge definitions
a -> x1 [arrowhead='none']
x1 -> x2 [arrowhead='none']
x1 -> b
x2 -> c
x2 -> d
d -> e
# Make subgraph definition so arrow is horzontal
subgraph {
rank = same; b; x1;
}
subgraph {
rank = same; c; x2;
}
}
}
")
Created on 2021-08-06 by the reprex package (v2.0.1)
Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.0.3 (2020-10-10)
#> os macOS Catalina 10.15.7
#> system x86_64, darwin17.0
#> ui X11
#> language (EN)
#> collate de_CH.UTF-8
#> ctype de_CH.UTF-8
#> tz Europe/Zurich
#> date 2021-08-06
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date lib source
#> backports 1.2.1 2020-12-09 [1] CRAN (R 4.0.2)
#> callr 3.7.0 2021-04-20 [1] CRAN (R 4.0.2)
#> cli 3.0.0 2021-06-30 [1] CRAN (R 4.0.2)
#> crayon 1.4.1 2021-02-08 [1] CRAN (R 4.0.2)
#> curl 4.3.2 2021-06-23 [1] CRAN (R 4.0.2)
#> DiagrammeR 1.0.6.1 2020-05-08 [1] CRAN (R 4.0.2)
#> digest 0.6.27 2020-10-24 [1] CRAN (R 4.0.2)
#> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.0.2)
#> evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.0)
#> fansi 0.5.0 2021-05-25 [1] CRAN (R 4.0.2)
#> fs 1.5.0 2020-07-31 [1] CRAN (R 4.0.2)
#> glue 1.4.2 2020-08-27 [1] CRAN (R 4.0.2)
#> highr 0.9 2021-04-16 [1] CRAN (R 4.0.2)
#> htmltools 0.5.1.1 2021-01-22 [1] CRAN (R 4.0.2)
#> htmlwidgets 1.5.2 2020-10-03 [1] CRAN (R 4.0.2)
#> httr 1.4.2 2020-07-20 [1] CRAN (R 4.0.2)
#> jsonlite 1.7.2 2020-12-09 [1] CRAN (R 4.0.2)
#> knitr 1.33 2021-04-24 [1] CRAN (R 4.0.2)
#> lifecycle 1.0.0 2021-02-15 [1] CRAN (R 4.0.2)
#> magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.0.2)
#> mime 0.11 2021-06-23 [1] CRAN (R 4.0.2)
#> pillar 1.6.1 2021-05-16 [1] CRAN (R 4.0.2)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.0.0)
#> processx 3.5.2 2021-04-30 [1] CRAN (R 4.0.2)
#> ps 1.6.0 2021-02-28 [1] CRAN (R 4.0.2)
#> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.0.0)
#> R6 2.5.0 2020-10-28 [1] CRAN (R 4.0.2)
#> RColorBrewer 1.1-2 2014-12-07 [1] CRAN (R 4.0.0)
#> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.0.3)
#> rlang 0.4.11 2021-04-30 [1] CRAN (R 4.0.2)
#> rmarkdown 2.9 2021-06-15 [1] CRAN (R 4.0.2)
#> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.0.2)
#> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.0)
#> stringi 1.7.3 2021-07-16 [1] CRAN (R 4.0.2)
#> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.0.2)
#> styler 1.3.2 2020-02-23 [1] CRAN (R 4.0.2)
#> tibble 3.1.2 2021-05-16 [1] CRAN (R 4.0.2)
#> utf8 1.2.1 2021-03-12 [1] CRAN (R 4.0.2)
#> vctrs 0.3.8 2021-04-29 [1] CRAN (R 4.0.2)
#> visNetwork 2.0.9 2019-12-06 [1] CRAN (R 4.0.2)
#> webshot 0.5.2 2019-11-22 [1] CRAN (R 4.0.0)
#> withr 2.4.2 2021-04-18 [1] CRAN (R 4.0.2)
#> xfun 0.24 2021-06-15 [1] CRAN (R 4.0.2)
#> 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
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()
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
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'm quite new using R, so hopefully this isn't too basic..
I'm trying to create a summary table, using qwraps2. Having followed the well-written tutorial without problems (qwraps2) I run into an error when applying my own dataset.
library(qwraps2)
summary_tbl1 <-
list("Gender" =
list("Female" = ~ qwraps2::n_perc0(.data$gender == 0),
"Male" = ~ qwraps2::n_perc0(.data$gender == 1)),
"Mean age (sd)" = ~ qwraps2::mean_sd(.data$inage),
"Age categories" =
list("65-74" = ~ qwraps2::n_perc0(.data$age_cat == 1),
"75-84" = ~ qwraps2::n_perc0(.data$age_cat == 2),
"> 85" = ~ qwraps2::n_perc0(.data$age_cat == 3))
)
#making the overall column
c_overall <- summary_table(my_dataset, summary_tbl1)
Error: x must be a formula Call rlang::last_error() to see a backtrace
The backtrace reads as follows:
12. stop(cnd)
11. rlang::abort(x)
10. rlang::f_rhs(y)
9. FUN(X[[i]], ...)
8. lapply(s, function(y) { rlang::f_rhs(y) })
7. FUN(X[[i]], ...)
6. lapply(summaries, function(s) { lapply(s, function(y) { rlang::f_rhs(y) })...
5. eval(lhs, parent, parent)
4. eval(lhs, parent, parent)
3. lapply(summaries, function(s) { lapply(s, function(y) { rlang::f_rhs(y) })...
2. summary_table.data.frame(new_dataset, summary_tbl1)
1. summary_table(new_dataset, summary_tbl1)
I have converted the dataset to a data.frame using as.data.frame, as that is what summary_tablerequires, from what i can understand.
My dataset is imported from STATA (Haven package), could that be the answer, and if that is case - any ideas on how to overcome?
Or could it be related to the size of my dataset (80.300 obs)?
Thanks in advance
Added the summary readout:
summary_tbl1
$`Gender`
$`Gender`$`Female`
~qwraps2::n_perc0(.data$gender == 0)
$`Gender`$Male
~qwraps2::n_perc0(.data$gender == 1)
$`Mean age (sd)`
~qwraps2::mean_sd(.data$inage)
$`Age categories`
$`Age categories`$`65-74`
~qwraps2::n_perc0(.data$age_cat == 1)
$`Age categories`$`75-84`
~qwraps2::n_perc0(.data$age_cat == 2)
$`Age categories`$`> 85`
~qwraps2::n_perc0(.data$age_cat == 3)
it looks like you've collected everything into a single list called 'Gender'. it appears your data format is gender[(male,female), mean_age, age_categories()]. so you have a list called genders containing an unnamed list, a numeric names mean_age, and a named list called age_categories.
#phi’s answer is correct. To explian in more detail: the summary is expected
to be a list of lists. That is, a list hwere each element is a list.
Let’s look at the structure of the provided summary: (EDIT: omitting
the .data pronoun as it is no longer recommened as of qwraps2 version
0.5.0, released 1 Sept 2020).
summary_tbl1 <-
list("Gender" =
list("Female" = ~ qwraps2::n_perc0(gender == 0),
"Male" = ~ qwraps2::n_perc0(gender == 1)
),
"Mean age (sd)" = ~ qwraps2::mean_sd(inage),
"Age categories" =
list("65-74" = ~ qwraps2::n_perc0(age_cat == 1),
"75-84" = ~ qwraps2::n_perc0(age_cat == 2),
"> 85" = ~ qwraps2::n_perc0(age_cat == 3)
)
)
str(summary_tbl1, max.level = 1)
#> List of 3
#> $ Gender :List of 2
#> $ Mean age (sd) :Class 'formula' language ~qwraps2::mean_sd(inage)
#> .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
#> $ Age categories:List of 3
The first and thrid elements are lists, but the second element is a formula.
The correct specification for the summary is:
summary_tbl1 <-
list("Gender" =
list("Female" = ~ qwraps2::n_perc0(gender == 0),
"Male" = ~ qwraps2::n_perc0(gender == 1)),
"inage" =
list("Mean age (sd)" = ~ qwraps2::mean_sd(inage)),
"Age categories" =
list("65-74" = ~ qwraps2::n_perc0(age_cat == 1),
"75-84" = ~ qwraps2::n_perc0(age_cat == 2),
"> 85" = ~ qwraps2::n_perc0(age_cat == 3))
)
str(summary_tbl1, max.level = 1)
#> List of 3
#> $ Gender :List of 2
#> $ inage :List of 1
#> $ Age categories:List of 3
Created on 2020-09-01 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_US.UTF-8
#> ctype en_US.UTF-8
#> tz America/Denver
#> date 2020-09-01
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date lib source
#> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.0)
#> backports 1.1.9 2020-08-24 [1] CRAN (R 4.0.2)
#> 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)
#> crayon 1.3.4 2017-09-16 [1] CRAN (R 4.0.0)
#> desc 1.2.0 2018-05-01 [1] CRAN (R 4.0.0)
#> 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.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)
#> fs 1.5.0 2020-07-31 [1] CRAN (R 4.0.2)
#> glue 1.4.2 2020-08-27 [1] CRAN (R 4.0.2)
#> highr 0.8 2019-03-20 [1] CRAN (R 4.0.0)
#> htmltools 0.5.0 2020-06-16 [1] CRAN (R 4.0.0)
#> knitr 1.29 2020-06-23 [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)
#> pkgbuild 1.1.0 2020-07-13 [1] CRAN (R 4.0.2)
#> pkgload 1.1.0 2020-05-29 [1] CRAN (R 4.0.0)
#> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.0)
#> processx 3.4.3 2020-07-05 [1] CRAN (R 4.0.0)
#> ps 1.3.4 2020-08-11 [1] CRAN (R 4.0.2)
#> R6 2.4.1 2019-11-12 [1] CRAN (R 4.0.0)
#> remotes 2.2.0 2020-07-21 [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.0)
#> rprojroot 1.3-2 2018-01-03 [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)
#> usethis 1.6.1 2020-04-29 [1] CRAN (R 4.0.0)
#> withr 2.2.0 2020-04-20 [1] CRAN (R 4.0.0)
#> xfun 0.16 2020-07-24 [1] CRAN (R 4.0.2)
#> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.0)
#>
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library