简体   繁体   中英

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

在此处输入图像描述

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

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

线条较粗的折线图

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM