简体   繁体   中英

Reposition colorbar in plotly and change color scale (R)

I'm creating a scattermapbox plot using plotly and would like to:

  1. Make the colorbar horizontal and position it on top my plot
  2. Change the colorscale to shades of red and rename it to 'Elevation'

I've tried to follow the examples online but it doesn't work:

  1. I added legend = list(orientation = 'h') to the layout but it made no difference. (2nd example here: https://plotly.com/r/scattermapbox/ )
  2. Added coloraxis = list(colorscale = "Reds") to the layout following the (first) example on https://plotly.com/r/scattermapbox/ without success
  3. Other examples show it is possible to rename the colorbar by adding a named list to marker by marker = list(..., colorbar = list(title = 'My title')) . There's no parameter named colorbar in the reference documentation. (example here: https://plotly.com/r/colorscales/ )

On a side note, I have a mapbox token, yet, the plot appears to work with a few styles only.

Data

Sharing 50 uniformly spaced points from the entire dataset (6000 points):

structure(list(lat = c(45.547955, 45.549801, 45.551437, 45.554653, 
45.559059, 45.560158, 45.563854, 45.567379, 45.5715069, 45.575817, 
45.579056, 45.582672, 45.586857, 45.591194, 45.59362, 45.597103, 
45.601231, 45.605034, 45.608997, 45.611233, 45.615376, 45.61932, 
45.622749, 45.625629, 45.628456, 45.631489, 45.631611, 45.632305, 
45.630135, 45.626793, 45.623497, 45.620045, 45.615589, 45.610992, 
45.606541, 45.602821, 45.599106, 45.595169, 45.591198, 45.5872079, 
45.582672, 45.578587, 45.57476, 45.570515, 45.565872, 45.56226, 
45.55862, 45.555603, 45.552097, 45.548283), lon = c(-73.666939, 
-73.668861, -73.674332, -73.673698, -73.672272, -73.66761, -73.664688, 
-73.661179, -73.660103, -73.658028, -73.657333, -73.654381, -73.652786, 
-73.651154, -73.648354, -73.644836, -73.64328, -73.641556, -73.63961, 
-73.637321, -73.635498, -73.632965, -73.629128, -73.624321, -73.620491, 
-73.615967, -73.613396, -73.61422, -73.618103, -73.622635, -73.627571, 
-73.632332, -73.635414, -73.637466, -73.638702, -73.640244, -73.643547, 
-73.646996, -73.649826, -73.651886, -73.652626, -73.6558, -73.6588359, 
-73.660416, -73.662086, -73.665947, -73.668335, -73.671501, -73.666359, 
-73.667671), ele = c(30.2, 27, 26.6, 25.7999999, 23.2, 26.7999999, 
20, 24, 22.7999999, 20.6, 19, 22.2, 19.2, 17.3999999, 25.2, 25.2, 
17, 16.6, 15.3999999, 17, 15.2, 15.6, 16.3999999, 16.7999999, 
17.7999999, 17.6, 24.3999999, 18.2, 18.6, 18.6, 19.2, 17, 17.2, 
18.7999999, 23.7999999, 27.7999999, 27.7999999, 26, 30.7999999, 
27.2, 29.2, 24, 23.7999999, 26.6, 24.7999999, 26.2, 31, 31, 31.2, 
32.6)), row.names = c(NA, -50L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x7fb33c8118e0>)

Code

plt <- plot_mapbox(data = track, mode = 'scattermapbox',
                   lat = ~lat, lon = ~lon, color = ~ele) %>% 
  layout(mapbox = list(style = 'carto-positron', zoom = 10, # open-street-map works, but light, dark, basic don't work 
                  center = list(lon = track[, mean(lon)], lat = track[, mean(lat)])), 
    margin = list(l = 0, r = 0, t = 0, b = 0), 
    legend = list(orientation = 'h'))

在此处输入图像描述

Use %>% colorbar() with parameters at https://plotly.com/r/reference/#scatter-marker-colorbar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM