R 创建离散颜色轴
我正在地图上绘制浓度数据,我的数据如下所示:R 创建离散颜色轴,r,ggplot2,plot,color-scheme,ggmap,R,Ggplot2,Plot,Color Scheme,Ggmap,我正在地图上绘制浓度数据,我的数据如下所示: 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(ma
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))
我想在我的地图上添加一个带标签的颜色轴,带有离散的正方形。有人能帮我吗
我附上了我的想法-这是从(这个问题没有帮助我,因为我正在使用ggmap
)
谢谢大家! 你应该让
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)
#导入软件包
需要(TIBLE)
需要(dplyr)
需要(ggmap)
要求(RColorBrewer)
#加载数据
df_avg我建议让ggplot()
为您做美学贴图:因为您自己做了贴图(我假设这里有某种Conc->color
贴图?)ggplot2无法自动为您生成图例。请看scale\u fill\u manual()Hi Shadow,谢谢您的回答:)这部分是什么意思?label=字母[1:11]在我的较大数据集中无法使用此标签这些是您要用于颜色的标签。也就是说,a
对应于颜色“#67001F”
,b
对应于“#B2182B”
,以此类推。
# 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)