对于 R 中的极坐标图,为什么线条不从颜色参数中获取颜色,但图例有色标?

问题描述 投票:0回答:1

我正在尝试创建一个极坐标图,其中线条颜色根据观察年份在梯度上变化(例如,有 90 年的日期,按月,在极坐标图上用一条线绘制,并且随着年份进度,我也希望线条的颜色沿着渐变前进)。

我得到一个显示颜色渐变范围的图例,但线条本身根本不匹配。

我收到以下错误消息。

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

这是我正在运行的代码:

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
            )

gsod_temp 数据帧的结构是:

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 ...

还有一些示例数据:

> 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       

我做错了什么?

如果我将年份转换为一个因子,我会逐年得到一种颜色,但它是一组离散的颜色,每年一个,而不是非离散的色标。

这是输出:

这是一个可以针对此代码运行的测试数据集:

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))

提前致谢!

r plotly polar-plot
1个回答
0
投票

在这个阶段,我能得出的最好结论是,plotly 目前不支持连续色阶/从线对象数组派生的色阶(我认为它最终会实现,因为它说“尚”不支持)。由于缺少某种手动编码的解决方法(我没有),我认为可以肯定地说,这目前是不可能的。

© www.soinside.com 2019 - 2024. All rights reserved.