如何在Vegabrite区域图中放置渐变色

k97glaaz  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(130)

我试图模仿Vega-lite面积图与梯度颜色,使用vegabrite package在R。

上图来自Vega-lite examples,显示了漂亮的颜色渐变:从白色(底部)到深绿色(顶部)。
我失败的尝试(下图)显示只是普通的绿色填充-我应该怎么做才能把梯度类似于上面的图像?

library(vegabrite)

vl_chart(data = 'https://vega.github.io/vega-editor/app/data/stocks.csv', 
         width = 300, height = 300) %>%
  vl_filter("datum.symbol == 'GOOG'") %>%
  vl_mark_area(stroke = "darkgreen", 
               strokeWidth = 2,
               fill = 'lightgreen', opacity = 0.4
               ) %>%
  vl_encode_x(field = 'date', timeUnit = 'yearmonth') %>%
  vl_axis_x(format = '%Y') %>% 
  vl_encode_y(field = 'price', type = 'quantitative')

q5iwbnjs

q5iwbnjs1#

这是我的临时解决方案-使用嵌套列表。
它需要进一步的尝试,直到输出的JSON文件与Vega-lite示例展示中显示的相同。

library(vegabrite)

vl_chart(data = 'https://vega.github.io/vega-editor/app/data/stocks.csv', 
         width = 300, height = 300) %>%
  vl_filter("datum.symbol == 'GOOG'") |>
  vl_mark_area(line = list(color = "darkgreen", size = 3),
               color = list(x1 = 1,
                            x2 = 1,
                            y1 = 1,
                            y2 = 0,
                            gradient = "linear",
                            stops = list(
                              list(offset = 0, color = "white"),
                              list(offset = 1, color = "darkgreen")
                            )
               )
  ) %>%
  vl_encode_x(field = 'date', timeUnit = 'yearmonth') %>%
  vl_axis_x(format = '%Y') %>% 
  vl_encode_y(field = 'price', type = 'quantitative')

相关问题