如何基于分类变量在 R Plotly 中创建叶绿素图? [英] How to create a chloropleth map in R Plotly based on a Categorical variable?

查看:21
本文介绍了如何基于分类变量在 R Plotly 中创建叶绿素图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建美国的叶绿素地图,该地图使用分类变量作为州颜色,但我只得到一张空白地图.绘图地图是否与分类数据兼容?如果是这样,语法如何变化?

对于我的数据,我只是上传包含状态和随机好"、坏"、好"之一的行表.

我可以在下面的代码中进行哪些更改以使其正常工作?我尝试了一种解决方法,可以稍微改变状态的颜色,但颜色条会变得不稳定.(value4 是我的分类变量Good"、Bad"、OK")

如果我的问题不清楚或我的信息不是很好,我们深表歉意.如果有人有进一步的问题,我可以回答.提前致谢

foo <- brewer.pal(n = 3,名称 = "Set1")df <- mutate(df, test = ntile(x = value4, n = 3))cw_map <- plot_ly(数据 = df,类型=choropleth",位置=〜州,locationmode = "美国各州",颜色=〜测试,颜色 = foo[df$test],z = ~ 测试)%>%布局(地理=列表(范围=美国"))打印(cw_map)

解决方案

您需要在代码表单中包含状态,所以让我们从它开始:

STATES <-c("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA",HI"、ID"、IL"、IN"、IA"、KS"、KY"、LA"、ME"、MD"、MA"、MI"、MN"、MS"、MO"、MT"、NE"、NV"、NH"、NJ"、NM"、NY"、NC"、ND"、OH"、OK"、OR"、PA"、RI"、SC"、SD"、TN"、TX"、UT"、VT"、VA"、WA"、WV"、WI"、WY")

和你一样,我们为每个状态提供随机值4:

df = data.frame(state=STATES,value4=sample(c("Good", "Bad", "OK."),length(STATES),replace=TRUE))

然后我们将您的 value4 作为因素,以及您之前所做的颜色等:

df$value4 = 因子(df$value4)df$test = as.numeric(df$value4)nfactor = 长度(级别(df$value4))foo <- brewer.pal(n = nfactor,name = "Set1")名称(foo)= 级别(df$value4)

要使颜色图例采用离散形式,您需要将其作为数据框提供,该数据框在z 的相对比例 上定义您的中断.它在 R plotly 中没有很好的记录,我用

I'm trying to create a chloropleth map of the US that uses a categorical variable for the state color, but I only get a blank map. Do plotly maps have compatibility with categorical data? If so, how does the syntax change?

For my data, I'm simply uploading a table of rows consisting the state and randomly one of "Good", "Bad", "OK."

What can I change in the code below for it to work? I've tried a workaround that slightly works to change the states' color but the colorbar gets wonky. (value4 is my Categorical Variable of "Good", "Bad", "OK")

Apologies if my question is not clear or my info is not great. I can answer further questions if anyone has them. Thanks in advance

foo <- brewer.pal(n = 3,
                        name = "Set1")

df <- mutate(df, test = ntile(x = value4, n = 3))

cw_map <- plot_ly(
  data = df,
  type = "choropleth",
  locations = ~ state,
  locationmode = "USA-states",
  color = ~ test,
  colors = foo[df$test],
  z = ~ test
) %>%
  layout(geo = list(scope = "usa"))

print(cw_map)

解决方案

You need to have the states in the code form so let's start with that:

STATES <-c("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", 
"HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", 
"MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", 
"NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", 
"UT", "VT", "VA", "WA", "WV", "WI", "WY")

Like you did, we give random value4 for each state:

df = data.frame(state=STATES,
value4=sample(c("Good", "Bad", "OK."),length(STATES),replace=TRUE))

Then we make your value4 as factor, and colors etc as you have done before:

df$value4 = factor(df$value4)
df$test = as.numeric(df$value4)
nfactor = length(levels(df$value4))
foo <- brewer.pal(n = nfactor,name = "Set1")
names(foo) = levels(df$value4)

To have the color legend in discrete form, you need to provide it as a data frame that defines your breaks on the relative scale for z. It's not very well documented in R plotly and I wrote the solution below for n factors with information from @emphet's plotly forum post and @marcosandri's SO post:

Z_Breaks = function(n){
CUTS = seq(0,1,length.out=n+1)
rep(CUTS,ifelse(CUTS %in% 0:1,1,2))
}

colorScale <- data.frame(z=Z_Breaks(nfactor),
col=rep(foo,each=2),stringsAsFactors=FALSE)

          z     col
1 0.0000000 #E41A1C
2 0.3333333 #E41A1C
3 0.3333333 #377EB8
4 0.6666667 #377EB8
5 0.6666667 #4DAF4A
6 1.0000000 #4DAF4A

And we plot:

cw_map <- plot_ly(
  data = df,
  type = "choropleth",
  locations = ~ state,
  locationmode = "USA-states",
  z = df$test,
  colorscale=colorScale,
  colorbar=list(tickvals=1:nfactor, ticktext=names(foo))
) %>%
layout(geo = list(scope = "usa")) 

这篇关于如何基于分类变量在 R Plotly 中创建叶绿素图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆