在ggplot2中创建一个多线图时,如何使一条线比其他线更粗? [英] When creating a multiple line plot in ggplot2, how do you make one line thicker than the others?

查看:1078
本文介绍了在ggplot2中创建一个多线图时,如何使一条线比其他线更粗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的融合数据框,其中有5个变量,我正在ggplot2的多线图中绘制。我在下面发布了我的代码,我觉得这个问题的答案应该很简单,但我找不到答案。



如果我在同一个图表上绘制5条线,是否有办法让其中一条线(这种情况下的平均值)比其他线更大胆/更大?

正如你可以在底部的代码中看到的那样,我将行的大小指定为2,这使得所有5行的大小都为2。使平均线(比例尺颜色函数中指定为黑色的线)变得比其他线更大。

我尝试将尺寸设置为 size = c(2,2,2,2,3),但ggplot2的确不是这样的。

  FiveLineGraph < -  ggplot(data = df,aes(x = Date,y = Temperature,group = model,color =模型))+ 
geom_line(size = 2)+
scale_colour_manual(values = c(red,blue,green,gold,black))

有什么想法?



感谢您的帮助。



谢谢。

解决方案

我添加了数据以使其成为可重复使用的示例。这种技术也适用于你的线条的颜色。

  library(ggplot2)
set.seed(99)
df< - data。 (x = c(1:5,1:5,1:5),y = rnorm(15,10,2),
group = c(rep(A,5),rep( B,5),rep(C,5)),
stringsAsFactors = FALSE)
ggplot(df,aes(x = x,y = y,group = group,color = group) )+ geom_line(size = 2)

  df $ mysize<  -  rep(2,nrow(df))
df $ mysize [df $ group = =B] < - 4
ggplot(df,aes(x = x,y = y,color = group,size = mysize))+ geom_line()+
scale_size(range = c (2,4),guide =none)


I have a simple melted data frame with 5 variables that I am plotting in a multiple line graph in ggplot2. I posted my code below and I feel the answer to this question should be simple, yet I can't find the answer.

If I am plotting 5 lines together on the same chart, is there a way to make one of the lines (the mean in this case) bolder/larger than the others?

As you can see in the code at the bottom, I specified that the size of the lines as 2 which makes all 5 lines the size of 2. But I was hoping to have the Mean line (the line specified as black in the scale colour function) become larger than the other lines.

I attempted setting size to size = c(2,2,2,2,3) but ggplot2 did not like that.

   FiveLineGraph <- ggplot(data= df, aes(x= Date, y=Temperature, group= model, colour= model)) +
  geom_line(size= 2) +
  scale_colour_manual(values = c("red","blue", "green", "gold","black"))

Any ideas?

I appreciate your help in advance.

Thanks.

解决方案

I've added data to make it a "reproducible example". This technique would work for the color of your lines also.

library("ggplot2")
set.seed(99)
df <- data.frame(x=c(1:5, 1:5, 1:5), y=rnorm(15, 10, 2), 
                 group=c(rep("A", 5), rep("B", 5), rep("C", 5)),
                 stringsAsFactors=FALSE)
ggplot(df, aes(x=x, y=y, group=group, colour=group)) + geom_line(size=2)

df$mysize <- rep(2, nrow(df))
df$mysize[df$group=="B"] <- 4
ggplot(df, aes(x=x, y=y, colour=group, size=mysize)) + geom_line() + 
  scale_size(range = c(2, 4), guide="none")

这篇关于在ggplot2中创建一个多线图时,如何使一条线比其他线更粗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆