Home > front end >  plotly produces false plot | warning "...not a multiple of replacement length"
plotly produces false plot | warning "...not a multiple of replacement length"

Time:12-03

Plotly seems to have a bug, otherwise I can't understand the issue. Until it is solved, I would need a workaround.

Does anyone understand why this with four unique years is working

library(sf)
library(plotly)
library(ggplot2)
library(dplyr)
dat = data.frame(cla = factor(rep(LETTERS[1:2],10)), year = rep(c(2018:2022),4),x=runif(20, 1, 99),y = runif(20, 1, 99))
dat_sf = st_as_sf(dat, coords = c("x","y"))
gg = dat_sf %>%
  ggplot(aes(color=cla, frame = year))   
  geom_sf(size=3)
ggplotly(gg)

and this throws with five years an error

dat = data.frame(cla = factor(rep(LETTERS[1:2],10)), year = rep(c(2018:2021),5),x=runif(20, 1, 99),y = runif(20, 1, 99))
dat_sf = st_as_sf(dat, coords = c("x","y"))
gg = dat_sf %>%
  ggplot(aes(color=cla, frame = year))   
  geom_sf(size=3)
ggplotly(gg)

CodePudding user response:

Problem is not with plotly package but with the way you have created the data.frame dat.

Also one correction when the code is working that time you have considered five distinct years and repeated the years 4 times whereas when the code isn't working as expected you have considered four distinct years and repeated five times. So when warning is occurring both cla A and B have no common year between them.

cla A has 2018 and 2020 whereas cla B has 2019 and 2021 as the distinct years which is causing the warning. This is happening because of rep(c(2018:2021), 5) which creates the data frame as

   cla year         x         y
1    A 2018 50.820322 88.405820
2    B 2019 34.074510 74.976814
3    A 2020 88.644793 96.961966
4    B 2021  4.133220  5.326413
5    A 2018 24.248510 89.533287
6    B 2019 68.276054 85.819080
7    A 2020 23.130205 76.989944
8    B 2021 32.212470 37.928009
9    A 2018 18.050414  5.126589
10   B 2019 79.540099 36.712286
11   A 2020 15.335642 27.827624
12   B 2021 81.626304 84.345813
13   A 2018 33.437787 36.515368
14   B 2019 37.668600 30.838823
15   A 2020 62.715052 75.395263
16   B 2021 10.470107 83.793793
17   A 2018  3.155384 45.876313
18   B 2019 98.318388 72.503903
19   A 2020 58.226007 11.199706
20   B 2021 77.618660 22.558351

Instead of that you may use rep(2018:2021, each = 5) which will create the data frame as below:

dat = data.frame(cla = factor(rep(LETTERS[1:2],10)), year = rep(2018:2021, each = 5),x=runif(20, 1, 99),y = runif(20, 1, 99))

   cla year         x         y
1    A 2018 94.487222 90.096357
2    B 2018 74.699679  6.525458
3    A 2018 81.257628 50.285014
4    B 2018 41.942156 35.353388
5    A 2018 59.196172 83.864500
6    B 2019 79.284709 80.030646
7    A 2019 88.064048 12.498460
8    B 2019 38.748776 70.843282
9    A 2019  9.851564 24.056348
10   B 2019 62.322876  8.345762
11   A 2020 74.072349 92.693322
12   B 2020  9.367089 16.402447
13   A 2020 30.450726 64.411620
14   B 2020 61.227091 18.004739
15   A 2020 74.787342  2.967253
16   B 2021 90.871599 52.088757
17   A 2021 47.623550  9.455234
18   B 2021 56.577708 28.734230
19   A 2021 73.188794 42.202620
20   B 2021 85.029848 58.553256

This will solve the problem of false plot as both cla A and B will have common years for plotting.

  • Related