I am creating a plotly graph that has a button that selects which series to be shown, quite a basic plotly functionality. The problem I have is that the purpose of the plot is to show the difference in variability and the differential rate of increase among the series. Plotly automatically sets the ylim for each axis based on the max/min of the series being plotted, which makes all series look like they are increasing at a similar rate even if one series has an increase of 1 unit over time while the other a trend 5 times steeper. I have been trying to find a way to define series-specific ylim values but cannot find a way. I know that I could relativize each series, but for my actual data, the absolute values are quite important. Having all lines plotted in a single all-encompassing ylim would also not work as it would squish some of the time series in the actual data I am trying to plot.
Anyone has any idea?
The actual graph I am trying to change is on this research website. The graph makes it appear that all species are growing in the same rate, when that is not the case...
Below is the example code with data and the graph with automatically defined ylim values. Note how all traces look like they have the same trend because of the large difference in ylim ranges.
set.seed(0)
#create example dataframe
year=c(2000:2018)
grape_prices=c(seq(from=3.5, to=4.5, length.out = 19))+runif(19)
apple_prices=c(seq(from=3, to=5.5, length.out = 19))+runif(19)*2
mango_prices=c(seq(from=7, to=12, length.out = 19))
DF=data.frame(year, grape_prices, apple_prices, mango_prices)+runif(19)
#find ylims for each line that have roughtly the same range
DF_range_and_lims=as.data.frame(t(apply(DF[,-1], 2, range)))
names(DF_range_and_lims)=c("min", "max")
DF_range_and_lims$range=DF_range_and_lims$max-DF_range_and_lims$min
highest_range=DF_range_and_lims$range[which(DF_range_and_lims$range==max(DF_range_and_lims$range))]
cat("highest range is ", highest_range, "\n")
DF_range_and_lims$ylim_low=floor(DF_range_and_lims$min)
DF_range_and_lims$ylim_high=ceiling(DF_range_and_lims$min+highest_range)
DF_plot_ylims=DF_range_and_lims[,c("ylim_low", "ylim_high")]
cat("These are the ylims I would like to use", "\n")
DF_plot_ylims
#plotly plot with button for series selector
library(plotly)
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right")
multi_species_equal_axis <- plot_ly(DF, x = ~year) %>%
add_lines(y = ~grape_prices, name = "Grape prices") %>%
add_lines(y = ~apple_prices, name = "Apple_prices", visible=T, color = I('red'), yaxis = "y2") %>% #yaxis = "y2",
add_lines(y = ~mango_prices, name = "Mango Prices", visible=F, color = I('red'), yaxis = "y2") %>% #yaxis = "y2",
layout(
xaxis = list(title = "", domain = c(0.1, 1)),
updatemenus = list(
list(
buttons = list(
list(method = "restyle",
args = list("visible", list(TRUE, TRUE, FALSE)),
label = "Apple prices"),
list(method = "restyle",
args = list("visible", list(TRUE, FALSE, TRUE)),
label = "Mango prices")))),
yaxis2 = ay,
yaxis = list(rangemode="normal", title = "Price in R$"))
multi_species_equal_axis