用R中的ggplot绘制趋势线

时间:2017-08-21 10:56:28

标签: r ggplot2 trendline

我一直在尝试为我的ggplot添加趋势线(关于发送电子邮件广告系列的数据和相应的开放率)。

我首先将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 个答案:

答案 0 :(得分:1)

geom_smooth()由于因子列 - > 而无效,因为[%]符号,y轴是一个因素,而geom_smooth()可以使用需要有数字数据!

删除[%]符号,geom_smooth()应该有用。

相关问题