Home > Blockchain >  Adjust line size of a ggplot object
Adjust line size of a ggplot object

Time:03-13

library(ggplot2)
p <- ggplot(iris,aes(Sepal.Width,Petal.Length)) 
     geom_line()
p

enter image description here

Imagine that you have a ggplot object and you have no access to the code or data that created this plot. How does one adjust the size/thickness of the plotted line?

I have tried modifying the theme, but that only changes the background lines and not the plotted lines.

p 
theme(line=element_line(size=10))

enter image description here

CodePudding user response:

You can change or add parameters to a specific layer in a ggplot object like this:

p$layers[[1]]$aes_params$size = 2
p

Line graph with thicker lines

CodePudding user response:

For more complex ggplots, where you don't know which layers are the geom_line layers, you will have to pick out which layers are geom_line layers to rewrite their aesthetic parameters:

geomlines <- which(sapply(p$layers, function(x) class(x$geom)[1] == "GeomLine"))

p$layers[geomlines] <- lapply(p$layers[geomlines], function(x) {
  x$aes_params$size <- 2
  x
})

p

enter image description here

  • Related