【问题标题】:Plotly-R: How to make a gapped y axis?Plotly-R:如何制作有间隙的 y 轴?
【发布时间】:2021-04-16 13:04:31
【问题描述】:

是否可以创建一个绘图条形图,例如以下网站的任何图表: https://plotly.com/r/bar-charts/ 但是有间隙(断开)的Y轴? 下面附上一个来自(ggplot2,我相信)的例子:

【问题讨论】:

  • 我急于帮助您,并且由于您的原始问题中缺少[R] 标签,我完全错过了这是不是 [Python]由于提供的链接,问题更可能和[R] 问题。所以我继续写了一个[Python] 答案。由于您不太可能对此感兴趣,但其他人可能会对此感兴趣,因此我也冒昧地在 [Python] 下重新发布您的问题。我最终会看到[R] 的方法,但希望[R] 的一些专业人士能打败我。
  • 如果您已经在 ggplot2 中拥有它但希望它具有交互性,您可以尝试使用函数 plotly::ggplotly() 将您的 ggplot 转换为 plotly。
  • @vestland ,你能把 Pyhton 答案的链接发给我吗?我经常在 R 和 Pyhton 之间切换。非常感谢你帮助我!我找到了:stackoverflow.com/questions/65766960/…
  • @jojo 你对R的类似方法感到满意吗?

标签: r plotly bar-chart


【解决方案1】:

据我所知,plotly 没有任何内置功能可以做到这一点。但是,如果您:

  1. 使用subplot(fig1, fig2, nrows = 2, shareX=TRUE, margin = <low>),并且

  2. 调整图形位置 [1, 1][2, 1] 的 y 轴,分别以定义的间隔内所需的截止值开始和结束。

剧情:

完整代码:

library(dplyr)
library(plotly)


years <- c(1995, 1996, 1997, 1998, 1999, 2000,2001, 2002, 2003, 2004, 2005, 2006,2007, 2008, 2009, 2010, 2011, 2012)
China <-  c(219, 146, 112, 127, 124, 180, 236,207, 236, 263,350, 430, 474, 1526,488, 537, 500, 439)
Rest_of_World <- c(16, 13, 10, 11, 28, 37,43, 55, 56, 88, 105, 156, 270,299, 340, 403, 549, 1499)
df <- data.frame(years, China, Rest_of_World)

colors <- c('rgb(31, 119, 180)',
 'rgb(255, 127, 14)',
 'rgb(44, 160, 44)',
 'rgb(214, 39, 40)',
 'rgb(148, 103, 189)',
 'rgb(140, 86, 75)',
 'rgb(227, 119, 194)',
 'rgb(127, 127, 127)',
 'rgb(188, 189, 34)',
 'rgb(23, 190, 207)')

cutinterval = c(600, 1400)

fig1 <- plot_ly(df, type = 'bar')
fig1 <- fig1 %>% add_trace(x=years, y=China, #yaxis = list(range = c(0, 400)),
                           marker=list(color = 'red'),
                           name = 'China',
                           legendgroup='China')
fig1 <- fig1 %>% add_trace(x=years, y=Rest_of_World,
                           marker=list(color = 'blue'),
                           name = 'Rest of World',
                           legendgroup='Rest_of_World')

fig2 <- plot_ly(df, type = 'bar')
fig2 <- fig2 %>% add_trace(x=years, y=China, #yaxis = list(range = c(1400, 1600)),
                           marker=list(color = 'red'),
                           legendgroup='China',
                           showlegend=FALSE)
fig2 <- fig2 %>% add_trace(x=years, y=Rest_of_World,
                           marker=list(color = 'blue'),
                           legendgroup='Rest_of_World',
                           showlegend=FALSE)
list(range = c(0, cutinterval[1]))

fig1 <- fig1 %>% layout(barmode = 'group')
fig <- subplot(fig1, fig2,nrows = 2, shareX=TRUE, margin = 0.02)

fig <- layout(fig, yaxis2 = list(range = c(0, cutinterval[1])),
                   yaxis = list(range = c(cutinterval[2], 1600))
                   #shapes = list(hline(1500))
              )


fig

【讨论】:

  • @jojo 我的建议对你有什么效果?
  • 是的,它运行良好。我实际上开发了您的代码,对其进行了定制,也旋转了可视化。我一直打算有空的时候把重新开发的代码放在这里。
  • @jojo 酷!您会考虑将我的建议标记为已接受的答案吗?
  • 当然,是的,我认为这是公认的答案
  • @jojo 谢谢!非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-14
  • 2017-12-16
  • 1970-01-01
  • 2021-03-10
  • 2019-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多