ggplot2 geom_smooth line not showing up on my graph

前端 未结 1 1563
花落未央
花落未央 2021-01-25 16:20

I am trying to add a line through my plotted data using geom_smooth, but I am running into difficulty.

Here is my code:

plot.BG = ggplot(da         


        
1条回答
  •  说谎
    说谎 (楼主)
    2021-01-25 17:12

    Aesthetic mappings defined in ggplot() are inherited by subsequent layers. However, if mappings defined in a layer (e.g., inside geom_point()) are local to that layer only. Since you want the same mappings to be used by both the geom_point and the geom_smooth layers, put them in the initial ggplot() call and they will be inherited by both.

    Reproducibly using mtcars:

    # only the points are displayed
    ggplot(mtcars) +
        geom_point(aes(x = hp, y = mpg, color = factor(cyl)) + 
        geom_smooth()
    
    # you could respecify for the geom smooth, but that's repetitive
    ggplot(mtcars) +
        geom_point(aes(x = hp, y = mpg, color = factor(cyl)) + 
        geom_smooth(aes(x = hp, y = mpg, color = factor(cyl))
    
    # put the mapping up front for all layers to inherit it
    ggplot(mtcars, aes(x = hp, y = mpg, color = factor(cyl)) +
        geom_point() + 
        geom_smooth()
    

    0 讨论(0)
提交回复
热议问题