geom_smooth不会为我的数据框绘制一条线

时间:2017-11-08 12:51:28

标签: r ggplot2

我有一个包含以下数据的数据框

my2016.regression.dataframe <- structure(list(Economy_Directorate = structure(c(9L, 1L, 18L, 
11L, 5L, 7L), .Label = c("20128895", "25392278", "26802176", 
"33214069", "34194316", "34863777", "34867843", "36497785", "37280694", 
"37411816", "44460126", "45484123", "47463441", "48354697", "57954259", 
"60187650", "65135916", "67317188"), class = "factor"), People_Directorate = structure(c(12L, 
14L, 17L, 16L, 13L, 15L), .Label = c("20128895", "25392278", 
"26802176", "33214069", "34194316", "34863777", "34867843", "36497785", 
"37280694", "37411816", "44460126", "45484123", "47463441", "48354697", 
"57954259", "60187650", "65135916", "67317188"), class = "factor")), .Names = c("Economy_Directorate", 
"People_Directorate"), row.names = c(NA, -6L), class = "data.frame")

我使用以下代码绘制它。它绘制了点,但它没有绘制lm。 你能不能帮我解释为什么它没有在geom_smooth中绘制lm

library(ggplot2)
ggplot(data =my2016.regression.dataframe )+
  geom_point(aes(y=Economy_Directorate,x=People_Directorate))+
  geom_smooth(method = "lm",aes(y=Economy_Directorate,x=People_Directorate),
              fill="orange",colour="red")

此致

1 个答案:

答案 0 :(得分:1)

您需要将列转换为数字类型。它们是目前的因素:

my2016.regression.dataframe$Economy_Directorate = as.numeric(as.character(my2016.regression.dataframe$Economy_Directorate))
my2016.regression.dataframe$People_Directorate = as.numeric(as.character(my2016.regression.dataframe$People_Directorate))

ggplot(data = my2016.regression.dataframe) +
  geom_point(aes(y=Economy_Directorate,x=People_Directorate))+
  geom_smooth(method = "lm",aes(y=Economy_Directorate,x=People_Directorate),
              fill="orange",colour="red")

enter image description here