首页 > 解决方案 > 使用 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)

ea_hb_map

标签: rggplot2polygon

解决方案


推荐阅读