r:作为y轴标签的ggplot2图像(已求解) [英] R: ggplot2 images as y-axis labels (solved)
本文介绍了r:作为y轴标签的ggplot2图像(已求解)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
帖子末尾的解决方案!
我正在尝试将图像添加到y轴标签。目前我只能将它们添加到图表中。您可以在代码块的底部找到添加图像的代码。我希望国旗显示在国家名称之后、下面或上面。有没有人知道怎么做,或者我在哪里可以找到教程?找不到一个在谷歌上搜索的。谢谢!
p <- ggplot(data, aes(x = country, y = thisyear)) +
geom_segment(aes(
x = reorder(country, thisyear) ,
xend = country,
y = lastyear,
yend = thisyear
),
color = "#3b3b3b") +
geom_point(size = 3, color = "#f7931b") +
geom_point(aes(x = country, y = lastyear), color = "#BCBCBC", size = 4) +
geom_point(aes(x = country, y = thisyear), color = "#f7931b", size = 4) +
annotate(
"text",
label = "this year",
x = nrow(data) - 0.7,
y = data[2, 3] + 3,
size = 4,
color = "#f7931b",
fontface = "bold"
) +
geom_curve(
aes(
x = nrow(data) - 0.85,
y = data[2, 3] + 3,
xend = nrow(data) - 1,
yend = data[2, 3] + 0.5
),
colour = "#f7931b",
size = 1,
curvature = -0.2,
arrow = arrow(length = unit(0.015, "npc"))
) +
annotate(
"text",
label = "last year",
x = nrow(data) - 1.5,
y = data[2, 2] + 3.2,
size = 4,
color = "#A8A8A8",
fontface = "bold"
) +
geom_curve(
aes(
x = nrow(data) - 1.35,
y = data[2, 2] + 3.2,
xend = nrow(data) - 1.05,
yend = data[2, 2] + 0.5
),
colour = "#A8A8A8",
size = 1,
curvature = -0.15,
arrow = arrow(length = unit(0.015, "npc"))
) +
scale_y_continuous(expand = expansion(mult = c(0, .05))) +
coord_flip() +
theme_ipsum() +
theme(
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank(),
legend.position = "none"
) +
labs(
title = "Share Of Global Bictoin Hashrate",
subtitle = paste0(as.character(format(maxdate, "%B %Y")), " Monthly Average"),
x = "",
y = '%',
caption = "@data99076083 | Source: Cambridge Centre for Alternative Finance (https://www.cbeci.org/mining_map)"
) +
theme_ipsum() +
theme(
legend.title = element_blank(),
plot.title = element_text(color = "#f7931b"),
plot.subtitle = element_text(color = "#3b3b3b"),
plot.caption = element_text(color = "#646464", face = 'bold'),
panel.border = element_rect(
colour = "grey",
fill = NA,
size = 1
)
)
p <-
p + geom_image(data = data, aes(x = id, y = 70, image = emoji), size = 0.04)
p
推荐答案
解决方案
如建议的那样,我已尝试使用[ggtext][2]教程添加图像。首先,我必须使用HTML代码创建标签向量:labels <- c()
for (i in 1:length(data$emoji)){
img.name <- data$country[i]
labels <- c(labels, paste0("<img src='", data$emoji[i], "' width='25' /><br>*", img.name,"*"))
}
示例图片代码:
"<img src='../pics/twitter-emojis/flag-cote-divoire_1f1e8-1f1ee.png'
width='100' /><br>*I. virginica*"
之后可以更改标签并使用降价打印:
p + scale_x_discrete(name = NULL,
labels = rev(labels)) +
theme(axis.text.y = element_markdown(color = "black", size = 11))
这篇关于r:作为y轴标签的ggplot2图像(已求解)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文