简体   繁体   中英

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

Solution at the end of the post!

I am trying to add images to a y-axis label. At the moment I am only able to add them inside the graph. You can find the code for the added images at the bottom of the code chunk. I want the flags to be displayed after or under or on top of the country name. Does anybody know how to do it or where I can find a tutorial? Could not find one googling. Thank you!

在此处输入图片说明

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

SOLUTION

As suggested I have tried to add the images with the [ggtext][2] tutorial. First I had to make the label vector with the HTML code:

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,"*"))
  
}

Example image code:

"<img src='../pics/twitter-emojis/flag-cote-divoire_1f1e8-1f1ee.png'
    width='100' /><br>*I. virginica*"

After that the labels can be changed and printed with markdown:

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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