Graphing error with facet_wrap and scales = "free" in plotly

I have a time series with different categories (faces), and I'm trying to create a chart using the facet_wrap function from ggplot2 and load it onto plotly .

If I set scales = "fixed" , I have no problem: the graph looks great on my computer and on plotly . But if I set scales = "free" , as in the code below, then ggplot2 on my computer looks fine, but the plotly version plotly not display correctly (on my computer or on the Internet).

Here is a sample code; I am creating some random values ​​for 12 states covering a 16-year period from 2000 to 2015, and I want to build a time series of these values ​​with each state having its own diagram and unique y axis.

 library(ggplot2) library(plotly) set.seed(1) states = sample(state.name, 12) ## create random list of 12 states ## create random data with values for state and by year dat <- data.frame(Year = rep(2000:2015, times = 12), State = rep(states, each = 16), Value = rnorm((12*16), mean = 5, sd = 2)) ## plot data with facet_wrap for each state p <- ggplot(dat, aes(Year, Value)) + geom_line() + facet_wrap(~ State, ncol = 4, scales = "free") p ggplotly(p) 

The ggplot2 (p) command displays as expected:

enter image description here

But ggplotly(p) distorted:

enter image description here

As far as I can tell, there was a previous thread in this section, but it was not allowed:

Flat faces do not translate correctly

I would appreciate any help with this.

+1
source share
1 answer

If you are open to alternatives, you can try with native plotly with subplot . Try:

 dat$id <- as.integer(dat$State) p <- plot_ly(dat, x = Year, y = Value, group = State, xaxis = paste0("x", id), marker=list(color="black")) p <- layout( subplot(p, nrows=3, margin = 0.05), xaxis = list(title = "Arizona"), xaxis2 = list(title = "Connecticut"), xaxis3 = list(title = "Florida"), xaxis4 = list(title = "Georgia"), xaxis5 = list(title = "Indiana"), xaxis6 = list(title = "Maine"), xaxis7 = list(title = "Nebraska"), xaxis8 = list(title = "Nevada"), xaxis9 = list(title = "New Hampshire"), xaxis10 = list(title = "South Dakota"), xaxis11 = list(title = "Tennessee"), xaxis12 = list(title = "Texas"), yaxis = list(title = "Value"), yaxis2 = list(title = ""), yaxis3 = list(title = ""), yaxis4 = list(title = ""), yaxis5 = list(title = "Value"), yaxis6 = list(title = ""), yaxis7 = list(title = ""), yaxis8 = list(title = ""), yaxis9 = list(title = "Value"), yaxis10 = list(title = ""), yaxis11 = list(title = ""), yaxis12 = list(title = ""), showlegend = FALSE) p 
+2
source

Source: https://habr.com/ru/post/1271903/


All Articles