简体   繁体   English

如何在 R 绘图叶绿体图中使用非默认色标?

[英]How to use a non-default colorscale in R plotly chloropleth maps?

I'm creating a chloropleth map in R using plotly, and the only trouble I'm having is setting a different colorscale.我正在使用 plotly 在 R 中创建叶绿素图,我遇到的唯一问题是设置不同的色阶。 I would like to use the magma colorscale from the viridis package, but I can't seem to figure out the correct way to do it.我想使用 viridis 包中的岩浆色阶,但我似乎无法找出正确的方法。 I've tried googling and searching, but no answers are quite working.我试过谷歌搜索和搜索,但没有答案是有效的。 Anyone have any advice?有人有什么建议吗?

The error I'm getting is: "unique() only applies to vectors."我得到的错误是:“unique() 仅适用于向量。” I've tried setting "discrete = TRUE" but that does not work.我试过设置“discrete = TRUE”,但这不起作用。

Let me know if you need more information.如果您需要更多信息,请与我们联系。

create_cw_map <- function(data, color_var) {
  if (is.null(data))
    return(NULL)

  g <- list(scope = "usa",
           projection = list(type = "albers usa"),
           showlakes = FALSE)

  cw_map <- plot_geo(data,
                     locationmode = "USA-states") %>%
            add_trace(z = ~ get(color_var),
                      locations = ~ state,
                      color = ~ get(color_var),
                      colorscale =  scale_fill_viridis(option = "magma")) %>%
            colorbar(title = color_var) %>%
            layout(geo = g)

            print(cw_map)
}

I do not have access to your data.我无权访问您的数据。 So I decided to use the tutorial data from the plotly package to demonstrate how to use viridis colors.所以我决定使用 plotly 包中的教程数据来演示如何使用 viridis 颜色。

Continuous veariable连续可变

If you read the help page for plot_ly() , you see that colors is specified as either a colorbrewer2.org palette name (eg "YlOrRd" or "Blues"), or a vector of colors to interpolate in hexadecimal "#RRGGBB" format, or a color interpolation function like colorRamp().如果您阅读了plot_ly()的帮助页面,您会看到colors被指定为 colorbrewer2.org 调色板名称(例如“YlOrRd”或“Blues”),或以十六进制“#RRGGBB”格式插入的颜色向量,或像 colorRamp() 这样的颜色插值函数。 What you can do is to create a vector of colors using magma() in the viridisLite package.您可以做的是使用 viridisLite 包中的magma()创建颜色向量。 Here I specified colors = magma(50, alpha = 1, begin = 0, end = 1, direction = 1) .这里我指定了colors = magma(50, alpha = 1, begin = 0, end = 1, direction = 1) n = 50 indicates that I want 50 colors in the color vector. n = 50 表示我想要颜色向量中的 50 种颜色。 You want to play around with this number for your own case.您想根据自己的情况使用这个数字。

library(dplyr)
library(viridis)
library(plotly)

df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")
df$hover <- with(df, paste(state, '<br>', "Beef", beef, "Dairy", dairy, "<br>",
                       "Fruits", total.fruits, "Veggies", total.veggies,
                       "<br>", "Wheat", wheat, "Corn", corn))

# give state boundaries a white border
l <- list(color = toRGB("white"), width = 2)

# specify some map projection/options
g <- list(scope = 'usa',
          projection = list(type = 'albers usa'),
          showlakes = TRUE,
          lakecolor = toRGB('white'))

p <- plot_geo(df, locationmode = 'USA-states') %>%
       add_trace(z = ~total.exports,
                 text = ~hover,
                 locations = ~code,
                 color = ~total.exports,
                 colors = magma(50, alpha = 1, begin = 0, end = 1, direction = 1)) %>%
       colorbar(title = "Millions USD") %>%
       layout(title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)',
              geo = g)

在此处输入图片说明

Categorical variable分类变量

After posting my answer, I thought you were using a categorical variable.发布我的答案后,我以为您使用的是分类变量。 I played around the example and think that it is tricky to create a chloropleth map with such a variable in plotly.我仔细研究了这个例子,并认为在 plotly 中创建一个带有这样一个变量的叶绿体图是很棘手的。 At least, I can assign colors to polygons based on a categorical variable, but a color bar appears in a funny way.至少,我可以根据分类变量为多边形分配颜色,但是颜色条以一种有趣的方式出现。 So I removed it.所以我删除了它。 (If anybody can improve this part, please do so.) (如果有人可以改进这部分,请这样做。)

Using the same data, I did the following.使用相同的数据,我做了以下事情。 I created a categorical variable using ntile() in the dplyr package.我在 dplyr 包中使用ntile()创建了一个分类变量。 I randomly created 9 levels in total.exports .我在total.exports随机创建了 9 个级别。 Then, I created nine colors using magma() .然后,我使用magma()创建了九种颜色。 When I drew the map below, I used colors = foo[df$export_nth] .当我绘制下面的地图时,我使用了colors = foo[df$export_nth] This is basically creating 50 colors using foo .这基本上是使用foo创建 50 种颜色。 export_nth is used as index numbers. export_nth用作索引号。 I hope this will help you to think how you can solve your situation.我希望这会帮助您思考如何解决您的情况。

mutate(df, export_nth = ntile(x = total.exports, n = 9)) -> df

# Create a magma color vector

foo <- magma(n = 9, alpha = 1, begin = 0, end = 1, direction = 1)

p <- plot_geo(df, locationmode = 'USA-states') %>%
     add_trace(z = ~export_nth,
               text = ~hover,
               locations = ~code,
               colors = foo[df$export_nth],
               color = ~export_nth,
               showscale = FALSE) %>%
     layout(title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)',
            geo = g)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM