手动更改R中Bubble for Plotly Bubble Map的大小 [英] Manually changing the size of the Bubbles for Plotly Bubble Map in R

查看:373
本文介绍了手动更改R中Bubble for Plotly Bubble Map的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试手动更改Plotly气泡图的气泡大小。使用提供的数据,我成功地更改了地图的颜色,但我无法使用相同的逻辑来更改大小。要改变颜色,我只需调用: colors_wanted< - c(red,blue,black,pink),并将此命令传递给颜色 plot_ly 内。你认为可以改变尺寸,而不是使用这种情况下的公式 sqrt 来确定尺寸?

I am currently trying to change the sizes of the Bubbles for Plotly's bubble map manually. I was successful in changing the colors of the map using the data provided but I am unable to use the same logic to change the size. To change the colors I simply called: colors_wanted <- c("red", "blue", "black", "pink") and passed this command to colors within plot_ly. Do you think it is possible to change the sizes rather than using the formula in this case sqrt to claim the sizes?

library(plotly)
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_us_cities.csv')
df$hover <- paste(df$name, "Population", df$pop/1e6, " million")

df$q <- with(df, cut(pop, quantile(pop), include.lowest = T))
levels(df$q) <- paste(c("1st", "2nd", "3rd", "4th"), "Quantile")
df$q <- as.ordered(df$q)

g <- list(scope = 'usa',projection = list(type = 'albers usa'),showland = TRUE,landcolor = toRGB("gray85"),
subunitwidth = 1, countrywidth = 1, subunitcolor = toRGB("white"),countrycolor = toRGB("white"))

plot_ly(df, lon = lon, lat = lat, text = hover,
  marker = list(size = sqrt(pop/10000) + 1, line = list(width = 0)),
  color = q, colors= colors_wanted, type = 'scattergeo', locationmode = 'USA-states') %>%
  layout(title = '2014 US city populations<br>(Click legend to toggle)', geo= g)


推荐答案

如果你想让大小对应于一个四分位数,那么这是有效的(并且有任何数量的变化都可以做到尺寸更具有分析意义):

If you want the size to correspond to a quartile then this works (and there are any number of variations on this that you could do to make the size more analytically meaningful):

plot_ly(df, lon = lon, lat = lat, text = hover, size = as.numeric(df$q), 
        #marker = list(size = sqrt(pop/10000) + 1, line = list(width = 0)),
        color = q, colors= colors_wanted, type = 'scattergeo', locationmode = 'USA-states') %>%
  layout(title = '2014 US city populations<br>(Click legend to toggle)', geo= g)

这是一个有趣的变化:

plot_ly(df, lon = lon, lat = lat, text = hover, size = aggregate(df$pop,by=list(df$q),sqrt)$x,
        #marker = list(size = sqrt(pop/10000) + 1, line = list(width = 0)),
        color = q, colors= colors_wanted, type = 'scattergeo', locationmode = 'USA-states') %>%
  layout(title = '2014 US city populations<br>(Click legend to toggle)', geo= g)

这篇关于手动更改R中Bubble for Plotly Bubble Map的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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