在R中的plotly或ggplot中的柱状图上添加箭头(或箭头的图像)?

问题描述 投票:1回答:1

有没有办法根据一些基础信息,在柱状图的侧面添加一个箭头图像?

我有以下脚本。

library(tidyverse)
library(plotly)

data <- tibble(url = c("google.com","yahoo.com","yandex.com"), values = c(500,400,300), change = c(0.5,-0.9,0.1))

data

data %>%
  plot_ly(x = data$values,
          y = data$url,
          type = "bar")

我想在每个柱状图旁边添加一些箭头,以显示数值是减少了还是增加了。data$change 所以,如果数字是正数,则一个箭头向上,绿色,如果是负数,则一个箭头是红色的,并指向下。

这可能吗?

如果这些都不可能--是否有办法只将百分比变化的文字覆盖在柱状图旁边?

希望这将会是一个闪亮的应用程序,所以即使有一个嵌入或覆盖一个html元素的方法将是有用的!如果在ggplot中有一个替代方案,我也会感兴趣。

如果在ggplot中有其他的选择,我也会感兴趣。

希望能有类似这样的东西。

Either an arrow or a number?

更新一下下面的答案,代码是:

`library(tidyverse)
library(plotly)

data <- tibble(url = c("google.com","yahoo.com","yandex.com"), values = c(500,400,300), change = c(0.5,-0.9,0.1))

data

data %>%
  plot_ly(x = data$values,
          y = data$url,
          type = "bar")


library(dplyr)
data <- data %>% 
  mutate(x.start = values + 50,
         y.end = seq(0,2,1),
         y.start = y.end + 0.5) %>% 
  mutate(y.start.new = case_when(sign(change) == -1 ~ y.end,
                                 TRUE ~ y.start),
         y.end.new = case_when(sign(change) == -1 ~ y.start,
                               TRUE ~ y.end)
  )


data %>%
  plot_ly(x = data$values,
          y = data$url,
          type = "bar") %>% 
  add_markers(~values, ~url) %>%
  add_annotations(x = ~x.start[change == "up"],
                  y = ~y.start.new[change == "up"],
                  xref = "x", yref = "y",
                  axref = "x", ayref = "y",
                  text = "",
                  showarrow = T,
                  ax = ~x.start[change == "up"],
                  ay = ~y.end.new[change == "up"],
                  arrowcolor = "green") %>% 
  add_annotations(x = ~x.start[change == "down"],
                  y = ~y.start.new[change == "down"],
                  xref = "x", yref = "y",
                  axref = "x", ayref = "y",
                  text = "",
                  showarrow = T,
                  ax = ~x.start[change == "down"],
                  ay = ~y.end.new[change == "down"],
                  arrowcolor = "red")
`

但你不产生相同的输出 - 只有一个箭头出现?

r ggplot2 plotly
1个回答
1
投票

您可以添加注释。 首先指定箭头的开始和结束位置。 请注意,在你提供的数据中,你有雅虎递减,而不是像你的情节中的yandex。

library(dplyr)
data <- data %>% 
  mutate(x.start = values + 50,
         y.end = seq(0,2,1),
         y.start = y.end + 0.5) %>% 
  mutate(y.start.new = case_when(sign(change) == -1 ~ y.end,
                                 TRUE ~ y.start),
         y.end.new = case_when(sign(change) == -1 ~ y.start,
                               TRUE ~ y.end)
         ) %>% 
   mutate(change_dir = case_when(sign(change) == -1 ~ "down",
                                sign(change) == 1 ~ "up"))

然后使用 add_annotations

data %>%
  plot_ly(x = data$values,
          y = data$url,
          type = "bar") %>% 
  add_markers(~values, ~url) %>%
  add_annotations(x = ~x.start[change_dir == "up"],
                   y = ~y.start.new[change_dir == "up"],
                   xref = "x", yref = "y",
                   axref = "x", ayref = "y",
                   text = "",
                   showarrow = T,
                   ax = ~x.start[change_dir == "up"],
                   ay = ~y.end.new[change_dir == "up"],
                  arrowcolor = "green") %>% 
  add_annotations(x = ~x.start[change_dir == "down"],
                  y = ~y.start.new[change_dir == "down"],
                  xref = "x", yref = "y",
                  axref = "x", ayref = "y",
                  text = "",
                  showarrow = T,
                  ax = ~x.start[change_dir == "down"],
                  ay = ~y.end.new[change_dir == "down"],
                  arrowcolor = "red")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.