Reputation: 35
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:
Upvotes: 2
Views: 4135
Reputation: 41
Here is update with ggplot
, that you can view y-axis broken with PNG or plotly
library(tidyverse)
library(plotly)
library(patchwork)
## Creat data
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) %>%
gather("Group","value",-years)
## whole figure
ggplot(data=df,aes(years,value,fill=Group))+
geom_bar(stat = "identity", position=position_dodge())+
theme_classic()
## first fig
p1=ggplot(data=df,aes(years,value,fill=Group))+
geom_bar(stat = "identity", position=position_dodge())+
coord_cartesian(ylim = c(0,600))+ #setting the down part
theme_classic()
## second fig
p2=ggplot(data=df,aes(years,value,fill=Group))+
geom_bar(stat = "identity", position=position_dodge())+
coord_cartesian(ylim = c(1000,1600))+ #setting the upper part
guides(x = "none")+ # remove x line
labs( title = "Graph with broken y axis")+
theme_classic()+
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title = element_blank(),
legend.position = "none", # remove legend
panel.background = element_blank(),
axis.line = element_line(colour = "black"))
## patchwork
## bind fig1+fig2
p2/p1
## plotly
subplot(p2,style(p1, showlegend = FALSE),nrows = 2, shareX=TRUE,
margin = 0.02)
Upvotes: 3
Reputation: 61084
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:
use subplot(fig1, fig2, nrows = 2, shareX=TRUE, margin = <low>)
, and
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.
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
Upvotes: 3