2017-07-26 3 views
2

Im Blick auf einige Probleme mit ggplot2 und plotly. Beim Erstellen eines Balkendiagramms mit ggplot2 und Übergabe an die Funktion ggplotly sind die Balken beim Abwählen der Variablen in der Mitte. Der Graph verhält sich nicht wie die Beispiele here .Balkendiagramm in plotly * flies * wenn Variablen abgewählt werden

Beispiel:

library(ggplot2) 
library(reshape2) 
library(plotly) 

df1 <- data.frame("Price" = rnorm(3, mean = 100, sd = 4), 
       "Type" = paste("Type", 1:3)) 

df2 <- data.frame("Price" = rnorm(3, mean = 500, sd = 4), 
        "Type" = paste("Type", 1:3)) 

df <- rbind(df1, df2) 

df$Dates <- rep(c("2017-01-01", "2017-06-30"), 3) 

df <- melt(df, measure.vars = 3) 

p <- ggplot(df, aes(fill=Type, y=Price, x=value)) + 
    geom_bar(stat="identity", position = "stack") 

ggplotly(p) 

G1

enter image description here

Im läuft auf folgendes:

> sessionInfo() 
R version 3.3.2 (2016-10-31) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 
Running under: Windows >= 8 x64 (build 9200) 

locale: 
[1] LC_COLLATE=Swedish_Sweden.1252 LC_CTYPE=Swedish_Sweden.1252 LC_MONETARY=Swedish_Sweden.1252 LC_NUMERIC=C     
[5] LC_TIME=Swedish_Sweden.1252  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] zoo_1.8-0   dygraphs_1.1.1.4 plotly_4.7.0.9000 reshape2_1.4.2  ggplot2_2.2.1.9000 lubridate_1.6.0 readxl_1.0.0 

Dank!

Antwort

1

Ich denke, das Problem liegt in der Interaktion zwischen ggplot2 und plotly. Verwenden Sie plot_ly Funktion direkt

p <- plot_ly(df, x = ~value, y = ~Price, type = 'bar',split=~Type) %>% 
    layout(yaxis = list(title = 'Count'), barmode = 'stack') 
p 
+0

Vielen Dank, es scheint so. Ich bevorzuge es, den Plot in ggplot2 zu erstellen, aber Ihre Lösung hat mein Problem gelöst. – Pierre

Verwandte Themen