【问题标题】:ggplot - change line widthggplot - 改变线宽
【发布时间】:2012-08-30 20:57:46
【问题描述】:

我可以通过做这样的事情在 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)

我可以做些类似的事情来为每个系列设置不同的线宽吗? (即,我想用粗红线绘制趋势,细蓝线绘制季节性调整序列。)

【问题讨论】:

  • 你能dput你的数据(m)吗?

标签: r ggplot2


【解决方案1】:

我只想在您的数据框中添加一个新的数值变量

##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)

然后为您的绘图命令添加尺寸美学:

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 以避免显示尺寸图例。

【讨论】:

  • 我该怎么做?我想让图例中的线条更粗,但让情节上的线条变细。
  • 你最好问一个新问题
  • super.useful.feature &lt;- "guide = FALSE"
【解决方案2】:

你可以这样做:

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,它省去了在数据框中创建新变量的麻烦。
猜你喜欢
  • 2013-01-25
  • 1970-01-01
  • 2019-03-23
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多