'Adjust line size of a ggplot object
library(ggplot2)
p <- ggplot(iris,aes(Sepal.Width,Petal.Length))+
geom_line()
p
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))
Solution 1:[1]
You can change or add parameters to a specific layer in a ggplot object like this:
p$layers[[1]]$aes_params$size = 2
p
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tivd |
| Solution 2 | Allan Cameron |




