R 手动缩放颜色和线型时调整图例宽度

R 手动缩放颜色和线型时调整图例宽度,r,ggplot2,R,Ggplot2,使用这些数据: 该代码: 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 = p

使用这些数据:

该代码:

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()
中设置
linetype=1
时,图例中的两行将显示为实线。因此,您需要将参数
show\u guide=FALSE
添加到
geom\u errorbar()


我能再问一个相关的问题吗?我想调整x轴和y轴上的打断(即更改轴标记)。在scale_y_continuous中指定breaks=c(etc)的常用方法似乎不起作用-这是因为我正在手动缩放颜色等吗?有没有办法更改轴刻度?非常感谢。对于这个+scale_y_continuous(breaks=c(0.1,0.15,0.2,0.25,0.35))工作没有问题。谢谢-我的代码肯定有其他问题!非常感谢:)
  + geom_errorbar(aes(ymin=DV-Error, ymax=DV+Error),
                width = 0.1,linetype=1,
                   show_guide=FALSE)