我正在绘制多个图,其中包含相同的4个离散值,未知,负,中性,正。
但是,并非所有数据集都包含所有离散值,因此
在图1中,未知=黄色
在图2中,未知=紫色, 等等...
如何设置它,以使未知状态始终为黄色,否定为紫色...
下面是我正在使用的代码
library(viridis)
p <- ggplot() +
geom_polygon(data = spdfMelbSent_fortified2.1, aes(fill = sentiment, x = long, y = lat, group = group) , size= 0, alpha=1) +
theme_void() +
scale_fill_viridis(discrete = TRUE, breaks=c('unknown','Negative','Neutral','Positive'), name="sentiment", guide = guide_legend( keyheight = unit(3, units = "mm"), keywidth=unit(12, units = "mm"), label.position = "bottom", title.position = 'top', nrow=1) ) +
labs(
title = "Melbourne and Geelong",
subtitle = "Sentiment 27th April"
) +
theme(
text = element_text(color = "#22211d"),
plot.background = element_rect(fill = "#f5f5f2", color = NA),
panel.background = element_rect(fill = "#f5f5f2", color = NA),
legend.background = element_rect(fill = "#f5f5f2", color = NA),
plot.title = element_text(size= 22, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
plot.subtitle = element_text(size= 17, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.43, l = 2, unit = "cm")),
plot.caption = element_text( size=12, color = "#4e4d47", margin = margin(b = 0.3, r=-99, unit = "cm") ),
legend.position = c(0.7, 0.09)
) +
coord_map()
p
答案 0 :(得分:1)
我只是复制评论作为答案,以将此问题标记为已回答。
似乎您在填充标尺中缺少limits
,该标尺定义了可能的值及其顺序。
在limits = c("unknown", "Negative", "Neutral", "Positive")
调用中添加scale_fill_viridis()
可以解决在已删除一个或多个级别的多个图之间的不一致。