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

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

问题描述

我正在尝试创建一个美国的绿藻色图,该色度使用分类变量作为州色,但是我只能得到一个空白图.地物图与分类数据兼容吗?如果是这样,语法会如何变化?

对于我的数据,我只是简单地上载一个由状态组成的行表,并随机地包含好",差",确定"之一.

我可以在下面的代码中进行哪些更改才能使其正常工作?我尝试了一种变通办法,该办法可以稍微改变状态的颜色,但是颜色栏会变色.(value4是我的良好",不良",确定"的分类变量)

很抱歉,如果我的问题不清楚或我的信息不好.如果有人有其他问题,我可以回答.预先感谢

  foo<-brewer.pal(n = 3,名称="Set1")df<-mutate(df,test = ntile(x = value4,n = 3))cw_map<-plot_ly(数据= dftype ="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 = factor(df $ value4)df $ test = as.numeric(df $ value4)nfactor =长度(levels(df $ value4))foo<-brewer.pal(n = nfactor,name ="Set1")名称(foo)=级别(df $ value4) 

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

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天全站免登陆