缩放点并使用plot_ly R包选择颜色

时间:2017-03-08 02:26:37

标签: r plotly

我刚刚开始在R中使用plotly并且它工作得很好但我无法弄清楚如何做两件事。

1)我需要选择分裂的颜色。目前,我正在被Territory分裂,并且我不允许我编码我想要每个领域的颜色。 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 个答案:

答案 0 :(得分: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