【问题标题】:Create discrete colour axis创建离散色轴
【发布时间】:2018-07-25 02:52:02
【问题描述】:

我在地图上绘制浓度数据,我的数据如下所示:

   Lat         Long         Conc        Colour
-33.90624     151.2237       10.0        #4393C3
-33.92404     151.2280       12.95       #92C5DE
-33.92384     151.2275       14.0        #D1E5F0

在地图上绘制使用:

map <- ggmap(map)+ 
   scale_x_continuous(limits = c(151.220, 151.230), expand = c(0, 0)) +
   scale_y_continuous(limits = c(-33.927, -33.902), expand = c(0, 0))

map + 
   geom_point(data = df_avg, aes(x = df_avg$Long, y = df_avg$Lat), 
       col = df_avg$Colour, cex = 4.2) + 
   ggtitle(paste0("PM2.5 (ug/m3)", "    ", title_start,"  -  ", title_end))

我使用的色阶是:(“RdBu”RColorBrewer 调色板)

"#67001F" "#B2182B" "#D6604D" "#F4A582" "#FDDBC7" 
   "#F7F7F7" "#D1E5F0" "#92C5DE" "#4393C3" "#2166AC" "#053061"

我想在我的地图一侧添加一个带有离散方块的标记颜色轴。谁能帮我这个?

我附上了我所想的a picture - 这是取自R ggplot2 discrete colour palette for gradient map(这个问题对我没有帮助,因为我正在使用ggmap

谢谢!

【问题讨论】:

  • 我建议让ggplot() 为你做美学映射:因为你自己做了映射(我假设某种Conc -&gt; Colour 映射在这里?)ggplot2 无法自动为您生成图例。
  • 看scale_fill_manual()

标签: r ggplot2 plot color-scheme ggmap


【解决方案1】:

您应该让ggplot 按照@MikkoMartillia 的建议为您进行颜色映射。然后你会得到一个自动图例。下面我根据您的数据创建了一个可重现的示例。首先,我将颜色标签添加到数据中。然后我基本上使用你的代码来创建情节,但在aes 调用中移动了颜色。最后,我使用"RdBu" 调色板和正确的限制添加了一个色标。

# import packages
require(tibble)
require(dplyr)
require(ggmap)
require(RColorBrewer)

# load data
df_avg <- tribble(~Lat, ~Long, ~Conc, ~Colour,
                  -33.90624, 151.2237, 10.0, "#4393C3",
                  -33.92404, 151.2280, 12.95, "#92C5DE",
                  -33.92384, 151.2275, 14.0, "#D1E5F0")

# add colour to data
df_labels <- tibble(label = letters[1:11], # change this to sensible labels
                    Colour = brewer.pal(11,  "RdBu"))
df_avg <- left_join(df_avg, df_labels)

# download map
map <- get_map(location = c(lon = 151.225, lat = -33.913), zoom = 14)
# map plot
p_map <- ggmap(map)+ 
  scale_x_continuous(limits = c(151.220, 151.230), expand = c(0, 0)) +
  scale_y_continuous(limits = c(-33.927, -33.902), expand = c(0, 0))
# add points to map
p_map + 
  geom_point(data = df_avg, aes(x = Long, y = Lat, color = label), cex = 4.2) +
  scale_color_brewer(palette = "RdBu", limits = df_labels$label) 

【讨论】:

  • 嗨,Shadow,感谢您的回复:) 这部分是什么意思? label = letters[1:11] 无法让它适用于我更大的数据集
  • 这些是您要用于颜色的标签。 IE。 a对应颜色"#67001F"b对应"#B2182B"等等。
猜你喜欢
  • 2017-01-28
  • 2019-10-26
  • 1970-01-01
  • 1970-01-01
  • 2019-04-21
  • 2014-05-23
  • 2022-01-02
  • 1970-01-01
  • 2016-04-07
相关资源
最近更新 更多