Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the line width (size) defined in ggplot2?

Tags:

r

ggplot2

The line width (size) aesthetics in ggplot2 seems to print approximately 2.13 pt wider lines to a pdf (the experiment was done in Adobe Illustrator with a Mac):

library(ggplot2)

dt <- data.frame(id = rep(letters[1:5], each = 3), x = rep(seq(1:3), 5), y = rep(seq(1:5), each = 3), s = rep(c(0.05, 0.1, 0.5, 1, 72.27/96*0.5), each = 3))

lns <- split(dt, dt$id)

ggplot() + geom_line(data = lns[[1]], aes(x = x, y = y), size = unique(lns[[1]]$s)) + 
  geom_text(data = lns[[1]], y = unique(lns[[1]]$y), x = 3.5, label = paste("Width in ggplot =", unique(lns[[1]]$s))) + 
  geom_line(data = lns[[2]], aes(x = x, y = y), size = unique(lns[[2]]$s)) + 
  geom_text(data = lns[[2]], y = unique(lns[[2]]$y), x = 3.5, label = paste("Width in ggplot =", unique(lns[[2]]$s))) + 
  geom_line(data = lns[[3]], aes(x = x, y = y), size = unique(lns[[3]]$s)) + 
  geom_text(data = lns[[3]], y = unique(lns[[3]]$y), x = 3.5, label = paste("Width in ggplot =", unique(lns[[3]]$s))) + 
  geom_line(data = lns[[4]], aes(x = x, y = y), size = unique(lns[[4]]$s)) + 
  geom_text(data = lns[[4]], y = unique(lns[[4]]$y), x = 3.5, label = paste("Width in ggplot =", unique(lns[[4]]$s))) + 
  geom_line(data = lns[[5]], aes(x = x, y = y), size = unique(lns[[5]]$s)) + 
  geom_text(data = lns[[5]], y = unique(lns[[5]]$y), x = 3.5, label = paste("Width in ggplot =", unique(lns[[5]]$s))) + 
  xlim(1,4) + theme_void()

ggsave("linetest.pdf", width = 8, height = 2)

# Device size does not affect line width:
ggsave("linetest2.pdf", width = 10, height = 6)

enter image description here

I read that one should multiply the line width by 72.27/96 to get a line width in pt, but the experiment above gives me a line width of 0.8 pt, when I try to get 0.5 pt.

As @Pascal points out, the line width does not seem to follow the pt to mm conversion that works for fonts and was defined by @hadley in one of the comments. I.e. the line width does not appear to be defined by "the magic number" 1/0.352777778.

What is the equation behind line width for ggplot2?

like image 954
Mikko Avatar asked Nov 27 '17 20:11

Mikko


People also ask

How do I change the width of a line in ggplot2?

To change line width, just add argument size=2 to geom_line().

How do I set line width in R?

To set plot line width/thickness in R, call plot() function and along with the data to be plot, pass required thickness/line-width value for the “lwd” parameter.

How do I increase the size of a line in R?

Adjust the R line thickness by specifying the options lwd (base plot) and size (ggplot2). Change manually the appearance (linetype, color and size) of ggplot lines by using, respectively, the function scale_linetype_manual(), scale_color_manual() and scale_size_manual().

How do I make lines bolder in ggplot2?

The larger the value given to the size argument, the thicker the line will be in the plot.


1 Answers

You had all the pieces in your post already. First, ggplot2 multiplies the size setting by ggplot2::.pt, which is defined as 72.27/25.4 = 2.845276 (line 165 in geom-.r):

> ggplot2::.pt
[1] 2.845276

Then, as you state, you need to multiply the resulting value by 72.27/96 to convert from R pixels to points. Thus the conversion factor is:

> ggplot2::.pt*72.27/96
[1] 2.141959

As you can see, ggplot2 size = 1 corresponds to approximately 2.14pt, and similarly 0.8 pt corresponds to 0.8/2.141959 = 0.3734899 in ggplot2 size units.

like image 125
Claus Wilke Avatar answered Oct 16 '22 20:10

Claus Wilke