[英]R plotly customization of multiple line plot
I have the following sample dataframe and I'm using Plotly in my R Shiny app to create a multi line plot: I have the following sample dataframe and I'm using Plotly in my R Shiny app to create a multi line plot:
df2=data.frame(DATE=as.Date(c('2022-01-04','2022-01-08','2022-02-06','2022-01-12','2022-02-15','2022-01-25','2022-02-08')),
QUANTITY=c(50,50,50,120,120,70,50),
PRICE=c(111,112, 104,90,93,98,105), TYPE=c('summer','summer','summer','summer','summer','winter','winter'),
COLOR=c('blue','blue','blue','blue','blue','red','red'))
plot_ly(df2, x = ~eval(parse(text='DATE')),
y = ~eval(parse(text='PRICE')), color = ~eval(parse(text='COLOR')), connectgaps = TRUE) %>% add_lines()
The three questions I have are the following:我的三个问题如下:
color
defines a variable to group colors by; color
定义了一个变量来分组 colors ; colors
is for designating a specific color. colors
用于指定特定颜色。
For example,例如,
plot_ly(df2, x = ~DATE, y = ~PRICE,
color = ~COLOR, colors = ~COLOR,
type = "scatter", mode = "markers+lines")
If you wanted the line size by quantity, you would need separate traces.如果您想要按数量计算线大小,则需要单独的迹线。 If you wanted to include markers by color, you need to do that with separate traces.如果您想按颜色包含标记,则需要使用单独的跟踪来完成。 The following code does this.以下代码执行此操作。 However, there was only 1 red at QUANTITY 50, and only 1 red at QUANTITY 70. Therefore, despite calling for lines, there is no line using this example data.然而,在 QUANTITY 50 只有 1 个红色,在 QUANTITY 70 只有 1 个红色。因此,尽管调用了线路,但没有使用此示例数据的线路。
p <- plot_ly()
lapply(1:length(unique(df2$QUANTITY)),
function(j){
lapply(1:length(unique(df2$COLOR)),
function(k){
cols <- unique(df2$COLOR)[k]
if(cols == "blue"){
p <<- p %>%
add_lines(data = df2 %>% filter(COLOR == cols,
QUANTITY == unique(QUANTITY)[j]),
x = ~DATE, y = ~PRICE, color = I(cols),
line = list(width = 3 * (unique(df2$QUANTITY)[j]/100)))
}
if(cols == "red"){
p <<- p %>%
add_trace(data = df2 %>% filter(COLOR == cols,
QUANTITY == unique(QUANTITY)[j]),
x = ~DATE, y = ~PRICE, color = I(cols),
line = list(width = 3 * (unique(df2$QUANTITY)[j]/100)),
type = "scatter", mode = "markers+lines")
}
})
})
p
If I take your expand on the example data, then use this code, it looks more obvious.如果我对示例数据进行扩展,然后使用此代码,它看起来更明显。
df3 <- data.frame(rbind(df2, df2 %>% mutate(PRICE = PRICE + 10, DATE = DATE + 4)))
p <- plot_ly()
lapply(1:length(unique(df3$QUANTITY)),
function(j){
lapply(1:length(unique(df3$COLOR)),
function(k){
cols <- unique(df3$COLOR)[k]
if(cols == "blue"){
p <<- p %>%
add_lines(data = df3 %>% filter(COLOR == cols,
QUANTITY == unique(QUANTITY)[j]),
x = ~DATE, y = ~PRICE, color = I(cols),
line = list(width = 3 * (unique(df3$QUANTITY)[j]/100)))
}
if(cols == "red"){
p <<- p %>%
add_trace(data = df3 %>% filter(COLOR == cols,
QUANTITY == unique(QUANTITY)[j]),
x = ~DATE, y = ~PRICE, color = I(cols),
line = list(width = 3 * (unique(df3$QUANTITY)[j]/100)),
type = "scatter", mode = "markers+lines")
}
})
})
p
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.