手动更改 R 中 Plotly Bubble Map 的气泡大小
Manually changing the size of the Bubbles for Plotly Bubble Map in R
我目前正在尝试手动更改 Plotly 气泡图的气泡大小。我成功地使用提供的数据更改了地图的颜色,但我无法使用相同的逻辑来更改大小。要更改颜色,我只需调用:colors_wanted <- c("red", "blue", "black", "pink")
并将此命令传递给 plot_ly
内的 colors
。您认为在这种情况下 sqrt
可以更改尺寸而不是使用公式来声明尺寸吗?
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)
如果您希望大小与四分位数相对应,则此方法可行(并且您可以对此进行任意数量的变体以使大小在分析上更有意义):
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)
我目前正在尝试手动更改 Plotly 气泡图的气泡大小。我成功地使用提供的数据更改了地图的颜色,但我无法使用相同的逻辑来更改大小。要更改颜色,我只需调用:colors_wanted <- c("red", "blue", "black", "pink")
并将此命令传递给 plot_ly
内的 colors
。您认为在这种情况下 sqrt
可以更改尺寸而不是使用公式来声明尺寸吗?
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)
如果您希望大小与四分位数相对应,则此方法可行(并且您可以对此进行任意数量的变体以使大小在分析上更有意义):
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)