简体   繁体   English

重新定位 plotly 中的颜色条并更改色标 (R)

[英]Reposition colorbar in plotly and change color scale (R)

I'm creating a scattermapbox plot using plotly and would like to:我正在使用 plotly 创建一个scattermapbox plotly并希望:

  1. Make the colorbar horizontal and position it on top my plot使颜色条水平并将 position 它放在我的 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.我在layout中添加了legend = list(orientation = 'h') ,但没有任何区别。 (2nd example here: https://plotly.com/r/scattermapbox/ ) (这里的第二个例子: 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 successhttps://plotly.com/r/scattermapbox/上的(第一个)示例之后将coloraxis = list(colorscale = "Reds")添加到layout中,但没有成功
  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')) .其他示例显示可以通过将命名列表添加到marker来重命名颜色条 marker = list(..., colorbar marker = list(..., colorbar = list(title = 'My title')) There's no parameter named colorbar in the reference documentation.参考文档中没有名为colorbar的参数。 (example here: https://plotly.com/r/colorscales/ ) (此处示例: 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.附带说明一下,我有一个 mapbox 令牌,然而,plot 似乎只适用于少数 styles。

Data数据

Sharing 50 uniformly spaced points from the entire dataset (6000 points):从整个数据集中共享 50 个均匀分布的点(6000 个点):

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'))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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