ggplot - 更改线条宽度

11

我可以通过在ggplot2中执行以下操作来以不同的颜色绘制每个系列...

colours <- c('red', 'blue')
p <- ggplot(data=m, mapping=aes_string(x='Date', y='value'))
p <- p + geom_line(mapping=aes_string(group='variable', colour='variable'), size=0.8)
p <- p + scale_colour_manual(values=colours)

有没有类似的方法来设置不同系列的线宽呢?(例如,我想使用粗红线绘制趋势线和细蓝线绘制季节调整系列。)


2
你能否使用 dput 函数将数据 (m) 上传? - seancarmody
2个回答

13

我会在你的数据框中添加一个新的数字变量。

##You will need to change this to something more appropriate
##Something like: 
##m$size = as.numeric(m$variable == "seasonal")
m$size = rep(c(0, 1), each=10)

然后在您的绘图命令中添加一个 size 美学:

p = p + geom_line(aes(group=variable, colour=variable, size=size))
##Set the size scale
p + scale_size(range=c(0.1, 2), guide=FALSE)

注意,我已经添加了guide=FALSE以避免显示大小图例。


1
我该如何做相反的操作?我想让图例中的线条更粗,但保持图中的线条细。 - baltazar
你最好提出一个新问题。 - csgillespie
super.useful.feature <- "guide = FALSE" - sudo make install

8
你可以这样做:
x <- 1:10
y1 <- x
y2 <- 1.5*x
df <- data.frame(x=rep(x, 2), y=c(y1, y2), id=as.factor(rep(1:2, each=10)))
ggplot(df) + geom_line(aes(x=x,y=y,group=id, colour=id, size=id)) +  
scale_size_manual(values=c(1,4))

同意@Jelena-bioinf的观点,这样可以避免在数据框中创建新变量。 - EcologyTom

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接