-1

I've been trying to add a trend line for my ggplot (regarding to sent data of email campaign & corresponding open rate).

I had first converted the x-axes into time series data, while y-axes is the open rate in %, then I plotted the ggplot as normal. Below is my codes:

x= strptime(service_equity$`Send Date`, '%d/%m/%Y')
y = service_equity$`Open Rate`
ggplot(service_equity, aes(x,y)) + geom_point() + geom_smooth(method= "lm")   ggtitle("Trend in Open Rate")

The data used in the plot:

> x[1] "2015-02-24 GMT" "2014-09-16 BST" "2015-10-26 GMT" "2016-10-27 BST" "2017-01-19 GMT" "2015-06-25 BST" "2017-03-14 GMT" "2017-04-27 BST"

> y[1] "23.15%" "26.62%" "26.93%" "22.94%" "25.26%" "23.85%" "19.59%" "17.14%" "27.68%" "26.56%" "24.14%" "26.36%" "22.32%" "34.63%" "34.60%"

The scatter plot was successfully plotted, but the trend line isn't there, and there are no error message or notification regarding to the "geom_smooth" function.

enter image description here

Please help!

BlackBeard
  • 10,246
  • 7
  • 52
  • 62
Vincent Lao
  • 11
  • 1
  • 1
  • 1
    Have you seen these post's [1](https://stackoverflow.com/questions/38412817/draw-a-trend-line-using-ggplot), [2](https://stackoverflow.com/questions/11476849/creating-ggplot-stack-bar-and-trend-line), [3](https://stackoverflow.com/questions/10911057/adding-a-simple-lm-trend-line-to-a-ggplot-boxplot/10911838#10911838),[4](https://stackoverflow.com/questions/12810861/how-to-get-a-single-trendline-with-multiple-data-sets-in-r-and-ggplot2) and [5](https://stackoverflow.com/search?q=trendline+ggplot2). If yes, and you have not found a solution as yet, than please describe how your question is diff? – mnm Aug 21 '17 at 11:03
  • because your y axis is considered as a factor, due to the [%] sign – Mal_a Aug 21 '17 at 11:03

1 Answers1

1

geom_smooth() is not working due to the factor column --> your y axis is a factor because of [%] sign and for geom_smooth() to work, you need to have numeric data!

Get rid of [%] sign and geom_smooth() should work.

Mal_a
  • 3,670
  • 1
  • 27
  • 60