7

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")
4
  • 1
    It is unclear what is the problem. In the plot 1 you get 16 data points and trend line (geom_smooth). Also in the plot 2 you get 16 points but this plot is wrong because weight has to be used as integer and not the factor. Commented Feb 17, 2016 at 7:22
  • 1
    Factor variables are essentially categorical variables. You can't fit a linear or whatever function to them. As for the integer values not all showing up, you can use scale_y_continuous to customize limits and breaks. Commented Feb 17, 2016 at 7:23
  • Hi, Gopala, how would you use scale_y_continous in the present example so that I get the same 16 data points in the x axis as in plot 2? Commented Feb 17, 2016 at 14:54
  • I can see no plots in your question, and your link is broken. Maybe you could update the question, so that the rest of community can also learn from it? Commented Nov 17, 2019 at 13:11

1 Answer 1

2

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")
Sign up to request clarification or add additional context in comments.

2 Comments

Richo64, thank you very much for your answer. Unfortunately, it does not what I wanted. I want to have combination of plot 1 and 2, where I have the trend line with all 16 data points indicated at the x axis as in plot 2.
Does this answer help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.