首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在plotly中制作用户自定义/显式的色标和图例?

如何在plotly中制作用户自定义/显式的色标和图例?
EN

Stack Overflow用户
提问于 2018-02-11 00:39:01
回答 1查看 934关注 0票数 0

我有一个按月划分的数据集。然而,当我绘制每个子集时,图例的颜色比例和范围都是不同的。我如何显式地定义它,以便它在所有子集上都是一致的?

因此,在下面的示例中,我希望未过滤的数据集和过滤的数据集的图例的色标和范围是相同的。

github link to dataset

代码语言:javascript
复制
#original dataset. I want to construct the legend with this
weather <- read.csv("weather2_rmse.csv") %>% 
  mutate(date_of_forecast = as.Date(date_of_forecast))

#filtered. I want to construct the points with this
data <- filter(weather, date_of_forecast == "2015-02-01")

weather_map <- function(data) {

  # change default color scale title
  m <- list(colorbar = list(title = ""))

  # geo styling
  g <- list(
    scope = 'north america',
    showland = TRUE,
    landcolor = toRGB("grey83"),
    subunitcolor = toRGB("white"),
    countrycolor = toRGB("white"),
    showlakes = TRUE,
    lakecolor = toRGB("white"),
    showsubunits = TRUE,
    showcountries = TRUE,
    resolution = 50,
    projection = list(
      type = 'conic conformal',
      rotation = list(lon = -100)
    ),
    lonaxis = list(
      showgrid = TRUE,
      gridwidth = 0.5,
      range = c(-140, -55),
      dtick = 5
    ),
    lataxis = list(
      showgrid = TRUE,
      gridwidth = 0.5,
      range = c(20, 60),
      dtick = 5
    )
  )

  #the plotly part
  p <- plot_geo(data, lat = ~latitude, lon = ~longitude, color = ~rmse) %>%
    add_markers(
      text = ~paste(data$rmse, "RMSE"), hoverinfo = "text"
    ) %>%
    layout(title = 'Average RMSE of MaxTemp Forecasts by Month<br>xxxxxxxxxx', geo = g)
  p
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-11 02:25:18

例如,这样定义一个比例,例如:

代码语言:javascript
复制
scl = list(c(0, "rgb(0, 0, 255)"), list(1, "rgb(0, 255, 0)"))

(您还可以添加更多值,这是基于分位数AFAIK的。所以像这样设置它

代码语言:javascript
复制
scl = list(c(0, "rgb(0, 0, 255)"), list(0.1, "rgb(0, 0, 255)"),
           c(0.1, "rgb(0, 255, 0)"), list(1, "rgb(0, 255, 0)"))

将使您的值的前10%为蓝色,其余为绿色。

然后,修改您的plot调用,例如:

代码语言:javascript
复制
  p <- plot_geo(data) %>%
    add_markers(y = ~latitude, x = ~longitude,
                mode = "markers", type = "scatter", 
                marker = list(color = ~rmse,
                              colorscale = scl,
                              cauto = FALSE,
                              colorbar = list(title=""),
                              cmax = 15,
                              cmin = 0)) %>%
    layout(title = 'Average RMSE of MaxTemp Forecasts by Month<br>xxxxxxxxxx', geo = g)
  p

通过使用cmaxcmin设置精确的限制,您应该在各子图中具有相同的比例

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48723071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档