2017-08-21 239 views
-1

我一直在試圖爲我的ggplot添加一個趨勢線(關於發送的電子郵件活動&的相應打開率)。使用ggplot繪製趨勢線R

我首先將x軸轉換爲時間序列數據,而y軸是打開率(%),然後我將ggplot繪製爲正常。下面是我的代碼:

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") 

在劇情中使用的數據:

> 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%" 

散點圖已成功繪製,但趨勢線是不存在的,並且沒有錯誤消息或通知關於「geom_smooth」功能。

enter image description here

請幫幫忙!

+1

你見過這些貼子的[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-toa-a- ggplot-箱線/ 10911838#10911838),[4](https://stackoverflow.com/questions/12810861/how-to-get-a-single-trendline-with-multiple-data-sets-in-r-and -ggplot2)和[5](https://stackoverflow.com/search?q=trendline+ggplot2)。如果是,並且您還沒有找到解決方案,請描述您的問題是如何區別的? – Ashish

+0

,因爲您的y軸被視爲一個因素,由於[%]符號 –

回答

1

geom_smooth()不因性因素列工作- >你的Y軸是因爲[%]標誌的一個因素,是geom_smooth()工作,你需要有數字數據!

擺脫[%]號和geom_smooth()應該工作。