Renata
Renata

Reputation: 1

How to generate one polynomial regression line for mapped variables?

I am plotting a polynomial regression for two different watershed data(they are separated as color=Treatment in my code). I am able to plot two different regression lines for each Treatment, but I am trying to plot the combined watershed data as one singular polynomial regression line. I would like to keep the color-coded data points as is.

This is the code I currently have:

ggplot(my_data, aes(x=pH, y=NH4, color=Treatment)) +
  geom_point(pch=21, size=0.5,alpha = 1) + 
  geom_smooth(method = "lm", formula = y ~ x + I(x^2), se = FALSE) +
  labs(y= "Ammonium", x = "pH") +
  theme_classic() + scale_y_continuous(
    expand = c(0, 0),
    limits = c(0, NA)) +
  theme(legend.title = element_blank()) +
  theme(legend.position = c(0.9, 0.9)) +
scale_color_manual(labels = c("Reference", "Ca-Treated"),
                   values = c("coral1", "darkturquoise"))

The link to the graph is below. As you can see, the code generates two separate lines but I would like one line for the combined data. How can I modify the code? TIA

Upvotes: 0

Views: 183

Answers (1)

TJ Mahr
TJ Mahr

Reputation: 3964

In ggplot2, we create a plot by defining a mapping between data features and plotting aesthetics. We use those mappings to draw plotting geometry (geoms and stats) in different layers.

In ggplot(data, aes(...)), you are setting a default data set and default aesthetic (aes()) for all layers in the plot. So when you use aes(x = pH, y = NH4, color = Treatment), you are setting all layers of the plot use draw their geometry using with colors based on the Treatment column. We can fix this in a couple of different ways.

First, we can not use a global color mapping and instead set the color mapping for just the layer that needs it. I think this approach is the best.

library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) + 
  geom_point(
    # the key line
    aes(color = factor(am))
  ) +
  stat_smooth(
    method = "lm", 
    formula = y ~ poly(x, 2, raw = TRUE), 
    se = FALSE
  )  +
  labs(caption = "layer specific color")

plot of teal and orange points with a blue line drawn through them

Second, we can use a global mapping, but override the mapping for just the layer in question. Here we set the mapping to NULL so that the color is not use.

ggplot(mtcars, aes(x = wt, y = mpg, color = factor(am))) + 
  geom_point() +
  stat_smooth(
    # remove that mapping for this layer
    aes(color = NULL),
    method = "lm", 
    formula = y ~ poly(x, 2, raw = TRUE), 
    se = FALSE
  ) +
  labs(caption = "layer color set to NULL")

plot of teal and orange points with a blue line drawn through them

Finally, and this case applies more for drawing annotations, we can use a global mapping but set the color mapping to a specific value, rather than rely on the data for the mapping.

ggplot(mtcars, aes(x = wt, y = mpg, color = factor(am))) + 
  geom_point() +
  stat_smooth(
    # Override the mapping by manually setting the aesthetic. 
    # Note that we are not inside aes() here.
    color = "maroon",
    method = "lm", 
    formula = y ~ poly(x, 2, raw = TRUE), 
    se = FALSE
  ) +
  labs(caption = "override layer color")

plot of teal and orange points with a maroon line drawn through them

Upvotes: 1

Related Questions