如何在ggplot中更改线条宽度?

187

Datalink: 使用的数据

我的代码:

ccfsisims <- read.csv(file = "F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/GTAP-CGE/GTAP_NewAggDatabase/NewFiles/GTAP_ConsIndex.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE)
ccfsirsts <- as.data.frame(ccfsisims)
ccfsirsts[6:24] <- sapply(ccfsirsts[6:24],as.numeric)
ccfsirsts <- droplevels(ccfsirsts)
ccfsirsts <- transform(ccfsirsts,sres=factor(sres,levels=unique(sres)))

library(ggplot2)

#------------------------------------------------------------------------------------------
#### Plot of food security index for Morocco and Turkey by sector
#------------------------------------------------------------------------------------------

#_Code_Begin...

datamortur <- melt(ccfsirsts[ccfsirsts$region %in% c("TUR","MAR"), ]) # Selecting regions of interest
datamortur1 <- datamortur[datamortur$variable %in% c("pFSI2"), ] # Selecting the food security index of interest
datamortur2 <- datamortur1[datamortur1$sector %in% c("wht","gro","VegtFrut","osd","OthCrop","VegtOil","XPrFood"), ] # Selecting food sectors of interest
datamortur3 <- subset(datamortur2, tradlib !="BASEDATA") # Eliminating the "BASEDATA" scenario results  

allfsi.f <- datamortur3
fsi.wht <- allfsi.f[allfsi.f$sector %in% c("wht"), ]

Figure29 <- ggplot(data=fsi.wht, aes(x=factor(sres),y=value,colour=factor(tradlib)))
Figure29 + geom_line(aes(group=factor(tradlib),size=2)) + facet_grid(regionsFull~., scales="free_y", labeller=reg_labeller) + scale_colour_brewer(type = "div") +
theme(axis.text.x = element_text(colour = 'black', angle = 90, size = 13, hjust = 0.5, vjust = 0.5),axis.title.x=element_blank()) + 
ylab("FSI (%Change)") + theme(axis.text.y = element_text(colour = 'black', size = 12), axis.title.y = element_text(size = 12, hjust = 0.5, vjust = 0.2)) + 
theme(strip.text.y = element_text(size = 11, hjust = 0.5, vjust = 0.5, face = 'bold'))

我的结果: Result_Figure

使用aes(size=2)的新结果: NewResult-Figure

我的问题是: 是否有一种更精确地控制线条宽度以避免第二幅图中的结果的方法?我特别发现它不适合文档使用,对于包含新定义线条宽度的图形而言,在出版目的上更是如此。

最好的祝愿, ismail


16
要改变线条宽度,只需在geom_line()中添加参数size=2。 - Didzis Elferts
4
刚刚做了一些实验,看起来大小不必像你使用1和2那样假定为整数值。我刚刚输入了1.5并得到了介于两者之间的结果。我不确定这样的固定值在所有情况下是否适用于你,但至少似乎是可调的。 - jxramos
7个回答

173

虽然 @Didzis 给出了 正确的答案,但我会进一步解释一些要点。

在 ggplot 调用中可以设置或映射美学。

  • 在 aes(...) 中定义的美学从数据中映射,并创建一个图例。

  • 也可以通过在 aes() 之外定义来将美学设置为单个值。

据我所知,你想要做的是将大小设置为单个值,而不是在调用 aes() 中进行映射。

当你调用 aes(size = 2) 时,它会创建一个名为 `2` 的变量,并使用它来创建大小,从常量值中映射它,因为它在 aes 调用中(因此出现在你的图例中)。

使用 size = 1(并且没有 reg_labeller,这可能在你的脚本中定义)

Figure29 +
    geom_line(aes(group=factor(tradlib)),size=1) +
    facet_grid(regionsFull~., scales="free_y") +
    scale_colour_brewer(type = "div") +
    theme(axis.text.x = element_text(
          colour = 'black', angle = 90, size = 13,
          hjust = 0.5, vjust = 0.5),axis.title.x=element_blank()) +
    ylab("FSI (%Change)") +
    theme(axis.text.y = element_text(colour = 'black', size = 12), 
          axis.title.y = element_text(size = 12, 
          hjust = 0.5, vjust = 0.2)) + 
    theme(strip.text.y = element_text(size = 11, hjust = 0.5,
          vjust =    0.5, face = 'bold'))

enter image description here

并且大小为2

 Figure29 + 
     geom_line(aes(group=factor(tradlib)),size=2) +
     facet_grid(regionsFull~., scales="free_y") + 
     scale_colour_brewer(type = "div") +
     theme(axis.text.x = element_text(colour = 'black', angle = 90,
          size = 13, hjust = 0.5, vjust = 
          0.5),axis.title.x=element_blank()) + 
     ylab("FSI (%Change)") +
     theme(axis.text.y = element_text(colour = 'black', size = 12),
          axis.title.y = element_text(size = 12,
          hjust = 0.5, vjust = 0.2)) + 
      theme(strip.text.y = element_text(size = 11, hjust = 0.5,
          vjust = 0.5, face = 'bold'))

enter image description here

现在您可以定义大小,以适应最终图像大小和设备类型。

很棒的答案。更新使用ggplot3.4.0,新的参数是linewidth而不是size - Josh

97

ggplot2中,可以使用geom_line()函数的size=参数来更改线条宽度。

#sample data
df<-data.frame(x=rnorm(100),y=rnorm(100))
ggplot(df,aes(x=x,y=y))+geom_line(size=2)

在这里输入图片描述


6
我已经尝试了你给出的选项,但问题是线条变得太粗,不适合文档展示。当使用 aes(size) 时,我得到了添加的第二张图。我希望能够更系统地控制线条宽度,以便选择最佳匹配。 - iouraich
9
@smailov83,不要在调用aes时加入大小(size)参数。请参考我的回答(或者ggplot2书籍)获取解释。 - mnel
7
默认值似乎小于 size=1,可能是 0.5,所以在我看来,使用 size=1 可以得到相当不错的结果。一个人也可以使用小数来微调宽度(比如说 size=1.2)。 - Ricardo
5
请注意,从ggplot2 3.4.0开始,size已经被弃用,对于线条现在应该使用更明确的linewidth - Maël

15

ggplot2中的线条宽度可以通过geom_line()中的参数lwd=进行更改。

geom_line(aes(x=..., y=..., color=...), lwd=1.5)

今天在geom_hline中出现了问题,"size="未被识别。 - Mike M
我相信在当前版本的ggplot中,该参数被称为linewidth。另外,lineend="square"会延长线条,而"butt"只会增加宽度。 - qwr
我相信在当前版本的ggplot中,这个参数被称为linewidth。另外,lineend="square"会延长线条,而"butt"只会增加线条的宽度。 - undefined

10

看起来如果你只在geom_line()部分中放置size参数而不使用aes(),它将适当地进行缩放。至少对于geom_density而言是这样的,我也遇到了同样的问题。


5

如果您想灵活修改线条宽度,可以使用“scale_size_manual”命令,这与选择颜色、填充、透明度等程序相同。

library(ggplot2)
library(tidyr)

x = seq(0,10,0.05)

df <- data.frame(A = 2 * x + 10,
                 B = x**2 - x*6,
                 C = 30 - x**1.5,
                 X = x)


df = gather(df,A,B,C,key="Model",value="Y")


ggplot( df, aes (x=X, y=Y, size=Model, colour=Model ))+
  geom_line()+
  scale_size_manual( values = c(4,2,1) ) +
  scale_color_manual( values = c("orange","red","navy") ) 


2

自 ggplot2 3.4.0 版本开始,请使用 linewidth

例如:

geom_line(aes(group=factor(tradlib)), linewidth=0.2)

2

只需在 aes() 函数之外添加 size 命令,并设置所需的任何小数值,例如 size = 1.5。

geom_line(data,aes(x=x,y=y), size=1.5)

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