How to change line width in ggplot?
Asked Answered
I

7

191

Datalink: the data used

My code:

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

My result: Result_Figure

Newresult with aes(size=2): NewResult-Figure

My question: Is there a way to control for line width more precisely to avoid the result in the second plot? I particularly find it document-unfriendly, and more so for publishing purposes to include the plot with the newly defined line width.

best, ismail

Intemperate answered 10/2, 2013 at 4:7 Comment(2)
To change line width, just add argument size=2 to geom_line().Overtask
Just did some experimentation and it looks like size doesn't have to assume integral values as you're using with 1 and 2. I just entered 1.5 and got something in-between. I'm not sure if a fixed value like that would work for you under all circumstances but it does appear tunable at least.Osyth
M
175

Whilst @Didzis has the correct answer, I will expand on a few points

Aesthetics can be set or mapped within a ggplot call.

  • An aesthetic defined within aes(...) is mapped from the data, and a legend created.

  • An aesthetic may also be set to a single value, by defining it outside aes().

As far as I can tell, what you want is to set size to a single value, not map within the call to aes()

When you call aes(size = 2) it creates a variable called `2` and uses that to create the size, mapping it from a constant value as it is within a call to aes (thus it appears in your legend).

Using size = 1 (and without reg_labeller which is perhaps defined somewhere in your script)

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

and with size = 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

You can now define the size to work appropriately with the final image size and device type.

Martyrology answered 11/2, 2013 at 0:39 Comment(1)
Great answer. Update with ggplot3.4.0, the new argument is linewidth instead of size.Micco
O
97

Line width in ggplot2 can be changed with argument size= in geom_line().

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

enter image description here

Overtask answered 10/2, 2013 at 10:39 Comment(4)
I have already tried your option, but the problem is that the line become too thick to be considered document-friendly. When using the aes(size), I get the second graph that I added. I want to be able to control for the width in a more systematic way so as to choose the best fit.Intemperate
@smailov83, don't put size within the call to aes. See my answer (or the ggplot2 book for an explanation.Martyrology
Default value seems to be less than size=1, maybe 0.5, so using size=1 yields quite good results in my opinion. One can also use decimal numbers to fine tune the width (say size=1.2).Statecraft
Note that from ggplot2 3.4.0, size is superseded for lines and is now replaced by the more explicit linewidth.Immitigable
P
15

Line width in ggplot2 can be changed with argument lwd= in geom_line().

geom_line(aes(x=..., y=..., color=...), lwd=1.5)
Partin answered 26/2, 2020 at 13:3 Comment(2)
worked for me today in geom_hline where "size=" is not recognizedDelk
I believe the argument is called linewidth in current versions of ggplot. Also lineend="square" extends the line while "butt" only increases width.Doss
T
10

It also looks like if you just put the size argument in the geom_line() portion but without the aes() it will scale appropriately. At least it works this way with geom_density and I had the same problem.

Tuck answered 4/6, 2019 at 2:44 Comment(0)
S
5

If you want to modify the line width flexibly you can use "scale_size_manual," this is the same procedure for picking the color, fill, alpha, etc.

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

Spancake answered 8/3, 2019 at 18:8 Comment(0)
D
4

Since ggplot2 3.4.0. use linewidth.

For example:

geom_line(aes(group=factor(tradlib)), linewidth=0.2)
Dowzall answered 18/3, 2023 at 9:54 Comment(0)
C
2

Just add the size command outside the aes() function, with any fractional value desired, e.g. size = 1.5

geom_line(data,aes(x=x,y=y), size=1.5)
Consubstantiate answered 21/1, 2023 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.