簡體   English   中英

在 plotly 中的條形圖或 R 中的 ggplot 中添加箭頭(或箭頭圖像)?

[英]Adding arrows (or image of arrow) onto bar chart in plotly or ggplot in R?

有沒有辦法根據一些基礎信息將箭頭圖像添加到條形圖的一側?

我有以下腳本:

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")

這會在 plotly 中生成一個簡單的條形圖,我想在每個條形圖旁邊添加一些箭頭,以顯示值根據data$change列減少或增加。 因此,如果數字為正,則箭頭朝上且呈綠色,如果為負,則箭頭朝下且呈紅色

這可能嗎?

如果這一切都不可能 - 有沒有辦法只覆蓋百分比變化的文本而不是條形圖旁邊?

希望這會將 go 放到 shiny 應用程序上,所以即使有一種嵌入或覆蓋 html 元素的方法也會有用!

如果 ggplot 有替代方案,我也會感興趣。

希望看起來像這樣:

是箭頭還是數字?

只是為了更新以下答案,代碼將是:

`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")
`

但是您不生產相同的 output - 只出現一個箭頭?

您可以添加注釋。 首先指定箭頭的開始和結束位置:請注意,在您提供的數據中,您有 yahoo 減少,而不是像 plot 中的 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"))

然后 plot 使用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")

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM