繁体   English   中英

Plotly-R:如何制作有间隙的 y 轴?

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

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

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

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

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

Plot:

在此处输入图像描述

完整代码:

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

这是ggplot的更新,您可以查看被 PNG 或plotly破坏的 y 轴

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)

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM