在R中的轴标签中绘制表情符号/自定义图像 [英] plot emoji/custom image in axis labels in R
问题描述
我正在尝试将R中的表情符号和自定义图片绘制为X轴的标签。
我已经阅读了类似的主题和
在R中是否有使用.png代替文本或emojifont标签的标签?
数据,我已经:
df.plot
description n dens rank xsize ysize
< fct> < int> < dbl> < dbl> < dbl> < dbl>
1哭泣的脸庞1207 1.5 8 9.62 7.22
2双重感叹号1326 1.6 7 9.62 7.22
3带着喜悦的眼泪39122 48.1 1 9.62 7.22
4咧着嘴笑的脸871 1.1 10 9.62 7.22
5笑脸咧着嘴的脸1872 2.3 4 9.62 7.22
6拥抱的脸1401 1.7 6 9.62 7.22
7百分2998 3.7 3 9.62 7.22
8大声哭泣的脸13375 16.4 2 9.62 7.22
9派对装扮1522 1.9 5 9.62 7.22
10疲倦的面孔974 1.2 9 9.62 7.22
和g指的是图像:
imgs<-lapply(paste0(df.plot $说明,'.png'),png :: readPNG);
g<-lapply(imgs,grid :: rasterGrob);
我找到了解决方案。
类似问题的答案在
I'm trying to plot Emojis and custom images in R as labels of X axis. I've read similar threads and questions, but I don't want to use emojifont in R, and instead use my own images as label (.png) and there's around 270 of these custom emojis.
I followed this article and managed to show emojis at top of bars, but I want the emojis as labels. similar to this image.
The only solution that came to my mind was changing value of density in mapply (df.plot$dens) to 1, in this code:
...
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)))
...
Therefore, the code is :
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;
and the results is :
Is there anyway to use .png as labels instead of text or emojifont, in R?
As for data, I've :
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
and g is referring to images:
imgs <- lapply(paste0(df.plot$description, '.png'), png::readPNG);
g <- lapply(imgs, grid::rasterGrob);
I found the solution. The answer to similar question is given in photo alignment with graph in r and here.
We need to create a function
my_axis = function(img) {
structure(
list(img=img),
class = c("element_custom","element_blank", "element") # inheritance test workaround
)
}
To make it look better I suggest to use rot=45 to rotate the labels
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")
}
and then call it:
gg <- gg + theme(axis.text.x = my_axis(pics),
axis.text.y = element_text(size=14),
axis.title.x = element_blank())
The output :
这篇关于在R中的轴标签中绘制表情符号/自定义图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!