如何使用 unicode 箭头导出高分辨率 ggplot 图形

How to export high resolution ggplot graphic with unicode arrows

我正在努力导出以下带有 Unicode 箭头的 ggplot2 图形(图例)。 这是 Plot Zoom 输出。

用于创建此图例的代码如下:

legend <- ggplot() +
  geom_tile(
    data = bivariate_color_scale,
    mapping = aes(
      x = gini,
      y = mean,
      fill = fill)
  ) +
  scale_fill_identity() +
  labs(y ="Higher bus. density →",
       x = "Higher patent growth →", 
       title = "Legend") +
  theme_minimal()+
  theme(
    panel.grid.minor = element_blank(), 
    panel.grid.major = element_blank(), 
    axis.line = element_blank(),
    axis.ticks = element_blank(),
    panel.border = element_blank(),
    axis.text.x = element_blank(),
    axis.text.y = element_blank() 
    ) +
  # make font small enough
  theme(axis.title = element_text(size = 8)) +
  # quadratic tiles
  coord_fixed()

我尝试使用以下两个代码导出此图例(高分辨率):

ggsave(filename="legend.png", 
       plot = legend,
       dpi= 300,
       width = 210, 
       height = 180, 
       units = "mm")

和...

ggsave(filename="legend.pdf",
       plot = legend, 
       dpi = 300, 
       device = cairo_pdf,
       width = 210, 
       height = 180, 
       units = "mm"
       )

两种代码都没有正确 translate/save Unicode 箭头(请参阅以下输出)。

有人建议我如何导出带有 Unicode 箭头的高分辨率图形吗? 提前谢谢你。


数据

bivariate_color_scale <- data.frame(gini = rep(1:3, 3), mean = rep(1:3, each = 3),
                                    fill = c("#beafc4", "#ae667c", "#9c263c",
                                             "#778fbd", "#6d5577", "#63233b",
                                             "#3970b3", "#354374", "#301d39"))

要显示 Unicode 字符,您可以使用 expression(),或(我的偏好)使用 sprintf(),然后使用序言 \u 调用与文本内联的字符。请参阅以下示例:

df <- data.frame(x=1:10, y=1:10)

ggplot(df, aes(x,y)) + geom_point() +
  labs(
    title=paste("This Title has an up arrow!", sprintf('\u2191')),
    x=paste('The x axis goes this way:', sprintf('\u2794')),
    y=paste('The y axis is this way:',sprintf('\u2190'))
  )

根本问题是您需要使用实际包含您要显示的 unicode 符号的字体。您没有指定特定字体,因此图形设备可以自由选择,png()pdf() 都选择没有您的符号的 Helvetica。交互设备可能选择了 Arial(你在 Windows 吗?),它确实有符号。

# code setup
library(ggplot2)

bivariate_color_scale <- data.frame(gini = rep(1:3, 3), mean = rep(1:3, each = 3),
                                    fill = c("#beafc4", "#ae667c", "#9c263c",
                                             "#778fbd", "#6d5577", "#63233b",
                                             "#3970b3", "#354374", "#301d39"))

legend <- ggplot() +
  geom_tile(
    data = bivariate_color_scale,
    mapping = aes(
      x = gini,
      y = mean,
      fill = fill)
  ) +
  scale_fill_identity() +
  labs(y ="Higher bus. density →",
       x = "Higher patent growth →", 
       title = "Legend") +
  theme_minimal()+
  theme(
    panel.grid.minor = element_blank(), 
    panel.grid.major = element_blank(), 
    axis.line = element_blank(),
    axis.ticks = element_blank(),
    panel.border = element_blank(),
    axis.text.x = element_blank(),
    axis.text.y = element_blank() 
  ) +
  # quadratic tiles
  coord_fixed()
# with Helvetica the symbol doesn't show
legend +
  theme(axis.title = element_text(family = "Helvetica"))

# with Arial the symbol shows
legend +
  theme(axis.title = element_text(family = "Arial"))

reprex package (v0.3.0)

于 2020-06-27 创建