Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

r - Change fill colour of polygon with ggplot2

I have a simple question which I've asked myself many times already. When I am plotting one or more polygons in R using ggplot2, how can I actually change the filling colour? I do not only want to change the outline of the polygon, but the whole thing.

In my first simple plots (ea_map and hb_map) it works fine to assign the colours "yellow" and "purple", however in my final plot "ea_hb_map" (which I've pictured below), the colours are set back to default.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...