r - 使用 ggplot2 更改多边形的填充颜色
问题描述
我有一个简单的问题,我已经问过自己很多次了。当我使用 ggplot2 在 R 中绘制一个或多个多边形时,如何实际更改填充颜色?我不仅想改变多边形的轮廓,还想改变整个事物。
在我的第一个简单绘图(ea_map 和 hb_map)中,可以很好地分配颜色“黄色”和“紫色”,但是在我的最终绘图“ea_hb_map”(如下图所示)中,颜色被设置回默认值。
ea <- readOGR("C:/Users/BASELINE/Eastern Arctic/Summer/2010_EA_S/cis_SGRDREA_20100628_pl_a.shp")
hb <- readOGR("C:/Users/BASELINE/Hudson Bay/Summer/2010_HB_S/cis_SGRDRHB_20100628_pl_a.shp")
ea_map <- ggplot() +
geom_polygon(data=ea, aes(x = long, y = lat, group = group), fill = "red")
plot(ea_map)
hb_map <- ggplot() +
geom_polygon(data=hb, aes(x = long, y = lat, group = group), fill = "purple")
plot(hb_map)
ea_df <- tidy(ea)
hb_df <- tidy(hb)
eastern_arctic_map <- ea_map +
geom_sf(data=world, fill = "antiquewhite1") +
coord_sf(xlim = c(-115, -50), ylim = c(50,83), expand = FALSE)+
scale_y_continuous(breaks = c(50, 60, 70, 80)) +
scale_x_continuous(breaks = c(-50, -70, -90, -110)) +
geom_polygon(data = ea_df, aes(x=long, y=lat, group=group, fill="Eastern Arctic"), alpha=0.4) +
labs(fill = "",
x = "lon",
y = "lat") +
theme_grey(base_size = 9) +
theme(legend.key.size = unit(0.8,"line"))
print(eastern_arctic_map)
ea_hb_map <- eastern_arctic_map +
geom_polygon(data=hb_df, aes(x = long, y = lat, group = group, fill = "Hudson Bay"), alpha=0.4)
print(ea_hb_map)
解决方案
推荐阅读
- git - 如何在 git 分支中获取给定作者的提交消息历史记录?
- bots - 聊天机器人的默认消息
- bash - 查找文件的一行,提取其中的一部分并在 Bash 中的同一位置替换
- node.js - Sequelize COUNT 在 INCLUDE 关联返回错误聚合函数或 GROUP BY 子句
- vb.net - 如何将主要重复错误更改为窗口形式的消息框
- javascript - Selenium:捕获 DOM 元素图像
- docker - 使用另一个镜像运行测试用例的 Docker 镜像
- c - 连接客户端/服务器以进行 UDP 广播的正确方法
- r - R删除所有以字符串开头的列都有NA的行
- python - 通过 Python 中的节俭服务器 JDBC 在 AWS EMR Spark 集群上执行 SQL?