Plotly:如何为每个数据设置背景颜色 [英] Plotly: How to set background color per data

查看:162
本文介绍了Plotly:如何为每个数据设置背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置两种不同的颜色作为绘图背景:像这样每个数据一种颜色

这是我到目前为止的情节:

解决方案

以下建议是一个最小的可重复示例,向您展示它可以完成.尽管并不像您希望的那样轻松优雅.据我所知,

代码:

库(dplyr)图书馆(情节)df = data.frame(x=c(1,2,3,4,5,6,7,8,9,10,11,12),y1=c(4,2,4,2,6,7,8,7,9,10,9,12),y2=c(3,4,5,6,3,1,5,6,3,2,7,8))列表(df$x)x_start <- df$x[seq(1, length(df$x), 2)]x_stops <- df$x[seq(2, length(df$x), 2)]p1 <- plot_ly(x=df$x, y=df$y1, mode='lines', line=list(color='green'), name = 'line')p2 <- plot_ly(df) %>% add_bars(x=~x, y=~y2, name='bar', width=0.4)# 设置形状shape=list(type='rect', line = list(color = 'rgba(0,0,0,0)'), fillcolor="rgba(147,112,219,0.1)", xref='x', yref='y')shape_offset = 0.5形状 <- 列表()for (i in seq_along(x_start)){打印(一)shape[["x0"]] <- x_start[i] + shape_offsetshape[["x1"]] <- x_stops[i] + shape_offset形状[[y0"]] <- 0形状[[y1"]] <- 16形状<- c(形状,列表(形状))}p1 <- 布局(p1,形状=形状,xaxis = 列表(showgrid=FALSE))p2 <- 布局(p2,形状=形状)p <- subplot(p1, p2, nrows = 2, margin=0.05)磷

希望对你有用.如果您愿意,我们可以在您有机会查看时讨论更多细节.

编辑 1:

这是在构建背景形状时考虑 y 的最大值的建议.如果您有兴趣,可以灵活调整子图的数量.

情节 2:

代码 2:

库(dplyr)图书馆(情节)df = data.frame(x=c(1,2,3,4,5,6,7,8,9,10,11,12),y1=c(4,2,4,2,6,7,8,7,9,10,9,12),y2=c(3,4,5,6,3,1,5,6,3,2,7,8))列表(df$x)x_start <- df$x[seq(1, length(df$x), 2)]x_stops <- df$x[seq(2, length(df$x), 2)]p1 <- plot_ly(x=df$x, y=df$y1, mode='lines', line=list(color='green'), name = 'line')p2 <- plot_ly(df) %>% add_bars(x=~x, y=~y2, name='bar', width=0.4)# 设置绘图 1 形状shape=list(type='rect', line = list(color = 'rgba(0,0,0,0)'), fillcolor="rgba(147,112,219,0.1)", xref='x', yref='y')shape_offset = 0.5形状 <- 列表()for (i in seq_along(x_start)){打印(一)shape[["x0"]] <- x_start[i] + shape_offsetshape[["x1"]] <- x_stops[i] + shape_offset形状[[y0"]] <- 0形状[["y1"]] <- max(df$y1)形状<- c(形状,列表(形状))#打印()}# 设置绘图 2 形状shape2=list(type='rect', line = list(color = 'rgba(0,0,0,0)'), fillcolor="rgba(147,112,219,0.1)", xref='x', yref='y')shape2_offset = 0.5形状 2 <- 列表()for (i in seq_along(x_start)){打印(一)shape2[["x0"]] <- x_start[i] + shape2_offsetshape2[["x1"]] <- x_stops[i] + shape2_offsetshape2[["y0"]] <- 0shape2[["y1"]] <- max(df$y2)形状 2 <- c(shapes2, list(shape2))#打印()}p1 <- 布局(p1,形状=形状,xaxis = 列表(showgrid=FALSE))p2 <-布局(p2,形状=形状2)p <- subplot(p1, p2, nrows = 2, margin=0.05)磷

I want to set two different colors as the plot background: one color per data like this

Here are my plots so far:

解决方案

The following suggestion is a minimal reproducible example to show you that it can be done. Albeit not so easily and elegantly as you may have hoped. To my knowledge, there does not yet exist a way to alternate backround colors without doing so through shapes. The following setup at least captures some of the features of your demonstrated setup, in that it is a mix of a line and a bar chart, and that it produces multiple subplots in one go.

Plot:

Code:

library(dplyr)
library(plotly)

df = data.frame(x=c(1,2,3,4,5,6,7,8,9,10,11,12),
                y1=c(4,2,4,2,6,7,8,7,9,10,9,12),
                y2=c(3,4,5,6,3,1,5,6,3,2,7,8))


list(df$x)

x_start <- df$x[seq(1, length(df$x), 2)]
x_stops <- df$x[seq(2, length(df$x), 2)]

p1 <- plot_ly(x=df$x, y=df$y1, mode='lines', line=list(color='green'), name = 'line')
p2 <- plot_ly(df) %>% add_bars(x=~x, y=~y2, name='bar', width=0.4)

# set up shapes
shape=list(type='rect', line = list(color = 'rgba(0,0,0,0)'), fillcolor="rgba(147,112,219,0.1)", xref='x', yref='y')
shape_offset = 0.5
shapes <- list()

for (i in seq_along(x_start)){
  print(i)
  shape[["x0"]] <- x_start[i] + shape_offset
  shape[["x1"]] <- x_stops[i] + shape_offset
  shape[["y0"]] <- 0
  shape[["y1"]] <- 16
  shapes <- c(shapes, list(shape))
}

p1 <- layout(p1, shapes=shapes, xaxis = list(showgrid=FALSE))                 
p2 <- layout(p2, shapes=shapes)                 


p <- subplot(p1, p2, nrows = 2, margin=0.05)

p

I hope this will be useful to you. If you'd like we can discuss further details when you've had a chance to look at it.

Edit 1:

Here's a suggestion that takes the max of y into consideration when the background shapes are built. This can be made flexible with regards to the number of subplots if you're interested.

Plot 2:

Code 2:

library(dplyr)
library(plotly)

df = data.frame(x=c(1,2,3,4,5,6,7,8,9,10,11,12),
                y1=c(4,2,4,2,6,7,8,7,9,10,9,12),
                y2=c(3,4,5,6,3,1,5,6,3,2,7,8))


list(df$x)

x_start <- df$x[seq(1, length(df$x), 2)]
x_stops <- df$x[seq(2, length(df$x), 2)]

p1 <- plot_ly(x=df$x, y=df$y1, mode='lines', line=list(color='green'), name = 'line')
p2 <- plot_ly(df) %>% add_bars(x=~x, y=~y2, name='bar', width=0.4)

# set up plot 1 shapes
shape=list(type='rect', line = list(color = 'rgba(0,0,0,0)'), fillcolor="rgba(147,112,219,0.1)", xref='x', yref='y')
shape_offset = 0.5

shapes <- list()


for (i in seq_along(x_start)){
  print(i)
  shape[["x0"]] <- x_start[i] + shape_offset
  shape[["x1"]] <- x_stops[i] + shape_offset
  shape[["y0"]] <- 0
  shape[["y1"]] <- max(df$y1)
  shapes <- c(shapes, list(shape))
  #print()
}

# set up plot 2 shapes
shape2=list(type='rect', line = list(color = 'rgba(0,0,0,0)'), fillcolor="rgba(147,112,219,0.1)", xref='x', yref='y')
shape2_offset = 0.5

shapes2 <- list()


for (i in seq_along(x_start)){
  print(i)
  shape2[["x0"]] <- x_start[i] + shape2_offset
  shape2[["x1"]] <- x_stops[i] + shape2_offset
  shape2[["y0"]] <- 0
  shape2[["y1"]] <- max(df$y2)
  shapes2 <- c(shapes2, list(shape2))
  #print()
}

p1 <- layout(p1, shapes=shapes, xaxis = list(showgrid=FALSE))                 
p2 <- layout(p2, shapes=shapes2)                 


p <- subplot(p1, p2, nrows = 2, margin=0.05)

p

这篇关于Plotly:如何为每个数据设置背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆