Scaling points and choosing colors with plot_ly R package

有些话、适合烂在心里 提交于 2020-01-05 07:12:40

问题


I have just started using plotly in R and it is working great but I cannot figure out how to do two things.

1) I need to pick the colors of my split. Currently, I am splitting by Territory and plotly doesn't allow me to code what colors I want each territory to be. 2) I also need to scale the points so that some markers are very large. I tried creating a size for each row and setting size = ~size and sizes = c(2, 100) but this did not work.

Any advice on how to do this? I've tried reading the plotly R reference guide but cannot figure out how to do this with plotly_mapbox. I've pasted my code without the size or color attempt because I could never get it to work.

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:


You can set the marker size via marker = list(size = 2).

Setting the colors is more tricky and cannot be done directly with plot_mapbox as far as I know.


But we can assign a new column to our data frame

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

then define our own color list

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

and finally plot everything via plot_geo

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

The whole code to get the custom colors in a scatter map in 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))



来源:https://stackoverflow.com/questions/42661873/scaling-points-and-choosing-colors-with-plot-ly-r-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!