Search code examples
rggplot2smoothing

Is there a ggplot2 function to remove original lines after using geom_smooth?


I have time series data of home sales which I've broken out by home type (horizontal axis = time, vertical axis = sale price, each line = a different home type). It's pretty messy so I've opted to use geom_smooth on the lines for better visualization. While this does produce smoothed lines, the messy ones are still displayed. I want the graph to not display the original data, but only the smooth lines. I have looked around and cannot seem to find a way to null out those old lines that works. The code displays where I'm currently at.

 ## sample code taken from: https://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph

library("reshape2")
library("ggplot2")

test_data <- data.frame(
var0 = 100 + c(0, cumsum(runif(49, -20, 20))),
var1 = 150 + c(0, cumsum(runif(49, -10, 10))),
date = seq(as.Date("2002-01-01"), by="1 month", length.out=100)
  )

test_data_long <- melt(test_data, id="date")  # convert to long format

ggplot(data=test_data_long,
   aes(x=date, y=value, colour=variable)) +
  geom_line() +
  geom_smooth(se = FALSE)

Solution

  • ggplot(data=test_data_long,
           aes(x=date, y=value, colour=variable)) +
            # geom_line() +
            geom_smooth(formula = y~x, se = FALSE)
    

    A warning will show, but the plot should be right.