簡體   English   中英

如何在 R 中的 plotly scattergeo 中隱藏或至少自定義顏色條

[英]How to hide, or at least customize colorbar in plotly scattergeo in R

我想創建一個帶有大寫標記的散點圖。 這些標記的大小和顏色與數據庫值有關。

如果我使用標准顏色,一切順利:地圖顯示正確,尺寸合適,顏色不同,沒有圖例(我在標記上的懸停文本中添加信息國家:值)。

但是,如果我在我的 scattergeo 圖中使用使用顏色功能的自定義調色板,則始終顯示顏色條。 Showlegend=F 和 Showscale=F 沒有幫助。 由於我刪除了顏色,所以顏色條消失了。 此外,如果我嘗試自定義它(例如更改標題或刻度標簽的格式),它不起作用。

換句話說,我沒有在這個顏色條上嘗試過任何選項!

這是我的數據:

structure(list(ISO3 = c("ARG", "AUS", "AUT", "BEL", "BRA", "CAN", 
"CHE", "CHL", "CHN", "COK", "DEU", "DNK", "ESP", "FIN", "FJI", 
"FRA", "GBR", "HKG", "IDN", "IND", "ITA", "JPN", "KOR", "LUX", 
"MEX", "MYS", "NCL", "NLD", "NOR", "NZL", "PHL", "PRT", "RUS", 
"SGP", "SWE", "THA", "TON", "USA", "WSM"), Total = c(1073L, 8204L, 
818L, 1502L, 1871L, 7958L, 3524L, 2456L, 3345L, 456L, 5010L, 
569L, 2775L, 184L, 75L, 60382L, 4424L, 415L, 146L, 405L, 8369L, 
8176L, 1034L, 235L, 961L, 137L, 6522L, 667L, 309L, 7960L, 238L, 
316L, 486L, 404L, 480L, 200L, 41L, 85225L, 46L), Size = c(16, 
30, 14, 18, 19, 30, 24, 21, 23, 12, 26, 13, 22, 8, 5, 50, 25, 
11, 7, 11, 30, 30, 16, 9, 15, 7, 28, 13, 10, 30, 9, 10, 12, 11, 
12, 8, 3, 54, 4), Color = c(3, 4, 3, 3, 3, 4, 4, 3, 4, 3, 4, 
3, 3, 2, 2, 5, 4, 3, 2, 3, 4, 4, 3, 2, 3, 2, 4, 3, 2, 4, 2, 2, 
3, 3, 3, 2, 2, 5, 2), ISO2 = c("AR", "AU", "AT", "BE", "BR", 
"CA", "CH", "CL", "CN", "CK", "DE", "DK", "ES", "FI", "FJ", "FR", 
"GB", "HK", "ID", "IN", "IT", "JP", "KR", "LU", "MX", "MY", "NC", 
"NL", "NO", "NZ", "PH", "PT", "RU", "SG", "SE", "TH", "TO", "US", 
"WS"), LABELFR = c("Argentine", "Australie", "Autriche", "Belgique", 
"Brésil", "Canada", "Suisse", "Chili", "Chine", "Iles Cook", 
"Allemagne", "Danemark", "Espagne", "Finlande", "Fidji", "France", 
"Royaume-Uni", "Hong-kong, Chine", "Indonésie", "Inde", "Italie", 
"Japon", "Corée, République de", "Luxembourg", "Mexique", "Malaisie", 
"Nouvelle-Calédonie", "Pays-Bas", "Norvège", "Nouvelle-Zélande", 
"Philippines", "Portugal", "Russie, Fédération de", "Singapour", 
"Suède", "Thaïlande", "Tonga", "Etats-Unis", "Samoa"), LABELEN = c("Argentina", 
"Australia", "Austria", "Belgium", "Brazil", "Canada", "Switzerland", 
"Chile", "China", "Cook Islands", "Germany", "Denmark", "Spain", 
"Finland", "Fiji", "France", "United Kingdom", "Hong Kong", "Indonesia", 
"India", "Italy", "Japan", "South Korea", "Luxembourg", "Mexico", 
"Malaysia", "New Caledonia", "Netherlands", "Norway", "New Zealand", 
"Philippines", "Portugal", "Russia", "Singapore", "Sweden", "Thailand", 
"Tonga", "United States", "Samoa"), CAPITAL = c("Buenos Aires", 
"Canberra", "Vienna", "Brussels", "Brasilia", "Ottawa", "Bern", 
"Santiago", "Beijing", "Avarua", "Berlin", "Copenhagen", "Madrid", 
"Helsinki", "Suva", "Paris", "London", "N/A", "Jakarta", "New Delhi", 
"Rome", "Tokyo", "Seoul", "Luxembourg", "Mexico City", "Kuala Lumpur", 
"Noumea", "Amsterdam", "Oslo", "Wellington", "Manila", "Lisbon", 
"Moscow", "Singapore", "Stockholm", "Bangkok", "Nuku'alofa", 
"Washington", "Apia"), LATITUDE = c("-34.583333333333336", "-35.266666666666666", 
"48.2", "50.833333333333336", "-15.783333333333333", "45.416666666666664", 
"46.916666666666664", "-33.45", "39.916666666666664", "-21.2", 
"52.516666666666666", "55.666666666666664", "40.4", "60.166666666666664", 
"-18.133333333333333", "48.86666666666667", "51.5", "0", "-6.166666666666667", 
"28.6", "41.9", "35.68333333333333", "37.55", "49.6", "19.433333333333334", 
"3.1666666666666665", "-22.266666666666666", "52.35", "59.916666666666664", 
"-41.3", "14.6", "38.71666666666667", "55.75", "1.2833333333333332", 
"59.333333333333336", "13.75", "-21.133333333333333", "38.883333", 
"-13.816666666666666"), LONGITUDE = c("-58.666667", "149.133333", 
"16.366667", "4.333333", "-47.916667", "-75.700000", "7.466667", 
"-70.666667", "116.383333", "-159.766667", "13.400000", "12.583333", 
"-3.683333", "24.933333", "178.416667", "2.333333", "-0.083333", 
"0.000000", "106.816667", "77.200000", "12.483333", "139.750000", 
"126.983333", "6.116667", "-99.133333", "101.700000", "166.450000", 
"4.916667", "10.750000", "174.783333", "120.966667", "-9.133333", 
"37.600000", "103.850000", "18.050000", "100.516667", "-175.200000", 
"-77.000000", "-171.766667"), CONTINENT = c("South America", 
"Australia", "Europe", "Europe", "South America", "Central America", 
"Europe", "South America", "Asia", "Australia", "Europe", "Europe", 
"Europe", "Europe", "Australia", "Europe", "Europe", "Asia", 
"Asia", "Asia", "Europe", "Asia", "Asia", "Europe", "Central America", 
"Asia", "Australia", "Europe", "Europe", "Australia", "Asia", 
"Europe", "Europe", "Asia", "Europe", "Asia", "Australia", "Central America", 
"Australia")), class = c("data.table", "data.frame"), row.names = c(NA, 
-39L), .internal.selfref = <pointer: 0x000001ffb6417970>, sorted = "ISO3")

這是我的代碼:

fig <- plot_ly(
  type = 'scattergeo',
  showlegend=F,
  mode='markers',
  data=TOUR,
  y=~LATITUDE,
  x=~LONGITUDE,
  text=sprintf("%s : %s",TOUR$LABELFR,TOUR$Total),
  hovertemplate = "%{text}<extra></extra>",
  colors=c(ispfPalette[c(9,4,2)]),
  color=~Color,
  marker=list(
    showscale=F,
    size=~Size,
    reversescale=F
    )
  )

這是我的輸出:

在此處輸入圖像描述

最好的解決方案是完全隱藏顏色欄,但我也很好奇如何通過更改標題和格式化值來自定義它(例如在 % 的情況下或如果我想更改小數分隔符)。

謝謝你的幫助 !

應要求更新

您可以修改colors參數中的顏色欄,例如:

fig <- plot_ly(
  type = 'scattergeo',
  showlegend=F,
  mode='markers',
  data=TOUR,
  y=~LATITUDE,
  x=~LONGITUDE,
  text=sprintf("%s : %s",TOUR$LABELFR,TOUR$Total),
  hovertemplate = "%{text}<extra></extra>",
  colors="YlOrRd",
 #colors = c("#1B98E0","black"),
  color=~Color,
  marker=list(
    showscale=F,
    size=~Size,
    reversescale=F
  ) 
)
fig

colors="YlOrRd" 在此處輸入圖像描述

colors = c("#1B98E0","black") 在此處輸入圖像描述

原始答案:只需在代碼末尾添加: %>% hide_colorbar

fig <- plot_ly(
  type = 'scattergeo',
  showlegend=F,
  mode='markers',
  data=TOUR,
  y=~LATITUDE,
  x=~LONGITUDE,
  text=sprintf("%s : %s",TOUR$LABELFR,TOUR$Total),
  hovertemplate = "%{text}<extra></extra>",
  colors=c(ispfPalette[c(9,4,2)]),
  color=~Color,
  marker=list(
    showscale=F,
    size=~Size,
    reversescale=F
    )
  ) %>% hide_colorbar()

在此處輸入圖像描述

暫無
暫無

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

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