[英]geom_line(): styling legend
我正在嘗試使用以下代碼制作 plot:
p <- ggplot()+
geom_line(fit.pre.net, mapping = aes(x = x, y = y, group = group, col = "Predicted (before SOEs)", linetype = "Predicted (before SOEs)"), size = 1) +
geom_line(fit.mid.net, mapping = aes(x = x, y = y, group = group, col = "Predicted (during SOEs)", linetype = "Predicted (during SOEs)"), size = 1) +
geom_line(fit.post.net, mapping = aes(x = x, y = y, group = group, col = "Predicted (after SOEs)", linetype = "Predicted (after SOEs)"), size = 1) +
geom_line(fit.pre.count, mapping = aes(x = x, y = y, group = group, col = "Counterfactual (before SOEs)", linetype = "Counterfactual (before SOEs)"), size = 1) +
geom_line(fit.mid.count, mapping = aes(x = x, y = y, group = group, col = "Counterfactual (during SOEs)", linetype = "Counterfactual (during SOEs)"), size = 1) +
scale_linetype_manual(values = c("Predicted (before SOEs)" = "solid", "Predicted (during SOEs)" = "solid", "Predicted (after SOEs)" = "solid",
"Counterfactual (before SOEs)" = "dashed", "Counterfactual (during SOEs)" = "dashed")) +
scale_color_manual(values = c("Predicted (before SOEs)" = "steelblue4", "Counterfactual (before SOEs)" = "steelblue4",
"Predicted (during SOEs)" = "red", "Counterfactual (during SOEs)" = "red", "Predicted (after SOEs)" = "seagreen4"))
我想按如下方式分配顏色和 l.netype:
“預測(國有企業之前)”:steelblue4,固體
“預測(在國有企業期間)”:紅色,實心
“預測(在國有企業之后)”:seagreen4,固體
“反事實(國有企業之前)”:steelblue4,虛線
“反事實(在國有企業期間)”:紅色,虛線
誰能告訴我如何解決這個問題?
正如評論中所建議的,我嘗試了這個:
p <- ggplot
geom_line(fit, mapping = aes(x = x, y = y, group = group, col = group, linetype = group), size = 1) +
scale_linetype_manual(values = c("Predicted (before SOEs)" = "solid", "Predicted (during SOEs)" = "solid", "Predicted (after SOEs)" = "solid",
"Counterfactual (before SOEs)" = "dashed", "Counterfactual (during SOEs)" = "dashed")) +
scale_color_manual(values = c("Predicted (before SOEs)" = "steelblue4", "Counterfactual (before SOEs)" = "steelblue4",
"Predicted (during SOEs)" = "red", "Counterfactual (during SOEs)" = "red", "Predicted (after SOEs)" = "seagreen4"))
數據現在看起來像這樣:
# A tibble: 10 × 3
x y group
<chr> <dbl> <chr>
1 2018-01 24.7 Predicted (before SOEs)
2 2020-03 25.1 Predicted (before SOEs)
3 2020-04 25.3 Predicted (during SOEs)
4 2021-09 25.0 Predicted (during SOEs)
5 2021-10 25.2 Predicted (after SOEs)
6 2022-07 25.2 Predicted (after SOEs)
7 2020-04 25.1 Counterfactual (before SOEs)
8 2021-09 25.4 Counterfactual (before SOEs)
9 2021-10 25.0 Counterfactual (during SOEs)
10 2022-07 24.9 Counterfactual (during SOEs)
然后我發現我可以通過在theme()
中添加以下代碼來更改圖例的位置和大小:
legend.text=element_text(size=10),
legend.justification=c(0.05,0.05),legend.position=c(0.05,0.05),legend.title=element_blank()
現在,1 和 3 已解決,但我的虛線圖例看起來仍然很奇怪......這里可能是什么問題?
您的 l.netypes 在圖例鍵中不正確可見的原因是圖例鍵中的線條尺寸太大,無法看到l.netype
的美感。 這個問題的解決方案是明確定義要在圖例中使用的size
更小。 默認情況下,圖例鍵美學與geom_*()
命令繪制的內容相匹配,但您可以重新定義美學以在圖例中使用或更改鍵的大小來解決問題。
下面是一個很好地演示此問題和上述修復的示例。 首先,這是基本的 plot:
圖書館(ggplot2)
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color=Species, linetype=Species))
p + geom_line()
在上面的plot中,可以很容易的區分出圖例中的l.netypes
。 請注意,圖例鍵的大小和美觀與主面板中顯示的相匹配。 讓我們看看當我們為geom_line()
中的線條定義更大的尺寸時會發生什么:
p + geom_line(size=1.8)
看起來熟悉? 相對於圖例鍵尺寸較大的線條尺寸會導致圖例鍵看起來不正確。 您可以通過增大圖例鍵或重新定義用於繪制圖例的美學來解決此問題。
要更改圖例的大小,您可以使用theme()
元素legend.key.size
或legend.key.width
或legend.key.height
。 我喜歡這里的legend.key.width
,因為我們正在看水平線:
p + geom_line(size=1.8) +
theme(legend.key.width = unit(40, 'pt'))
一個可能更好的解決方案(在我看來)是明確定義圖例中使用的美學。 這可以用來告訴ggplot
在圖例中“畫得更小”:
p + geom_line(size=1.8) +
guides(
linetype=guide_legend(override.aes = list(size=1))
)
要使用的修復程序將取決於您的特定 plot 以及它與圖例相比的外觀以及個人喜好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.