我正在尝试创建一个图例,以指示该图如何进行颜色编码。但是,此图例未显示。
这与当前已占用整个空间的地块有关吗?
这是我下面的代码,用于创建情节(并尝试添加图例):
cols <- c("50 miles"="#B10026","100 miles"="#FC4E2A","150 miles"="#FEB24C", "200 miles"="#FFEDA0")
hurricane_plot <- ggplot(data = US_subset_counties) +
geom_polygon(mapping = aes(x = long, y = lat, group = group), size = 0.2, color="gray", fill=NA) +
geom_polygon(data=counties_200, aes(x = long, y = lat, group = group), size = 0.2, fill="#FFEDA0") +
geom_polygon(data=counties_150, aes(x = long, y = lat, group = group), size = 0.2, fill="#FEB24C") +
geom_polygon(data=counties_100, aes(x = long, y = lat, group = group), size = 0.2, fill="#FC4E2A") +
geom_polygon(data=counties_50, aes(x = long, y = lat, group = group), size = 0.2, fill="#B10026") +
geom_polygon(data=US_subset_states, aes(x = long, y = lat, group = group), size = 0.2, color = "black", fill=NA) +
theme_bw() +
theme(panel.background = element_blank()) +
scale_colour_manual(name="Distance From Radius",values=cols)
hurricane_plot
谢谢!
编辑:
@Ben发表评论后,我尝试将填充物放入es内无济于事。但是,当我将填充更改为col并使用了辅助线时,这是我得到的结果:
我如何将框和标签的颜色更改为上面的cols矢量,并且有一种方法可以删除绘图上的边框颜色?
这是我更新的代码:
hurricane_plot <- ggplot(data = US_subset_counties) +
geom_polygon(mapping = aes(x = long, y = lat, group = group), size = 0.2, color="gray", fill=NA) +
geom_polygon(data=counties_200, aes(x = long, y = lat, group = group, color="#FFEDA0"), size = 0.2, fill="#FFEDA0") +
geom_polygon(data=counties_150, aes(x = long, y = lat, group = group, color="#FEB24C"), size = 0.2, fill="#FEB24C") +
geom_polygon(data=counties_100, aes(x = long, y = lat, group = group, color="#FC4E2A"), size = 0.2, fill="#FC4E2A") +
geom_polygon(data=counties_50, aes(x = long, y = lat, group = group, color="#B10026"), size = 0.2, fill="#B10026") +
geom_polygon(data=US_subset_states, aes(x = long, y = lat, group = group), size = 0.2, color = "black", fill=NA) +
theme_bw() + theme(panel.background = element_blank()) +
guides(color=guide_legend("Distance From Radius"))
答案 0 :(得分:2)
@Alan-
这是子集美国州数据后可能需要的可复制示例。就像您一样,它使用cols
,aes中的fill
和scale_fill_manual
。让我知道您是否打算这样做,希望对您有所帮助。
library(maps)
us_states <- map_data("state")
states1 <- subset(us_states, region %in% c("california", "oregon", "washington"))
states2 <- subset(us_states, region %in% c("illinois", "indiana", "iowa"))
states3 <- subset(us_states, region %in% c("new york", "massachusetts", "connecticut"))
cols <- c("West"="#B10026","Midwest"="#FC4E2A","Northeast"="#FEB24C")
state_plot <- ggplot(data = us_states) +
geom_polygon(data=us_states, aes(x = long, y = lat, group = group), size = 0.2) +
geom_polygon(data=states1, aes(x = long, y = lat, group = group, fill="West"), size = 0.2) +
geom_polygon(data=states2, aes(x = long, y = lat, group = group, fill="Midwest"), size = 0.2) +
geom_polygon(data=states3, aes(x = long, y = lat, group = group, fill="Northeast"), size = 0.2) +
theme_bw() +
theme(panel.background = element_blank()) +
scale_fill_manual(name="Area of Country", values=cols)
state_plot