2017-03-08 38 views
1

我刚开始在R中使用绘图,它工作得很好,但我无法弄清楚如何做两件事。缩放点并用plot_ly选择颜色R包

1)我需要选择我的分割的颜色。目前,我由领土分裂,并且不允许我编码我想要每个领土是什么颜色。 2)我还需要调整点数,以便某些标记非常大。我试着为每一行创建一个尺寸,并设置size =〜size和sizes = c(2,100),但这不起作用。

有关如何做到这一点的任何建议?我试着阅读剧情的R参考指南,但不知道如何用plotly_mapbox来做到这一点。我粘贴了我的代码,但没有进行大小或颜色尝试,因为我无法使其工作。

p <- df %>% 
    plot_mapbox(lat = ~lat, lon = ~lon, 
       split = ~Territory, 
       mode = 'scattermapbox', 
       text = df$text, 
       hoverinfo = "text" 
      ) %>% 
    layout(title = 'Ship to Zip Codes', 
     font = list(color='white'), 
     plot_bgcolor = '#191A1A', 
     paper_bgcolor = '#191A1A', 
     mapbox = list(style = 'dark'), 
     legend = list(orientation = 'h', 
        font = list(size = 8)), 
     margin = list(l = 25, r = 25, 
         b = 25, t = 25, 
         pad = 2)) 

回答

1

您可以通过marker = list(size = 2)设置标记大小。

设置颜色比较棘手,不能直接用plot_mapbox来完成,据我所知。


但是,我们可以分配一个新列到我们的数据帧

df$colors <- factor(df$class, levels = unique(df$class)) 

然后我们自己定义的颜色列表

cols <- c("red", "blue", "black", "green", "orange", "cyan", "gray50") 

,并最终通过plot_geo

plot_geo(df) %>% 
    add_markers(
    x = ~reclong, y = ~reclat, color = ~colors, colors = cols, marker = list(size = 2)) 
绘制一切

整个代码在Plotly中获取散点图中的自定义颜色。

library(plotly) 

df = read.csv('https://raw.githubusercontent.com/bcdunbar/datasets/master/meteorites_subset.csv') 

df$colors <- factor(df$class, levels = unique(df$class)) 
cols <- c("red", "blue", "black", "green", "orange", "cyan", "gray50") 

plot_geo(df) %>% 
add_markers(x = ~reclong, 
      y = ~reclat, 
      color = ~df$colors, 
      colors = cols, 
      marker = list(size = 2)) 

enter image description here