How to create base R plot 'type = b' equivalent in ggplot2?

前端 未结 4 2078
日久生厌
日久生厌 2020-12-15 19:40

Base plot() functionality allows one to set type=\'b\' and get a combined line and point plot in which the points are offset from the line segments

4条回答
  •  情话喂你
    2020-12-15 20:28

    One option which is less hacky than manually matching the stroke color with the panel background is to get the panel background beforehand, either from theme_get for the default theme, or with a specific theme that you'll be using. Using a stroked shape like 21 lets you make the inner circle black and the stroke the same color as the background.

    library(ggplot2)
    
    bgnd <- theme_get()$panel.background$fill
    
    ggplot(pressure, aes(x = temperature, y = pressure)) + 
      geom_line() + 
      geom_point(shape = 21, fill = "black", size = 2, stroke = 1, color = bgnd)
    

    A couple SO questions (here's one) deal with the math behind shortening segments between points. It's simple but tedious geometry. But in the time since this question was first posted, the lemon package has come out, which has a geom to do this. It's got arguments for how to calculate the shortening, which probably require just some simple tweaking.

    library(lemon)
    
    ggplot(pressure, aes(x = temperature, y = pressure)) +
      geom_pointline()
    

提交回复
热议问题