How to change size from specific geom in ggplot2?

后端 未结 2 2074
执笔经年
执笔经年 2021-01-21 22:46

I have a ggplot containing 2 layers geom_point and geom_line as shown below.

gp <- ggplot(data = mtcars , aes(x = disp , y = hp)) +         


        
2条回答
  •  执笔经年
    2021-01-21 23:20

    If you wish to replace the existing size with a smaller one (or replace a solid linetype with a dashed one, a filled shape with an unfilled one, etc.), overlaying may not have the best visual effect. As an alternative, you can dig into the specific layer of the ggplot object you've created, & manually change the parameters there.

    (Note that this requires you to know the order of geom layers in the object.)

    gp$layers[[2]]$aes_params$size <- 0.5 # change the size of the geom_line layer from 1 to 0.5
    gp$layers[[1]]$aes_params$size <- 1   # change the size of the geom_point layer from 3 to 1
    

    I assume your use case involves modifying a ggplot object outputted by some package's plotting function? Otherwise, it's probably simpler to specify the desired parameters at the point of plot creation...

提交回复
热议问题