繁体   English   中英

在R中的轴标签中绘制表情符号/自定义图像

[英]plot emoji/custom image in axis labels in R

我正在尝试将R中的表情符号和自定义图像绘制为X轴的标签。 我已经读过类似的主题和问题 ,但是我不想在R中使用emojifont,而是使用自己的图片作为标签(.png),这些自定义表情符号大约有270种。

我关注了这篇文章,并设法在条形图的顶部显示表情符号,但我希望将表情符号作为标签。 与此图像相似。

在此处输入图片说明

我想到的唯一解决方案是在以下代码中将mapply(df.plot $ dens)中的density值更改为1:

...
mapply(function(x, y, i) {
          annotation_custom(g[[i]], xmin = x-0.5*df.plot$xsize[i], xmax = x+0.5*df.plot$xsize[i], 
                            ymin = y-0.5*df.plot$ysize[i], ymax = y+0.5*df.plot$ysize[i])},
          df.plot$rank, df.plot$dens, seq_len(nrow(df.plot)))
...

因此,代码为:

g1 <- ggplot(data = df.plot, aes(x = rank, y = dens)) +
  geom_bar(stat = 'identity', fill = 'dodgerblue4') +
  xlab(xlab) + ylab(ylab) +
  mapply(function(x, y, i) {
    annotation_custom(g[[i]], xmin = x-0.5*df.plot$xsize[i], xmax = x+0.5*df.plot$xsize[i], 
                      ymin = y-0.5*df.plot$ysize[i], ymax = y+0.5*df.plot$ysize[i])},
    df.plot$rank, 1, seq_len(nrow(df.plot))) +
  scale_x_continuous(expand = c(0, 0), breaks = seq(1, nrow(df.plot), 1), labels = seq(1, nrow(df.plot), 1)) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 1.10 * max(df.plot$dens))) +
  theme(panel.grid.minor.y = element_blank(),
        axis.title.x = element_text(size = 10), axis.title.y = element_text(size = 14), 
        axis.text.x  = element_text(size = 8, colour = 'black'), axis.text.y  = element_text(size = 8, colour = 'black'));
g1;

结果是: 在此处输入图片说明

无论如何,在R中是否使用.png作为标签而不是文本或emojifont?

至于数据,我已经:

 df.plot

   description                         n  dens  rank xsize ysize
   <fct>                           <int> <dbl> <dbl> <dbl> <dbl>
 1 crying face                      1207   1.5     8  9.62  7.22
 2 double exclamation mark          1326   1.6     7  9.62  7.22
 3 face with tears of joy          39122  48.1     1  9.62  7.22
 4 grinning face                     871   1.1    10  9.62  7.22
 5 grinning face with smiling eyes  1872   2.3     4  9.62  7.22
 6 hugging face                     1401   1.7     6  9.62  7.22
 7 hundred points                   2998   3.7     3  9.62  7.22
 8 loudly crying face              13375  16.4     2  9.62  7.22
 9 party popper                     1522   1.9     5  9.62  7.22
10 tired face                        974   1.2     9  9.62  7.22

g表示图像:

imgs <- lapply(paste0(df.plot$description, '.png'), png::readPNG); 
g <- lapply(imgs, grid::rasterGrob);

我找到了解决方案。 相似问题的答案在r此处的 图形与照片对齐中给出。

我们需要创建一个功能

my_axis = function(img) {
  structure(
    list(img=img),
    class = c("element_custom","element_blank", "element") # inheritance test workaround
  )
}

为了使其看起来更好,我建议使用rot = 45旋转标签

   element_grob.element_custom <- function(element, x,...)  {
  stopifnot(length(x) == length(element$img))
  tag <- names(element$img)

  # add vertical padding to leave space
  g1 <- textGrob(paste0(tag, "\n\n\n\n\n"), x=x, vjust=0.6,rot = 45)
  g2 <- mapply(rasterGrob, x=x, image=element$img[tag], 
               MoreArgs=list(vjust=0.6, interpolate=FALSE,
                             height=unit(3,"lines")),
               SIMPLIFY=FALSE)
  gTree(children=do.call(gList, c(g2, list(g1))), cl="custom_axis")
}

然后调用它:

gg <- gg + theme(axis.text.x  = my_axis(pics),
                 axis.text.y  = element_text(size=14),
                 axis.title.x = element_blank())

输出 :

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM