簡體   English   中英

基於值 R Plotly 的紅色或綠色陰影顏色標記

[英]Color markers in shades of red or green based on value R Plotly

我正在嘗試從測量逆風的戶外活動中獲取 plot 數據。 我想在 map 上顯示此數據,在該活動中,所有具有正逆風的點都以紅色陰影顯示,所有具有負逆風的點都以綠色陰影顯示。 陰影應與逆風強度相對應。 對於headwind == 0的位置,plot 顯示一個沒有填充的標記(只有邊界顏色 = 黑色)。

下圖顯示了我到目前為止所擁有的。 雖然我確實得到了一個 plot,其標記顏色與逆風強度成正比,但我無法將其變為上述陰影或紅色或綠色。 我已經嘗試按照Plotly 網站上的示例使用ifelse(headwind > 0, ... , colorwaycoloraxis ,但無法得到我想要的。我還嘗試通過使用明確地設置標記樣式

marker = list(color = ~headwind, line = list(color = 'black', width = 0.5')

我還想沿路徑顯示一些箭頭來指示方向。 這不是整個賽道上需要的,而是幾個點。

我見過一些類似的問題(例如,在 Plotly 和 R 中以紅色顯示負數)但它們與 staticdess 相關,基於正/負值而不是基於顏色的正/負值我想要什么。

代碼

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

Output

在此處輸入圖像描述

數據

減少到〜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"))

這有點晚了,但可能是一個解決方案:

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的變量之前放置一個負數 (-) 來反轉顏色 map。

如果你這樣做,你可以得到這個結果(沒有添加減號):

在此處輸入圖像描述

上面顯示的是更簡單的路線,並且還允許您獲得顏色條。 如果您想 go 標記樣式路線,我認為您可以通過以下方式進行:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM