2

I'm trying to plot the lm line using this code

df <- data.frame(c1=factor(c(1,1,1,1,2,2,2,2,3,3,3,3)),c2=c(65,42,56,75,43,43,21,23,12,12,21,11)) p <- ggplot(aes(x=c1,y=c2),data=df) p + geom_point() + geom_smooth(method="lm") 

But the lm line is not shown. Am I missing something?

5
  • Possible duplicate of stackoverflow.com/questions/15633714/… Commented May 5, 2018 at 14:56
  • 1
    Why is 'c1' a factor? Change it to numeric and you should be able to get the line Commented May 5, 2018 at 15:02
  • 2
    If you do happen to require a factor, set aes(group = 1). Commented May 5, 2018 at 15:03
  • 1
    @Axeman: Thanks, that was it! Could you turn your comment into an answer so that I can accept it? Commented May 5, 2018 at 15:06
  • Possible duplicate. Commented May 5, 2018 at 15:08

1 Answer 1

2

The reason is that c1 is a factor. Converting it to numeric will solve the problem

library(dplyr) library(ggplot2) df %>% mutate(c1 = as.numeric(as.character(c1)) %>% ggplot(aes(x = c1, y = c2)) + geom_point() + geom_smooth(method="lm") 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.