library(tidyverse)
ggplot(data = data.frame(x = c(0, 1)), aes(x))
geom_function(fun = dnorm, n = 10001,
args = list(mean = .5, sd = .125),
show.legend = T)
geom_function(fun = dbeta, n = 10001,
args = list(shape1 = 10, shape2 = 8),
linetype = 5, show.legend = T)
geom_function(fun = dbeta, n = 10001,
args = list(shape1 = 15, shape2 = 8),
linetype = 2, show.legend = T)
geom_function(fun = dbeta, n = 10001,
args = list(shape1 = 20, shape2 = 8),
linetype = 3, show.legend = T)
ylab("f(θ)")
xlab("θ")
scale_linetype_manual(
values = c("a" = 1,
"b" = 5,
"c" = 2,
"d" = 3)
)
theme_test(base_size = 20)
如果繪制此圖,圖例將不會采用正確的線型。它將始終顯示線型“1”或“實線”。
如何在 scale_linetype_manual() 中顯示不同的正確線型?
uj5u.com熱心網友回復:
如果你想有一個圖例,你必須在美學上進行映射,即不是將線型設定為引數,而是將其設定在內部aes()并使用您在中使用的標簽scale_linetype_manual:
注意:這樣做我們可以擺脫,show.legend=T因為 ggplot 會自動添加一個圖例。
library(ggplot2)
ggplot(data = data.frame(x = c(0, 1)), aes(x))
geom_function(aes(linetype = "a"), fun = dnorm, n = 10001,
args = list(mean = .5, sd = .125))
geom_function(aes(linetype = "b"), fun = dbeta, n = 10001,
args = list(shape1 = 10, shape2 = 8))
geom_function(aes(linetype = "c"), fun = dbeta, n = 10001,
args = list(shape1 = 15, shape2 = 8))
geom_function(aes(linetype = "d"), fun = dbeta, n = 10001,
args = list(shape1 = 20, shape2 = 8))
ylab("f(θ)")
xlab("θ")
scale_linetype_manual(
values = c("a" = 1,
"b" = 5,
"c" = 2,
"d" = 3)
)
theme_test(base_size = 20)

編輯相反的線型美觀另一種選擇映射的是通過設定所需的線型override.aes的說法guide_legend。這也可以用于設定不同的顏色或...。但請注意,這樣做您必須按照類別在圖例中出現的順序設定線型:
注意:通過 分配線型scale不起作用。為此,我們必須映射美學。
library(ggplot2)
ggplot(data = data.frame(x = c(0, 1)), aes(x))
geom_function(fun = dnorm, n = 10001,
args = list(mean = .5, sd = .125),
show.legend = T, linetype = 3)
geom_function(fun = dbeta, n = 10001,
args = list(shape1 = 10, shape2 = 8),
linetype = 5)
geom_function(fun = dbeta, n = 10001,
args = list(shape1 = 15, shape2 = 8),
linetype = 2)
geom_function(fun = dbeta, n = 10001,
args = list(shape1 = 20, shape2 = 8),
linetype = 3)
ylab("f(θ)")
xlab("θ")
scale_linetype_manual(
values = c("a" = 1,
"b" = 2,
"c" = 2,
"d" = 3)
)
theme_test(base_size = 20)
guides(linetype = guide_legend(override.aes = list(linetype = c(1, 5, 2, 3), color = c(1, 5, 2, 3))))

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/377677.html
