用R绘制间隙分析

时间:2019-04-18 11:25:52

标签: r plotly shiny-server

我想使用本机绘图(而不是ggplotly)创建一个GAP分析,以获取所有event_click信息,以备将来之需。 这里我的代码与ggplotly。我只是没有用本地剧情来做到这一点...

这是我的数据:

 graph_GAPAnalysis_Prdct <- structure(list(Model = c("Overall", "Group1", "Group1", "Group1", "Group2", "Group2", "Group3", "Group4", "Group4", "Group4", "Group3", "Group3", "Overall"), Product = structure(1:13, .Label = c("Obj.M04-2019", "Product1", "Product2", "Product3", "Product4", "Product5", "Product6", "Product7", "Product8", "Product9","Product10", "Product11", "M04-2019"), class = c("ordered","factor")), Var1 = c(6.65, 14.67, 12, 1.87, 5.25, 7.61, 4.75, 6.29, 6.68, 4.33, 3.06, 6.4, 6.65), Obj = c(9.4, 9.5, 9.5, 9.5, 12, 12, 8, 9, 9, 9, 8, 8, 9.4), Delta = c(-2.75, 5.17, 2.5, -7.63, -6.75, -4.39, -3.25, -2.71, -2.32, -4.67, -4.94, -1.6, -2.75), DATE = c(201904L,201904L, 201904L, 201904L, 201904L, 201904L, 201904L, 201904L, 201904L,201904L, 201904L, 201904L, 201904L), Period_Type = c("M", "M","M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M"), Year_name = c("M04-2019","M04-2019", "M04-2019", "M04-2019", "M04-2019", "M04-2019", "M04-2019","M04-2019", "M04-2019", "M04-2019", "M04-2019", "M04-2019", "M04-2019"), Var2 = c(4764.22, 456, 456, 456, 432.22, 456, 456, 456, 456, 456, 456, 228, 4764.22), Var3 = c(-134.23725, 23.5752, 11.4, -34.7928, -29.17485, -20.0184, -14.82, -12.3576, -10.5792, -21.2952, -22.5264, -3.648, -134.23725), Ecart = c(9.4, 0.482964303872435,0.23354173301375, -0.712769369157965, -0.597679388545281, -0.410099283172145,-0.303604252917875, -0.253159238586905, -0.21672672823676, -0.436255957269685, -0.46147846443517, -0.0747333545643999, 6.65), id = 1:13, type = c("Reference", "GAP +", "GAP +", "GAP -", "GAP -", "GAP -", "GAP -", "GAP -", "GAP -", "GAP -", "GAP -", "GAP -", "Reference"), end = c(9.4, 9.88296430387244, 10.1165060368862, 9.40373666772822, 8.80605727918294, 8.39595799601079, 8.09235374309292, 7.83919450450601, 7.62246777626925, 7.18621181899957, 6.7247333545644, 6.65, 0), start = c(0, 9.4, 9.88296430387244, 10.1165060368862, 9.40373666772822, 8.80605727918294, 8.39595799601079, 8.09235374309292, 7.83919450450601, 7.62246777626925,7.18621181899957, 6.7247333545644, 6.65)), .Names = c( "Model", "Product","Var1", "Obj", "Delta", "DATE", "Period_Type", "Year_name","Var2", "Var3", "Ecart", "id", "type", "end", "start"), row.names = c(12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,11L, 13L), class ="data.frame")
情节
seuil_GAPAnalysis_Prdct =min(graph_GAPAnalysis_Prdct$end[graph_GAPAnalysis_Prdct$end!=0])

render_GAPAnalysis_Prdct = ggplot(graph_GAPAnalysis_Prdct, aes(Product, fill = type, text=sprintf("Product: %s <br> Obj : %s <br> Real : %s <br> Weight: %s", Product, graph_GAPAnalysis_Prdct[,ncol(graph_GAPAnalysis_Prdct)-11], graph_GAPAnalysis_Prdct[,ncol(graph_GAPAnalysis_Prdct)-12], round(as.numeric(as.character(Ecart)),digits=2)))) + geom_rect(aes(x = Product, xmin = id - 0.45, xmax = id + 0.45, ymin = end, ymax = start))+scale_y_continuous(breaks=seq(0,round(max(as.numeric(graph_GAPAnalysis_Prdct$Ecart))),1))+ggtitle("GAP Analysis Production")+xlab("Product")+ylab("% Loss")+theme(axis.text.x = element_text(angle=60, hjust=1,size = 9))+scale_fill_manual(values = c("Reference"="#1589FF", "GAP +"="#F75D59","GAP -"="#4CC417"))+theme(plot.title = element_text(hjust = 0.5))
ggplotly(render_GAPAnalysis_Prdct,tooltip="text") %>% layout(margin = list(b = 130), xaxis = list(tickangle = 315))

情节:

enter image description here

谢谢。

我用ggplotly绘制了这个图。但是我无法成功地通过密谋做到这一点

1 个答案:

答案 0 :(得分:1)

以下一些想法可以帮助您解决问题:

# Add coordinates of bars' corners
df <- graph_GAPAnalysis_Prdct
df$x.1 <- df$id-0.45
df$y.1 <- df$end
df$x.2 <- df$id-0.45
df$y.2 <- df$start
df$x.3 <- df$id+0.45
df$y.3 <- df$start
df$x.4 <- df$id+0.45
df$y.4 <- df$end
df$x.5 <- df$id-0.45
df$y.5 <- df$end
df$text <- sprintf("Product: %s <br> Obj : %s <br> Real : %s <br> Weight: %s", df$Product, df[,ncol(df)-11], df[,ncol(df)-12], round(as.numeric(as.character(df$Ecart)), digits=2))

# From wide to long
df2 <- reshape(df, direction = "long", 
        varying = 16:25, v.names = c("x", "y"), 
        timevar="corner", times=c("1","2","3","4","5"),
        idvar = "id")

# Plot bars using mode="rect"
p <- plot_ly(data=df2, x=~x, y=~y, split=~id, color=~type, fill="toself", 
             text=~text, name=~type, legendgroup=~type,
             hoverinfo = 'text', hoveron="fills",  type="scatter", mode="lines")
p

enter image description here