繁体   English   中英

R:ggplot2 图像作为 y 轴标签(已解决)

[英]R: ggplot2 images as y-axis labels (solved)

解决方案在文末!

我正在尝试将图像添加到 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*"

之后,可以更改标签并使用 Markdown 打印:

p +   scale_x_discrete(name = NULL,
                       labels = rev(labels)) +
  theme(axis.text.y = element_markdown(color = "black", size = 11))

在此处输入图片说明

暂无
暂无

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

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