ggplot2¶
Paneles de figuras¶
Con patchwork es lindo: https://ggplot2-book.org/arranging-plots.html
library(ggplot2)
library(patchwork)
p1 <- ggplot(iris, aes(x=Species, y=Sepal.Length))+ geom_violin()
p2 <- ggplot(iris, aes(x=Sepal.Width, y=Sepal.Length))+ geom_point()
p1 + p2
Con ggarrange y grid extra el códogo queda igual de feo que siempre, pero hay más control seguramente.
- https://rpkgs.datanovia.com/ggpubr/reference/ggarrange.html
- https://bookdown.org/rdpeng/RProgDA/the-grid-package.html
- https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html
Ejemplo de ordenar plots en varios paneles, varios anidados:
pans <- list(
# Panels A and B
pan.sub.AB = ggarrange(ncol=2,
# Panel A
pan.sub.A = ggarrange(p$f2A.left, p$f2A.right,
ncol = 1, nrow = 2, labels = c("A", ""),
align = "h"), # https://stackoverflow.com/a/48004459
# Panel B
pan.sub.B = ggarrange(p$f2B.left, p$f2B.right,
ncol = 1, nrow = 2, labels = c("B", ""),
align = "h") # https://stackoverflow.com/a/48004459
),
# Panel C
pan.sub.C = ggarrange(
left = ggarrange(ncol=1,
p$f2C.top_left,
p$f2C.bot_left),
right = f2.null,
ncol = 2, nrow = 1, labels = c("C", "")
),
# Panel D placeholder
pan.sub.D = ggarrange(f2.null,
ncol = 2, nrow = 1, labels = "D"),
pan.sub.E = ggarrange(nrow = 2,
f2.null,
ggarrange(ggarrange(p$p2C.bot_right.a,
p$p2C.bot_right.b,
ncol = 2),
f2.null,
widths = c(0.9, 0.1)),
labels = c("E", ""))
)
# To pass a list of ggarrange objects, instead of simple arguments, use "plotlist".
fig2 <- ggarrange(
plotlist=pans,
nrow = 2, ncol = 2
)
fig2
Exportar figuras¶
Notas sobre exportar figuras desde R, y usarlas en otro lado.
Usualmente un dolor de cabeza.
Figma¶
Problemas:
Hay alternativas open source: https://itsfoss.com/figma-alternatives/
El export de R es malo y el import de Figma es malo también. Excelente combinación.
Cosas que pueden funcionar:
- Probar primero con drag-and-drop del SVG sobre el área de trabajo de Figma.
- Postié en el foro de Figma con una forma medio fiaca de que se importen bien, usando Inkscape:
- Opening the R SVG plot in Inkscape,
- For preserving text as
- ungroup all elements (using “Ctrl+Shift+G"),
- select a text element and select same object type (Edit → Select Same → Object Type; or “Alt+Shift+A").
- invert the selection (using Edit → Object to path; or “!”),
- Convert selected objects to paths (using Path → Object to path, or "Ctr+Shift+C").
Para que la figura tenga las dimensiones correctas hay que:
- Especificar las unidades en pixeles en ggsave.
- Usar
dpi=96
en ggsave.
Hice una función para automatizar esto:
- Usa ggsave para guardar la figura como PDF.
- Usa Inkscape desde la linea de comandos para convertir un PDF a SVG.
- Ver: https://gitlab.com/naikymen/r_utils/-/blob/master/R/plot_exports.R
Penpot et al¶
Otro illustrator online: https://penpot.app/
Limites del gráfico¶
Hay una función horrible para obtener los límites del grafico, pero solo de los datos: layer_scales(p)$x$get_limits()
.
Sin emabrgo, ggplot "expande" los límites del gráfico por defecto, en alguna cantidad misteriosa.
¿Alguien saber qué es "wavier()"?
Para usar los límites hay que cambiar el default de esa expansion a algo conocido, y sumarselo al range de los datos.
Por ejemplo:
p <- datos %>%
ggplot(aes(round(t.drop), t.bud, text=ucid)) +
geom_segment(x = min(datos$t.drop)-1, y = min(datos$t.drop)-1,
xend = max(datos$t.drop)+1, yend = max(datos$t.drop)+1,
color = "gray") +
# geom_abline(slope = 1, intercept = 0, color = "black") +
geom_point(alpha = .5, color = "#009E73") +
theme_bw() +
scale_x_continuous(expand = expansion(add = 1)) +
scale_y_continuous(expand = expansion(add = 1))
Y eso genera esto:
Ahora... ¿por qué no usar geom_abline?
La respuesta es esto:
abline genera una recta mucho más larga que el gráfico y la clippea después. Lousy coding...