R 中的水平线,当 x 轴离散时
Horizontal line in R plotly, when xaxis is discrete
我想创建一个 bar plot
(使用 plotly
包),它(几个月)会有 red horizontal line
(获得收益)。我希望下面的图能更准确地显示我的问题。
获得第一个图所需的代码和数据:
library("plotly")
library("dplyr")
data.frame(miesiac_label = as.character(as.roman(c(1:12))),
miesiac = c(1:12),
ile = c(12000, 12100, 11100, 12000, 12000, 11900, 12200, 12100, 6000, 12100, 12100, 12100),
gain = c(rep(NA, 7), 11000, 12000, 12000, 12000, 12000)) -> dane
dane$miesiac_label <- factor(dane$miesiac_label, levels = dane[["miesiac_label"]])
plot_ly(dane) %>%
add_trace(x = ~miesiac_label, y = ~ile,
type = 'bar', marker = list(color = '#99d3df')) %>%
add_trace(x = ~miesiac_label, y = ~gain, name = 'Gain',
type = "scatter", mode='lines+markers', marker = list(color = 'red'),
line = list(color = 'red'))
我认为我应该有一个连续的比例尺来做这个,然后改变 x axis labels
,但我不知道如何改变这些标签(我试图在 google 首先,当然)...
非常感谢您的帮助!
我已经设法使用 ggplot
和奇妙的 ggplotly()
构建了您想要的东西
正常执行 ggplot 标准会导致悬停时出现可怕的工具提示,但这可以通过 text
美学和 ggplotly
调用中的 tooltip
参数进行调整
例如:
ggplot(dane, aes(x = miesiac_label, y = ile)) +
geom_bar(aes(text = paste("x:", miesiac_label, "y:",ile)),
stat = "identity", fill = "#99d3df") +
geom_segment(aes(x = miesiac - 0.5, xend = miesiac + 0.5,
y = gain, yend = gain,
text = paste0("gain: ",gain))
, colour = "red"
, linetype = 2)
ggplotly(tooltip = "text")
结果如下图:
类似的东西对你有用吗?您可以调整 add_segments
.
中的数字
a <- list(
title = "miesiac_label",
showticklabels = TRUE,
tickmode= "array",
ticktext = as.character(as.roman(c(1:12))),
tickvals = c(1:12)
)
plot_ly(dane) %>%
add_bars(x = ~miesiac, y = ~ile) %>%
add_segments(x = 7.5, xend = 8.5, y = 10000, yend = ~10000, line = list(dash = "dash")) %>%
add_segments(x = 8.5, xend = 12.5, y = 12000, yend = ~12000, line = list(dash = "dash")) %>%
layout(showlegend = FALSE, xaxis = a)
我想创建一个 bar plot
(使用 plotly
包),它(几个月)会有 red horizontal line
(获得收益)。我希望下面的图能更准确地显示我的问题。
获得第一个图所需的代码和数据:
library("plotly")
library("dplyr")
data.frame(miesiac_label = as.character(as.roman(c(1:12))),
miesiac = c(1:12),
ile = c(12000, 12100, 11100, 12000, 12000, 11900, 12200, 12100, 6000, 12100, 12100, 12100),
gain = c(rep(NA, 7), 11000, 12000, 12000, 12000, 12000)) -> dane
dane$miesiac_label <- factor(dane$miesiac_label, levels = dane[["miesiac_label"]])
plot_ly(dane) %>%
add_trace(x = ~miesiac_label, y = ~ile,
type = 'bar', marker = list(color = '#99d3df')) %>%
add_trace(x = ~miesiac_label, y = ~gain, name = 'Gain',
type = "scatter", mode='lines+markers', marker = list(color = 'red'),
line = list(color = 'red'))
我认为我应该有一个连续的比例尺来做这个,然后改变 x axis labels
,但我不知道如何改变这些标签(我试图在 google 首先,当然)...
非常感谢您的帮助!
我已经设法使用 ggplot
和奇妙的 ggplotly()
正常执行 ggplot 标准会导致悬停时出现可怕的工具提示,但这可以通过 text
美学和 ggplotly
调用中的 tooltip
参数进行调整
例如:
ggplot(dane, aes(x = miesiac_label, y = ile)) +
geom_bar(aes(text = paste("x:", miesiac_label, "y:",ile)),
stat = "identity", fill = "#99d3df") +
geom_segment(aes(x = miesiac - 0.5, xend = miesiac + 0.5,
y = gain, yend = gain,
text = paste0("gain: ",gain))
, colour = "red"
, linetype = 2)
ggplotly(tooltip = "text")
结果如下图:
类似的东西对你有用吗?您可以调整 add_segments
.
a <- list(
title = "miesiac_label",
showticklabels = TRUE,
tickmode= "array",
ticktext = as.character(as.roman(c(1:12))),
tickvals = c(1:12)
)
plot_ly(dane) %>%
add_bars(x = ~miesiac, y = ~ile) %>%
add_segments(x = 7.5, xend = 8.5, y = 10000, yend = ~10000, line = list(dash = "dash")) %>%
add_segments(x = 8.5, xend = 12.5, y = 12000, yend = ~12000, line = list(dash = "dash")) %>%
layout(showlegend = FALSE, xaxis = a)