【问题标题】:R: ggplot2 images as y-axis labels (solved)R:ggplot2 图像作为 y 轴标签(已解决)
【发布时间】:2021-10-15 06:01:08
【问题描述】:

解决方法见文末!

我正在尝试将图像添加到 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 中的第二个示例图?
  • 感谢您的提示。我看过它,但不幸的是,它不起作用。
  • 你能告诉我们失败的原因吗?也许我们可以从那里提供帮助?
  • 我明白了。原帖中的解决方法! :) 谢谢大家!
  • jobischo - 请考虑自行回答,而不是在问题中提供解决方案。然后您可以接受自己的答案

标签: r ggplot2 plot markdown axis-labels


【解决方案1】:

解决方案

按照建议,我尝试使用 [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))

【讨论】:

    猜你喜欢
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 2015-03-13
    • 2016-12-03
    • 2012-04-17
    • 1970-01-01
    相关资源
    最近更新 更多