I am trying to solve a question which says:

Use ggplot() to plot the graphs to see the relationship between variables GS with COA.

I am writing it as:

c <- ggplot(data, aes(x=GS, y=COA)) + 
       geom_point(shape=1) + 
       geom_smooth(method=lm, color = "yellow", se=TRUE) + 
       xlab("GS") + 

ggMarginal(c, type="histogram")

But it also shows me a linear line. The second question is plotting a linear regression model between GS and COA and plot the regression (least-squares) line on the same plot.

My code above it showing the regression line as well. I am new to R, just started learning ggplot() and how it works. Does my code cover both problems 1 and 2 or do I have to change the code for the first problem? Should I use some other method of ggplot() to show the relationship?


1 Answer 1


It is difficult to see what you're trying to accomplish without a minimal reproducible example.

The following code should be sufficient to answer the first question, though without some example data we cannot actually investigate the relationship between $x$ and $y$. That I will leave to you.

# 1st Step    

ggplot(data, aes(x = GS, y = COA)) + 
  geom_point(shape = 1)

Next, you were asked to plot the regression line on the same plot. Well, in that case, just add geom_smooth() and it will overlay the fitted line.

# 2nd Step

ggplot(data, aes(x = GS, y = COA)) + 
  geom_point(shape = 1) +
  geom_smooth(method = "lm", color = "yellow", se = TRUE)  # layer on the next geom

By layering on the geom_smooth() function you're fitting a least squares regression line to the observed data. The line is drawn over the data points.

Technically, you're addressing both questions in your call to ggplot(). For explication purposes, I will use your code on the mtcars dataset which is preloaded in R. This is a scatterplot of all cars in the dataset. It clearly shows the negative relationship between weight and fuel economy. Try running each line of code sequentially and note how each layer adds a new piece of information to the plot. Below the plot is output from a linear regression model using the lm() function; this is a simple summary of what you're observing in the plot.

# Trying out your code on the mtcars dataset in R

ggplot(data = mtcars, aes(x = wt, y = mpg)) + 
  geom_point(shape = 1) + 
  geom_smooth(method = "lm", color = "yellow", se = TRUE) + 
  xlab("Weight") + 
  ylab("Miles Per Gallon") +


> summary(mod <- lm(mpg ~ wt, data = mtcars))

lm(formula = mpg ~ wt, data = mtcars)

    Min      1Q  Median      3Q     Max 
-4.5432 -2.3647 -0.1252  1.4096  6.8727 

            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
wt           -5.3445     0.5591  -9.559 1.29e-10 ***
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.046 on 30 degrees of freedom
Multiple R-squared:  0.7528,    Adjusted R-squared:  0.7446 
F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10

We lose approximately 5 miles per gallon with each additional half-ton increase in weight. Note this ignores all other variables in the dataset. The intercept is where the line would cross the $y$-axis. Note: geom_smooth() will not extrapolate beyond the observed data, at least not by default. Try setting the limits of the $x$-axis from 0 to 6 and then include one additional parameter to the smoothing function: geom_smooth(..., fullrange = TRUE).

I hope this helps your understanding of how ggplot() works in practice.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.