![](/img/trans.png)
[英]How to specify different background colors for each facet label in ggplot2?
[英]How to vary the colors within each facet panel as well as across different facet panels in ggplot2
我有一個類似下面的圖,該圖是使用ggplot2
創建的,它具有三個構面面板。
但是,我想在左側面板中顯示兩個紅色陰影,在中間面板中顯示兩個綠色陰影,在右側面板中包含兩個藍色陰影。 因此,換句話說,每個面板中的兩個色帶的顏色不同,並且面板之間的顏色也不同。 但是我還沒有想辦法。 我用來獲取代碼的代碼如下。 factor1
是一個2級可變的,並且factor2
是一個3級的變量。 在此先感謝您的幫助!
ggplot(dataset, aes(x = window, group=factor1)) +
facet_grid(.~factor2) +
geom_line(aes(y=fit), alpha=0.8, colour="gray20") +
geom_ribbon(aes(ymin=fit - 1.96*se, ymax=fit+1.96*se, fill=factor1), alpha=0.7) +
geom_vline(xintercept=0, colour="gray20") +
geom_vline(xintercept=4, colour="red", size=.1) +
geom_hline(xintercept=0, colour="black",size=.1, linetype="dashed") +
theme(panel.background=element_rect(fill="white", colour="black")) +
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank())
您可以指定變量交互以改變顏色。 您需要手動定義調色板以獲得所需的陰影:
ggplot(dataset, aes(x = window, group=factor1)) +
facet_grid(.~factor2) +
geom_line(aes(y=fit), alpha=0.8, colour="gray20") +
geom_ribbon(aes(ymin=fit - 1.96*se, ymax=fit+1.96*se, fill=factor1:factor2), alpha=0.7) +
geom_vline(xintercept=0, colour="gray20") +
geom_vline(xintercept=4, colour="red", size=.1) +
geom_hline(xintercept=0, colour="black",size=.1, linetype="dashed") +
theme(panel.background=element_rect(fill="white", colour="black")) +
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.