[英]Single legend when using group, linetype and colour in ggplot2?
[英]Adjust legend width when colour and linetype are scaled manually
使用此數據:
這段代碼:
ggplot(data = trial, aes(x = as.factor(Year), y = DV,group = TMT, col = TMT,linetype=TMT)) +
theme_bw() +
geom_point(size = 3,position = pd) +
geom_errorbar(aes(ymin=trial$DV-trial$Error, ymax=trial$DV+trial$Error),
position = pd,
width = 0.1,
linetype=1) +
geom_line(position = pd) +
ylab("DV") +
xlab("Year") +
theme(axis.title.y=element_text(size=rel(1.1),vjust=0.2),
axis.title.x=element_text(size=rel(1.1), vjust=0.2),
axis.text.x=element_text(size=rel(1)),
axis.text.y=element_text(size=rel(1)),
text = element_text(size=13)) +
scale_colour_brewer(palette="Set1") +
scale_colour_manual(name = "Tmt",
labels = c("C", "S"),
values = c("red","blue"))+scale_linetype_manual(name = "Tmt",
labels = c("C", "S"),
values = c("solid","dashed"))
我正在創建以下圖表:
我想在我的傳奇中清楚地表達線型。 我不確定我的手動縮放線型是否有效,或者是否因為圖例的寬度太窄。 我試過添加:
+guides(linetype=guide_legend(keywidth=5))
調整寬度,但它不起作用。 任何人都有任何想法如何解決這個問題?
非常感謝!
要更改圖例鍵的寬度,您應該使用函數theme()
和參數legend.key.width=
。 您還需要包網格來設置單位。
library(grid)
+ theme(legend.key.width=unit(1,"cm"))
但在您的特定情況下,您需要對您的情節進行另一次更改。 當您在geom_errorbar()
中設置geom_errorbar()
linetype=1
, geom_errorbar()
兩條線都將顯示為實線。 因此,您需要將參數show_guide=FALSE
添加到geom_errorbar()
。
+ geom_errorbar(aes(ymin=DV-Error, ymax=DV+Error),
width = 0.1,linetype=1,
show_guide=FALSE)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.