ggplot geom_point: how to set font of custom plotting symbols?

爷,独闯天下 提交于 2021-02-04 17:56:09

问题


With ggplot::geom_point we are able to set any character for plotting symbols using scale_shape_manual. I am coming with an example which demonstrates the purpose: use triangles to make a heatmap with two values in each cell:

require(ggplot2)

data <- data.frame(
    val = rnorm(40),
    grp = c(rep('a', 20), rep('b', 20)),
    x   = rep(letters[1:4], 5),
    y   = rep(letters[1:5], 4)
)

p <- ggplot(data, aes(x = x, y = y, color = val, shape = grp)) +
    geom_point(size = 18) +
    scale_shape_manual(values=c("\u25E4","\u25E2")) +
    theme_minimal() +
    theme(panel.grid = element_blank())

ggsave('triangle-tiles.pdf', device = cairo_pdf, width = 4.1, height = 3.5)

This works fine if the font used for the symbols has these special characters. Otherwise apparently fails. I am aware that we can explicitely define font and get the same result with geom_text:

require(dplyr)

data <- data %>% mutate(sym = ifelse(grp == 'a', "\u25E4", "\u25E2"))

p <- ggplot(data, aes(x = x, y = y, color = val, label = sym)) +
    geom_text(size = 18, family = 'DejaVu Sans') +
    theme_minimal() +
    theme(
        panel.grid = element_blank()
    )

ggsave('triangle-tiles-2.pdf', device = cairo_pdf, width = 4.1, height = 3.5)

However the fact that also in geom_point these are characters coming from a typeface makes me really curious what is the way to overwrite the default.

I checked the grob of this plot trying to follow this example and found the points this way:

gr <- ggplotGrob(p)
gr[['grobs']][[6]]$children$geom_point

Here we have x, y, size, lwd (used in example above) etc but no typeface. Also I am wondering how to find directly and automatically the grob of the points from the root grob, e.g. with grid::getGrob, e.g. in the cited example grid::grid.edit finds them.

I found some promising code here which uses editGtable, a method I could not find in any package, maybe is an old one. Then I tried editGrob with no success:

font <- gpar(fontfamily = 'DejaVu Sans', fontsize = 14)
editGrob(gr[['grobs']][[6]], 'geom_point.points', grep = TRUE, global = TRUE, gp = font)

来源:https://stackoverflow.com/questions/48531257/ggplot-geom-point-how-to-set-font-of-custom-plotting-symbols

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!