Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

Plotly-R: How to make a gapped y axis?

Is it possible to create a plotly bar chart, e.g. any chart from the following website: https://plotly.com/r/bar-charts/ but with the gapped (broken) Y axis? An example from (ggplot2, I believe) attached below: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To my knowledge, plotly hasn't got any built-in functionality to do this. But it's still possible to make a figure that matches your image using subplots if you:

  1. use subplot(fig1, fig2, nrows = 2, shareX=TRUE, margin = <low>), and

  2. adjust the y axes for figure positions [1, 1] and [2, 1] to respectively start and end with your desired cutoff values in a defined interval.

Plot:

enter image description here

Complete code:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...