I have a small issue, which I can ridiculously not solve myself.
I have a simple data frame, which I want to plot with ggplot2
. When I use the variable weight as a factor I get all values in the x-axis, s. plot 2, but not when I use it as integer, s. plot 1. However, I want to use geom_smooth
, which seems to function only with plot 1, but not in plot 2 where weight is a factor.
How do I get a graph in ggplot2
, which shows me all values of weight and additionally the geom_smooth
function?
Consider this example file:
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")
geom_smooth() and stat_smooth() are effectively aliases: they both use the same arguments. Use stat_smooth() if you want to display the results with a non-standard geom.
Geom_line creates a single line for both panels and distributes the colors according to the colour variable, while geom_smooth does not draw the smooth line in the 2nd panel.
The warning geom_smooth() using formula 'y ~ x' is not an error. Since you did not supply a formula for the fit, geom_smooth assumed y ~ x, which is just a linear relationship between x and y. You can avoid this warning by using geom_smooth(formula = y ~ x, method = "lm")
The geom smooth function is a function for the ggplot2 visualization package in R. Essentially, geom_smooth() adds a trend line over an existing plot.
Is this what you are after?
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With