2016-10-04 145 views
3

如何在地圖上以圖形方式更改標記尺寸?如果我將size參數設置爲任何數字,它會使其大小相同。如果我將它映射到我的數據中的一個變量,那麼標記將會很小,以便真正能夠區分第一位的差異。理想情況下,我想通過映射到變量來增加基本大小並保持比例方面。設置標記尺寸

重複的例子:

library(data.table) 
library(plotly) 
library(dplyr) 

sample <- data.table(Region=c("Illinois","Illinois","California","California","Texas","Texas"), 
        code=c("IL","IL","CA","CA","TX","TX"), 
        Group=c("A","B"), 
        Value=rnorm(6, mean=100, sd=6)) 

sample[Region=="Illinois", c('lat', 'long') := list(40.3363, -89.0022)] 
sample[Region=="California", c('lat', 'long') := list(36.17, -119.7462)] 
sample[Region=="Texas", c('lat', 'long') := list(31.106, -97.6475)] 


x <- list(
    scope = 'usa', 
    projection = list(type = 'albers usa'), 
    showlakes = F, 
    lakecolor = toRGB('lightblue') 
) 

sample %>% 
    plot_geo(
    locationmode='USA-states' 
) %>% 
    add_markers(
    y=~lat, x=~long, hoverinfo="text", 
    color=~Group, 
    text=~Group, size=~Value 
) %>% 
    layout(
    title='plotly marker map', 
    geo=x 
) 
+0

@哈克-R嗯,對我的作品,而'plot_geo'是在plotly包。可能是你的版本問題? – moman822

+0

@ Hack-R版本4.5。2 – moman822

+0

我想使地圖上的點更大(如果這是什麼不是你的意思是'按鈕') – moman822

回答

3

使用size的問題是,你從字面上指定一個確切的尺寸爲所有的標記。

OTOH您可以使用sizes參數按比例縮放基本大小和像素之間的映射。

例如:

library(data.table) 
library(plotly) 
library(dplyr) 

sample <- data.table(Region=c("Illinois","Illinois","California","California","Texas","Texas"), 
        code=c("IL","IL","CA","CA","TX","TX"), 
        Group=c("A","B"), 
        Value=rnorm(6, mean=100, sd=6)) 

sample[Region=="Illinois", c('lat', 'long') := list(40.3363, -89.0022)] 
sample[Region=="California", c('lat', 'long') := list(36.17, -119.7462)] 
sample[Region=="Texas", c('lat', 'long') := list(31.106, -97.6475)] 


x <- list(
    scope = 'usa', 
    projection = list(type = 'albers usa'), 
    showlakes = F, 
    lakecolor = toRGB('lightblue') 
) 

sample %>% 
    plot_geo(
    locationmode='USA-states' 
) %>% 
    add_markers(
    sizes=c(1000,100), 
    y=~lat, x=~long, hoverinfo="text", 
    color=~Group, 
    text=~Group, size=~Value 
) %>% 
    layout(
    title='plotly marker map', 
    geo=x 
) 

這是地圖是什麼樣子之前的換算(即使用你的問題的代碼):

enter image description here

這是什麼地圖看起來像是之後的縮放比例(即上圖中的代碼):

enter image description here

當然,我非常隨意地選擇了具體的值,你可能需要調整它。我也認爲爲AB指定不同的顏色可能會有所幫助。

這裏就是我使用的顏色,以幫助提高AB之間的視覺差異,同時保持縮放比例爲例:

sample %>% 
    plot_geo(
    locationmode='USA-states' 
) %>% 
    add_markers(
    sizes=c(1000,100), 
    y=~lat, x=~long, hoverinfo="text", 
    color=~Group, 
    colors = c("blue", "yellow"), 
    text=~Group, 
    size=~Value 
) %>% 
    layout(
    title='plotly marker map', 
    geo=x 
) 

enter image description here

另一個變化:

enter image description here

和另一個:

enter image description here

4

最簡單的,而且很可能規範的方法是使用marker.sizeref屬性。你裏面marker=list(...)把這個包像這樣

plot_geo(sample, locationmode='USA-states') %>% 
    add_markers(y=~lat, x=~long, hoverinfo="text", 
    color=~Group, text=~Group, size=~Value, 
    marker=list(sizeref=0.1, sizemode="area")) %>% 
    layout(title='plotly marker map', geo=x) 

enter image description here

需要注意的是,小sizeref,標記越大。 E.g與sizeref=0.02我們得到

enter image description here

+0

@ moman822這是否解決你的問題btw?如果有幫助,請隨時註冊並/或接受此解決方案。 – dww

+0

嗨,有沒有辦法將尺寸標註爲圖例,以便表示尺寸/尺寸的理解含義? – Ben

+0

@你應該問,作爲一個新的問題 – dww