下面的代碼改編自
選項 2
您可以使用scale_color_manual. 只需確保您的Vaccine列確實是具有水平"0"和"1"
dat %>% mutate(state = reorder(state, desc(state))) %>%
ggplot(aes(year, state, fill = rate))
geom_tile(aes(color = Vaccine), size = 0.35)
scale_color_manual(values = c('0' = "white", '1' = "pink"))
scale_x_continuous(expand = c(0,0))
scale_fill_gradientn(colors = jet.colors(16), na.value = 'white')
geom_vline(xintercept = 1963, col = "black")
theme_minimal()
theme(panel.grid = element_blank())
coord_cartesian(clip = 'off')
ggtitle(the_disease)
ylab("")
xlab("")
theme(legend.position = "bottom", text = element_text(size = 8))
annotate(geom = "text", x = 1963, y = 50.5, label = "Vaccine introduced",
size = 3, hjust = 0)
guides(color = guide_legend(override.aes = list(fill = "white")))

對于它的價值,我更喜歡選項1
更新
如果您想要多個疫苗起點,您也可以這樣做:
dat %>% mutate(state = reorder(state, desc(state))) %>%
ggplot(aes(year, state, fill = rate))
geom_tile(color = "white", size = 0.35)
annotate("rect", xmin = 1963, xmax = Inf, ymin = 25, ymax = 50,
alpha = 0.2, fill = "palevioletred1")
annotate("rect", xmin = 1975, xmax = Inf, ymax = 25, ymin= 0,
alpha = 0.2, fill = "palevioletred1")
scale_x_continuous(expand = c(0,0))
scale_fill_gradientn(colors = jet.colors(16), na.value = 'white')
theme_minimal()
theme(panel.grid = element_blank())
coord_cartesian(clip = 'off')
ggtitle(the_disease)
ylab("")
xlab("")
theme(legend.position = "bottom", text = element_text(size = 8))
annotate(geom = "text", x = 1963, y = 50.5,
label = "Vaccine introduced", size = 3, hjust = 0)

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/446284.html
上一篇:R代碼:沒有適用于“字符”類物件的“tbl_vars”方法
下一篇:在列中的新值之后插入NA行
