ggplot2 geom_smooth 以变量为因子

Til*_*und 6 r ggplot2 dataframe

我有一个小问题,我自己无法解决这个问题。

我有一个简单的数据框,我想用ggplot2. 当我使用变量权重作为一个因子时,我会得到 x 轴上的所有值,s。plot 2,但不是当我将它用作整数时,s。情节 1。但是,我想使用geom_smooth,它似乎只在plot 1 中起作用,但在plot 2中不起作用,其中重量是一个因素。

我如何在 中得到一个图表ggplot2,它显示了所有的权重值和geom_smooth函数?

考虑这个示例文件:

require(ggplot2)

x <- get.url(https://dl.dropboxusercontent.com/u/109495328/example.csv)
app_df <- read.csv(x, header=T, sep = ",", quote = "", stringsAsFactors = FALSE, na.strings = "..")     
colnames(app_df) <- c("Date", "Weight")

date <- as.Date(strptime(app_df$Date, "%d.%m.%Y"))
weight <- app_df$Weight
df <- na.omit(data.frame(date,weight))

# plot 1 (only few values indicated in x axis)
ggplot(df, aes(date,weight)) +
  geom_point() +
  geom_line(aes(group = "1")) +
  geom_smooth(method = "lm")

# plot 2 (no smooth function)
ggplot(df, aes(date,as.factor(weight))) +
  geom_point() +
  geom_line(aes(group = "1")) +
  geom_smooth(method = "lm")
Run Code Online (Sandbox Code Playgroud)

Mis*_*ist 2

这就是你所追求的吗?

require(ggplot2)

x <- url("https://dl.dropboxusercontent.com/u/109495328/example.csv")
app_df <- read.csv(x, header=T, sep = ",", quote = "", stringsAsFactors = FALSE, na.strings = "..")     
colnames(app_df) <- c("Date", "Weight")

date <- as.Date(strptime(app_df$Date, "%d.%m.%Y"))
weight <- app_df$Weight
df <- na.omit(data.frame(date,weight))

# plot 1 (only few values indicated in x axis)
ggplot(df, aes(date,weight)) +
  geom_point() +
  geom_line(aes(group = "1")) +
  geom_smooth(method = "lm")

# plot 2 (no smooth function)
ggplot(df, aes(date,as.numeric(as.factor(weight)))) +
  geom_point() +
  geom_line(aes(group = "1")) +
  geom_smooth(method = "lm")
Run Code Online (Sandbox Code Playgroud)