I'm trying to create a polar plot where the line colour changes across a gradient based on the year of the observation (e.g. there are 90 years of date, by month, plotted with a line on a polar plot, and as the years progress, I also want the color of the line to progress along the gradient).

I'm getting a legend showing the color gradient range, but the line itself isn't matching at all.

I'm getting the following error message.

line.color doesn't (yet) support data arrays

Here is the code I'm running:

gsod_temp %>%
  filter(ctry == 'AU') %>%
  arrange(date) %>%
  plot_ly(type = 'scatterpolar', 
          mode = 'lines') %>%
  add_trace(r = ~avg_temp, 
            theta = ~(360*(mo_num/12)),
            color = ~year
            )

Structure of the gsod_temp dataframe is:

tibble [168,912 × 10] (S3: tbl_df/tbl/data.frame)
 $ ...1     : num [1:168912] 19 45 46 47 83 84 85 86 87 140 ...
 $ ctry     : chr [1:168912] "RU" "DE" "GB" "IE" ...
 $ year     : num [1:168912] 1931 1932 1932 1932 1933 ...
 $ mo       : chr [1:168912] "01" "01" "01" "01" ...
 $ avg_temp : num [1:168912] 28.6 37 45 47.4 41.9 ...
 $ Name     : chr [1:168912] "Russian Federation" "Germany" "United Kingdom" "Ireland" ...
 $ year_mo  : num [1:168912] 193101 193201 193201 193201 193301 ...
 $ mo_num   : num [1:168912] 1 1 1 1 1 1 1 1 1 1 ...
 $ date     : Date[1:168912], format: "1931-01-01" "1932-01-01" ...
 $ mo_factor: Ord.factor w/ 12 levels "01"<"02"<"03"<..: 1 1 1 1 1 1 1 1 1 1 ...

And some example data:

> head(gsod_temp)
# A tibble: 6 × 10
   ...1 ctry   year mo    avg_temp Name            year_mo mo_num date       mo_factor
  <dbl> <chr> <dbl> <chr>    <dbl> <chr>             <dbl>  <dbl> <date>     <ord>    
1    19 RU     1931 01      28.6   Russian Federa…  193101      1 1931-01-01 01       
2    45 DE     1932 01      37.0   Germany          193201      1 1932-01-01 01       
3    46 GB     1932 01      45.0   United Kingdom   193201      1 1932-01-01 01       
4    47 IE     1932 01      47.4   Ireland          193201      1 1932-01-01 01       
5    83 IE     1933 01      41.9   Ireland          193301      1 1933-01-01 01       
6    84 KZ     1933 01       0.517 Kazakhstan       193301      1 1933-01-01 01       

What am I doing wrong?

If I convert the year to a factor, I get one colour by year, but then it's a discrete set of colours, one for each year, instead of a non-discrete color scale.

Here's the output:

A polar plot made with plotly in R, showing average monthly temperature, with a colour scale for year, but the line is solid orange

Here's a test dataset that can be run against this code:

gsod_temp <- data.frame(
  ctry = "AU",
  year = c(2021,2022,2023,2021,2022,2023,2021,2022,2023,2021,2022,2023,2021,2022,2023,2021,2022,2023,2021,2022,2023,2021,2022,2023,2021,2022,2023,2021,2022,2023,2021,2022,2021,2022
  ),
  avg_temp = c(73.7015450651341,75.4104290076383,73.6868657848023,72.2521273258409,73.1598298903227,73.4616700246468,70.1546055016525,71.9082260213444,71.2348908276238,65.3337760102265,67.2879372517948,65.4183841482946,60.3262843120212,61.1217217232473,58.5109055884243,56.5089054849122,55.9951720218307,56.9544276966095,55.8852550612849,54.0446941350753,56.258190531307,57.8043905662884,57.1572954432571,58.4890723197841,61.1094747395333,60.643525057218,63.5242135946568,64.9447142925673,64.809677957349,65.1997852147852,67.7951035968358,66.7495405578443,72.3003820409705,70.6337899227766
  ),
  mo_num = c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9,10,10,10,11,11,12,12
  ),
  date = c("2021-01-01","2022-01-01","2023-01-01","2021-02-01","2022-02-01","2023-02-01","2021-03-01","2022-03-01","2023-03-01","2021-04-01","2022-04-01","2023-04-01","2021-05-01","2022-05-01","2023-05-01","2021-06-01","2022-06-01","2023-06-01","2021-07-01","2022-07-01","2023-07-01","2021-08-01","2022-08-01","2023-08-01","2021-09-01","2022-09-01","2023-09-01","2021-10-01","2022-10-01","2023-10-01","2021-11-01","2022-11-01","2021-12-01","2022-12-01"
  )
)

gsod_temp %<>% mutate(date = ymd(date))

Thanks in advance!

1

There are 1 answers

0
Lachlan Macnish On

At this stage, the best conclusion I can reach is that plotly does not currently support continuous colour scales/colour scales derived from arrays for line objects (I assume it will come eventually given it says it is not supported "yet"). Short of some kind've of manually coded work-around (which I don't have) I think it is safe to say that this is currently not possible.