Plot dashed regression line with geom_smooth in ggplot2 - r

I have a simple plot in ggplot2 and want to add a dashed regression line. So far I have:
library(ggplot2)
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
theme_bw()
Which returns what I want, but with a solid line:
I want to make the line dashed. I think I should use scale_linetype_manual() but my attempts have been hacky.
A simple question, but I couldn't find a duplicate.

As per the help page (see ?geom_smooth), linetype is one of the aesthetics geom_smooth understands.
So, you can adjust to use geom_smooth(method = "lm", se = FALSE, linetype="dashed")
library(ggplot2)
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, linetype = "dashed") +
theme_bw()

Related

Increase width of ribbon when increasing size of linear model line in ggplot2

I am plotting a simple linear model with ggplot2. However, when I increase the size of the line, the ribbon does not change size (understandable). However, how would I scale the ribbon so that it matches the increase in line thickness?
Here is a simple example using the iris dataset:
library(ggplot2)
ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) +
geom_point() +
stat_smooth(method = "lm", col = "red")
As you can see when you increase size (I'm over exaggerating the size here), then more of the ribbon is covered up.
ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) +
geom_point() +
stat_smooth(method = "lm", col = "red", size = 5)
Essentially, the ribbon only needs to extend out as much as the additional thickness of the line that is displacing/obscuring the ribbon.
Expected Results
Perhaps like this?
ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) +
geom_point() +
stat_smooth(method = "lm", col = "red", size = 5,
aes(ymin = after_stat(y - 5*se),
ymax = after_stat(y + 5*se)))

R weird behavior with geom_abline()

I am using this code:
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_smooth(method = "lm", se = FALSE, color = '#376795', size = 1) +
geom_abline(intercept = 34.232237, slope = -4.539474, linetype = 'dashed')
And I get this graph:
Then I comment out the middle line of code with command + shift + c
ggplot(mtcars, aes(x = wt, y = mpg)) +
# geom_smooth(method = "lm", se = FALSE, color = '#376795', size = 1) +
geom_abline(intercept = 34.232237, slope = -4.539474, linetype = 'dashed')
I get a graph without any lines. Where did the line from geom_abline() go?
I then switch the order and be careful with the + signs...
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_abline(intercept = 34.232237, slope = -4.539474, linetype = 'dashed') +
geom_smooth(method = "lm", se = FALSE, color = '#376795', size = 1)
Both lines are back. So the code for geom_abline() seemed fine, right?
So I then comment out the middle line:
ggplot(mtcars, aes(x = wt, y = mpg)) +
# geom_abline(intercept = 34.232237, slope = -4.539474, linetype = 'dashed') +
geom_smooth(method = "lm", se = FALSE, color = '#376795', size = 1)
The geom_smooth() is there but not the abline. I'm really confused by this behavior. I really just want the abline and not the smooth but this doesn't work:
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_abline(intercept = 34.232237, slope = -4.539474, linetype = 'dashed')
There must be a simple reason. But also - why is the behavior inconsistent? It feels like a bug because the same code in one place seems to work and in another place doesn't.
You can use this code to plot only the abline:
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_blank() +
geom_abline(intercept = 34.232237, slope = -4.539474, linetype = 'dashed')
Output:

Changing legend in geom_density

I cannot understand why the legend is not changing given the following code. The same options do the trick with geom_histogram. Thanks in advance for any assistance.
data(mtcars)
ggplot(mtcars, aes(x = disp, color = as.factor(am))) +
geom_density(aes(group = am)) +
theme_classic() +
guides(fill = guide_legend(reverse=TRUE)) +
labs(x = "Displacement", y = "Density") +
scale_fill_manual(name="",values=c("black","gray"),labels=c("Foreign","Domestic"))
You used color in your call of aes(). To modify the scale for this variable you need to use scale_color_manual and not scale_fill_manual.
It's tricky because geom_histogram does use fill, but geom_density uses color.
Working solution :
data(mtcars)
ggplot(mtcars, aes(x = disp, color = as.factor(am))) +
geom_density(aes(group = am)) +
theme_classic() +
guides(fill = guide_legend(reverse=TRUE)) +
labs(x = "Displacement", y = "Density") +
scale_color_manual(name="",values=c("black","gray"),labels=c("Foreign","Domestic"))

How to have regression lines in a different color brewer pallete than the points in my scatter plot?

Using GapMinder data, I've made the plot below with a different regression line by continent:
Here is the code:
ggplot(gapminder_82,
aes(gdpPercap, lifeExp, color = continent)) +
geom_point() +
scale_x_log10() +
scale_color_brewer(palette = "Set2") +
geom_smooth(method = "lm", se = F)
The problem is that the lines aren't really visible. So I'd like to use 2 different color palettes from color brewer. The Pastel2 for the points, but I'd like to use "Dark2" for the lines. It would make the lines stand out.
How would I do it?
You can use a filled point shape for the points, allowing you to use a fill scale for the points and colour for the lines:
ggplot(gapminder_82,
aes(gdpPercap, lifeExp)) +
# Make the edge color for the points totally transparent
geom_point(aes(fill = continent), shape = 21, size = 3, colour = "#FFFFFF00") +
scale_x_log10() +
geom_smooth(aes(color = continent), method = "lm", se = F) +
scale_fill_brewer(palette = "Pastel2") +
scale_color_brewer(palette = "Dark2") +
theme_bw()
Result:
Even if separate color palettes were possible, I think it would lead to confusion since you would be mapping the same variable to two different colours.
How about adjusting the alpha of the points to increase the visibility of the lines?
gapminder_82 %>%
ggplot(aes(gdpPercap, lifeExp)) +
geom_point(aes(color = continent), alpha = 0.1) +
geom_smooth(method = "lm",
se = FALSE,
aes(color = continent)) +
scale_x_log10() +
scale_color_brewer(palette = "Set2") +
theme_bw()

Adding a weighted least squares trendline in ggplot2

I am preparing a plot using ggplot2, and I want to add a trendline that is based on a weighted least squares estimation.
In base graphics this can be done by sending a WLS model to abline:
mod0 <- lm(ds$dMNP~ds$MNP)
mod1 <- lm(ds$dMNP~ds$MNP, weights = ds$Asset)
symbols(ds$dMNP~ds$MNP, circles=ds$r, inches=0.35)
#abline(mod0)
abline(mod1)
in ggplot2 I set the argument weight in geom_smooth but nothing changes:
ggplot(ds, aes(x=MNP, y=dMNP, size=Asset) +
geom_point(shape=21) +
geom_smooth(method = "lm", weight="Asset", color="black", show.legend = FALSE)
this gives me the same plot as
ggplot(ds, aes(x=MNP, y=dMNP, size=Asset) +
geom_point(shape=21) +
geom_smooth(method = "lm", color="black", show.legend = FALSE)
I'm late, but for posterity and clarity, here is the full solution:
ggplot(ds, aes(x = MNP, y = dMNP, size = Asset)) +
geom_point(shape = 21) +
geom_smooth(method = "lm", mapping = aes(weight = Asset),
color = "black", show.legend = FALSE)
Don't put the weight name in quotes.

Resources