使用plotly创建填充区域线图

时间:2019-08-16 15:50:45

标签: r plotly

我想使用此数据集以绘图方式创建基本的填充区域图:

week<-c(2,1,3)
pts<-c(10,20,30)
wex<-data.frame(week,pts)

x轴应包含week,如您所见,它可能在数据集中不是按顺序排列的,而必须在图的x轴上按顺序排列。 y轴应包含pts

由于某种原因,我什么也不做,但似乎没有错误。

library(plotly)
week<-c(2,1,3)
pts<-c(10,20,30)
wex<-data.frame(week,pts)

wex$week <- factor(wex$week, levels = wex[["week"]])
p <- plot_ly(x = ~wex$week, y = ~wex$pts,
             type = 'scatter', mode = 'lines',fill = 'tozeroy')


p

2 个答案:

答案 0 :(得分:1)

x轴上的值必须为数字(see example)并排序。

wex <- wex[order(wex$week), ]

# wex$week <- factor(wex$week, levels = wex[["week"]])
plot_ly(x = ~wex$week, y = ~wex$pts, type = 'scatter', mode = 'lines', 
        fill = 'tozeroy')

enter image description here

答案 1 :(得分:1)

如果您想保留x轴,这将起作用,但这不是一个纯粹的plot_ly答案:

p <- ggplot(wex, aes(x = week, y = pts)) + 
  geom_point() + 
  geom_line() + 
  geom_ribbon(aes(ymin = 0, ymax = pts), fill = "blue", alpha = .6, group = 1) 
g <-  ggplotly(p)
g

enter image description here