[英]Adding arrows (or image of arrow) onto bar chart in plotly or ggplot in R?
Is there a way to add an arrow image onto the side of a barchart, based on some underlying information?有没有办法根据一些基础信息将箭头图像添加到条形图的一侧?
I have the following script:我有以下脚本:
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")
This produces a simple bar chart in plotly, I would like to add some arrows next to each bar chart to show that the values have either decreased or increased based on the data$change
column.这会在 plotly 中生成一个简单的条形图,我想在每个条形图旁边添加一些箭头,以显示值根据
data$change
列减少或增加。 so if the number is positive a arrow turned up and green, if negative, then an arrow that is red and pointed down因此,如果数字为正,则箭头朝上且呈绿色,如果为负,则箭头朝下且呈红色
Is this possible?这可能吗?
If none of this possible - is there a way to overlay just the text of the percentage change instead next to the bar charts?如果这一切都不可能 - 有没有办法只覆盖百分比变化的文本而不是条形图旁边?
Hopefully this would go onto a shiny app, so even if there is a way of embedding or overlaying a html element would be useful!希望这会将 go 放到 shiny 应用程序上,所以即使有一种嵌入或覆盖 html 元素的方法也会有用!
If there is an alternative in ggplot, I would also be interested.如果 ggplot 有替代方案,我也会感兴趣。
Hopefully something that looks like this:希望看起来像这样:
JUST TO UPDATE IN REGARDS TO BELOWS ANSWER THE CODE WOULD BE:只是为了更新以下答案,代码将是:
`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")
`
But you do not produce the same output - only one arrow appears?但是您不生产相同的 output - 只出现一个箭头?
You can add annotations.您可以添加注释。 First specify arrow start and end positions: Please note that in the data you provide you have yahoo decreasing, not yandex like in your plot.
首先指定箭头的开始和结束位置:请注意,在您提供的数据中,您有 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"))
Then plot using add_annotations
然后 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.