geom_smooth() Not Plotting Best Fit LIne

I know I'm probably missing something related to syntax or that someone else might have already asked a similar question (I'm very novice) but I can't get geom_smooth() to plot a best fit line for this chart. What can I do in order to get geom_smooth() in ggplot to work? Should I use ggplot2 to get a best fit line instead?

# install tidyverse package
install.packages("tidyverse")

# install readr library
library(readr)

library(readxl)

responses <- read_excel("Abreviated Responses.xlsx")


# Don't forget to Capitalize V
View(responses)

# Import data from excel sheet in from the range AB1 through AB7
rating_info <- read_excel('Abreviated Responses.xlsx', sheet = 1, range = 'AB1:AB71')

# Import data from excel sheet in from the range AQ1 through AQ7A
current_understanding <- read_excel("Abreviated Responses.xlsx", sheet = 1, range = 'AQ1:AQ71')

# Create a Data frame from both imported data vectors
rating_vs_understaing<-data.frame(rating_info, current_understanding)

# Plot the Data frame (this is where the question about geom_smooth is not working)
ggplot(data = rating_vs_understaing, aes(x= X4B_Rate_Information, y = X7B_Currently_Understand)) + geom_point() + geom_smooth(method = lm, se = FALSE)

# Plot the Data frame, jitter() will slightly randomize the results since the data set is too small and similar
ggplot(data = rating_vs_understaing, aes(x= X4B_Rate_Information, y = X7B_Currently_Understand)) + geom_point() + geom_jitter()

Are you after the regression line? See this:

Also, your code isn't reproducible as we don't have your dataset. See:

Thank you! This is part of one of my first projects in RStudio. I do not have permission to publish the data (as it is a part of a capstone) however, I'm guessing that it might be possible that my dataset might not be large enough to produce a best fit line.

You don't have to literally publish the data, you could rather provide example data that has similar enough properties to approximate your data.
for example

# example data
set.seed(42)
(rating_info <- data.frame(X4B_Rate_Information = 1:25))
(current_understanding <- data.frame(X7B_Currently_Understand = 1:25 + sample(-5:5,size = 25,replace=TRUE)))


# Create a Data frame from both imported data vectors
rating_vs_understaing<-data.frame(rating_info, current_understanding)

# Plot the Data frame (this is where the question about geom_smooth is not working)
ggplot(data = rating_vs_understaing, aes(x= X4B_Rate_Information, y = X7B_Currently_Understand)) + geom_point() + geom_smooth(method = lm, se = FALSE)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.