r - 当 x 轴离散时,R 中的水平线

标签 r plotly r-plotly

我想创建一个条形图(使用plotly包),它(在几个月内)会有红色水平线 (增益获得)。我希望下面的图能够更准确地显示我的问题。

enter image description here

获取第一个图所需的代码和数据:

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轴标签,但我不知道如何更改这些标签(我试图找到它当然,首先是在谷歌中)...

非常感谢您的帮助!

最佳答案

类似的东西对你有用吗?您可以调整 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) 

关于r - 当 x 轴离散时,R 中的水平线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40995621/

相关文章:

r - RMarkdown 中的命令是 "source"并显示现有 .R 文件中的代码?

python - 如何在带有子图的绘图中设置辅助x轴及其范围?

r - 如何使用函数 ggplot() 和 plot_ly 增加 R 中轴标签和轴标题之间的空间?

html - 如何调整包含plotly的div以绘制尺寸,使其不与文档重叠

返回在绘图散点图中选择的数据点

r - 将 data.frame 扩展为长格式并增加值

R Markdown 标题/正文间距格式

r - 带有缺失值的 dplyr 交叉表

r - 工具提示删除回归线 ggplotly

python - Jupyter Notebook 中未返回 Plotly 表