基于值 R Plotly 的红色或绿色阴影颜色标记

Posted

技术标签:

【中文标题】基于值 R Plotly 的红色或绿色阴影颜色标记【英文标题】:Color markers in shades of red or green based on value R Plotly 【发布时间】:2020-09-19 02:29:13 【问题描述】:

我正在尝试绘制测量逆风的户外活动的数据。我想在活动发生的地图上显示这些数据,所有具有正逆风的点以红色阴影显示,所有具有负逆风的点以绿色阴影显示。阴影应与逆风强度相对应。对于 headwind == 0 的位置,绘图显示一个没有填充的标记(只有边界颜色 = 黑色)。

下图显示了我目前所拥有的。虽然我确实得到了一个标记颜色与逆风强度成正比的图,但我无法将其变为上述阴影或红色或绿色。我按照Plotly website 上的示例尝试过使用ifelse(headwind > 0, ...colorwaycoloraxis,但无法得到我想要的。我还尝试使用

显式设置标记样式
marker = list(color = ~headwind, line = list(color = 'black', width = 0.5')

我还想沿路径显示一些指示方向的箭头。这不是整条赛道都需要的,而是几个点。

我见过一些类似的问题(例如Show negatives in Red in Plotly and R),但它们与基于正/负值的静态项圈有关,而不是“取决于值的颜色阴影”——这正是我想要的。

代码

plot_mapbox(data = track, mode = 'scattermapbox',
                   lat = ~lat, lon = ~lon, color = ~headwind, coloraxis = 'coloraxis' )%>% 
  layout(mapbox = list(style = 'light', zoom = 10, 
                       center = list(lon = track[, mean(lon)], lat = track[, mean(lat)])), 
         margin = list(l = 0, r = 0, t = 0, b = 0), 
         legend = list(orientation = 'h'), 
         coloraxis=list(colorscale='Reds'))

输出

数据

减少到约 100 分。

structure(list(lat = c(45.2924274, 45.2919851, 45.2932087, 45.2914594, 
45.2931871, 45.2905293, 45.2867926, 45.2851495, 45.2806734, 45.2768259, 
45.2768488, 45.274695, 45.2725324, 45.2683376, 45.2629192, 45.2602198, 
45.2579205, 45.2577605, 45.2569129, 45.2564982, 45.2569011, 45.256326, 
45.2548296, 45.2537323, 45.2491721, 45.2440505, 45.2396301, 45.235752, 
45.2338643, 45.231723, 45.2295332, 45.2278063, 45.2290903, 45.2274071, 
45.2302112, 45.2331995, 45.2313716, 45.2292849, 45.2288604, 45.2275476, 
45.224561, 45.2245779, 45.2247184, 45.2256043, 45.2282358, 45.2316037, 
45.2339442, 45.2376578, 45.2382877, 45.2350226, 45.2402104, 45.2394283, 
45.2408562, 45.2449018, 45.2491195, 45.2527104, 45.257015, 45.262031, 
45.2671074, 45.2724731, 45.2757569, 45.2786716, 45.2847369, 45.2911555, 
45.296165, 45.3023245, 45.3093462, 45.3128727, 45.3176175, 45.3182203, 
45.3172247, 45.3150336, 45.3143277, 45.3137151, 45.3117404, 45.3092333, 
45.3056604, 45.3033563, 45.3015722, 45.3011463, 45.2999077, 45.299969, 
45.300848, 45.2999944, 45.2979399, 45.2949387, 45.2932506, 45.2922789, 
45.2900246, 45.2906673, 45.2925133, 45.292688, 45.2932092, 45.2932651, 
45.2925192, 45.2916088, 45.2920727, 45.293156, 45.2933158, 45.2928059, 
45.2912818), lon = c(-74.0511001, -74.0510721, -74.0480827, -74.0462761, 
-74.0497021, -74.0510732, -74.0562937, -74.0624753, -74.0680052, 
-74.0731452, -74.0812064, -74.0878658, -74.0933321, -74.0972941, 
-74.1011036, -74.1074512, -74.1138247, -74.1192535, -74.1262533, 
-74.1305444, -74.1349428, -74.1364462, -74.142992, -74.1492447, 
-74.1498239, -74.1456689, -74.1418102, -74.1448902, -74.13985, 
-74.1319085, -74.1219148, -74.1147096, -74.1142337, -74.1137429, 
-74.1151895, -74.1128846, -74.1031788, -74.0925199, -74.0834883, 
-74.0747626, -74.071491, -74.0625856, -74.0538449, -74.0455299, 
-74.0373525, -74.0315021, -74.0245414, -74.0185206, -74.0100138, 
-74.0048819, -74.0079128, -74.000793, -73.9929405, -73.9870303, 
-73.9807194, -73.9753671, -73.9691101, -73.9631622, -73.9575471, 
-73.952474, -73.9547135, -73.9567679, -73.9520204, -73.9469607, 
-73.9407129, -73.9385812, -73.9353202, -73.9335782, -73.9332331, 
-73.9412661, -73.9464184, -73.9518319, -73.9598527, -73.9665792, 
-73.973165, -73.9797307, -73.9858473, -73.9921346, -73.9987069, 
-74.0049949, -74.0106624, -74.0172195, -74.0226944, -74.0274091, 
-74.0317759, -74.0348736, -74.0381058, -74.0403379, -74.044471, 
-74.0499726, -74.0510617, -74.0510679, -74.0497654, -74.0481948, 
-74.0470505, -74.0462857, -74.0468899, -74.0477243, -74.0493063, 
-74.0506922, -74.0508461), headwind = c(2.76940699529868, -4.29064844726939, 
6.32939665765863, -2.70212357865094, -2.09430865625362, -2.94437152200737, 
3.68039781522879, -2.75460681090356, -2.07118995329537, -3.89223400006753, 
-5.6124577618958, -3.23990330748452, -2.88799894880197, 1.44386194468002, 
-1.62757112099828, -3.96534234224143, -5.58548285989353, -5.23546206473328, 
-5.04334794153736, -5.56582086006945, -6.39713309671529, -2.73204215445971, 
-2.78247155160577, -5.17465320535599, 5.28479649375971, 5.39349119652677, 
0.849065796538837, 6.1572886165794, 6.47933760909835, 6.28990051645839, 
6.47828306146309, -5.95187923093366, -3.11387204666763, 6.52730933870623, 
4.53187908395984, 6.1914653496707, 6.47203610314572, 6.51460622049559, 
3.92334577299324, 5.33127767516553, 6.16537629683053, 6.14743204861233, 
5.75787435417292, 5.18990225296579, 0.031201973853795, 4.0060340212202, 
3.72671304639426, 3.68803811795611, 6.28794462723367, 6.13432766841712, 
-3.83797593187167, 6.20074522372471, 2.25019132086052, 2.34232979604033, 
2.46012726189235, 2.81030573673775, 2.29081004114046, 2.47674006029921, 
1.69579455567582, 1.05875647894816, -5.71328877613563, 0.400325286795701, 
0.750303681038721, 0.815375623113835, 4.24236675877916, -1.31079219711205, 
0.110210983368552, -0.0828764225562778, -3.91850736541859, -5.18122542043517, 
-1.39587089138698, -4.94418957442122, -5.04101231364248, -5.07277220131849, 
-3.81628803351557, -3.56330772557761, -2.39925134381422, -4.10818544442121, 
-4.05009025319977, -3.84240671983316, -5.08635597161047, -5.12449622177523, 
-4.74598802026557, -5.17604073914986, -0.0645565019448928, -1.8515420821822, 
-2.87236823446881, -4.11528743158025, -3.97830983132787, -5.13844836391256, 
2.23381424164358, 1.08931403267591, 3.36153935085126, 5.1383423986223, 
1.63143963861697, 5.06951911447557, -4.33050440827119, -5.04189097530029, 
-4.78806209512054, -4.62849978544911, 2.3653185162215)), row.names = c(NA, 
-101L), class = c("data.table", "data.frame"))

【问题讨论】:

【参考方案1】:

这有点晚了,但可能是一个解决方案:

plot_mapbox(data = track, mode = 'scattermapbox',
                   lat = ~lat, lon = ~lon, color = ~headwind, colors = 'RdBu' )%>% 
  layout(mapbox = list(style = 'light', zoom = 10, 
                       center = list(lon = track[, mean(lon)], lat = track[, mean(lat)])), 
         margin = list(l = 0, r = 0, t = 0, b = 0), 
         legend = list(orientation = 'h'))

由于我不知道你是否想要另一种颜色,你应该看看这里(https://plotly.com/python/builtin-colorscales/)。一种选择是将RdBu 更改为PiYG。另请注意,您可以通过在您定义为 color 的变量之前放置一个负数 (-) 来反转颜色图。

如果你这样做,你可以得到这个结果(不加减号):

上面显示的是更简单的路线,并且还允许您获得颜色条。如果您想走标记样式路线,我认为您可以通过以下方式进行:

plot_mapbox(data = track, 
            mode = 'scattermapbox',
            lat = ~lat, 
            lon = ~lon, 
            color = ~headwind, 
            markers = list (
                  colorscale = "RdBu",
                  line = list(
                        color = 'black', 
                        width = 0.5))) %>% 
  layout(mapbox = list(style = 'light', zoom = 10, 
                       center = list(lon = track[, mean(lon)], lat = track[, mean(lat)])), 
         margin = list(l = 0, r = 0, t = 0, b = 0), 
         legend = list(orientation = 'h')) %>%
  hide_colorbar()

【讨论】:

以上是关于基于值 R Plotly 的红色或绿色阴影颜色标记的主要内容,如果未能解决你的问题,请参考以下文章

RGB与CMYK色彩原理

----颜色版----

vue背景透明度消失

Python:如何使用plotly制作阴影区域或交替背景颜色?

基于数字的颜色编码

使用 RGB 值设置颜色