Create lines parallel to my `geom_smooth(method = lm)` line - r

library(tidyverse)
ggplot(mpg, aes(cty, hwy)) +
geom_point() +
geom_smooth(method = lm)
I would like two purple lines to run parallel to my geom_smooth(method = lm) line. Each line should be 10 units away from the geom_smooth(method = lm) line. One line would be above, and the other line below the geom_smooth(method = lm) line.
How do I accomplish this?

As this answer suggests, there doesn't seem to be a natural way to shift a line. What we may do then is to use multiple geom_smooth with different offsets:
ggplot(mpg, aes(cty, hwy)) +
geom_point() +
lapply(c(-10, 0, 10), function(o)
geom_smooth(method = lm, formula = y + o ~ x))

Related

Add an extra point to a single facet in ggplot

I want to add a single point to only one facet. I tried to set the group in geom_point:
ggplot(data=mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~class) + geom_point(aes(x=5, y=25, group = "pickup"), colour="blue")
But that put a blue dot in all facets. Is there any way to constrain the point to only one?
You could achieve your desired result by putting the information for your point in a dataframe which you pass to the data argument of your second geom_point:
library(ggplot2)
ggplot(data=mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~class) +
geom_point(data = data.frame(displ = 5, hwy = 25, class = "pickup"), colour="blue")

Matching legends for ggplot2

I refer to section 12.2.4, exercise 1 in Hadley Wickham's ggplot2 book.
In the exercise, the following code is provided:
fwd <- subset(mpg, drv == "f")
rwd <- subset(mpg, drv == "r")
ggplot(fwd, aes(displ, hwy, colour = class)) + geom_point()
ggplot(rwd, aes(displ, hwy, colour = class)) + geom_point()
Which produces the following output:
The following task is given: Modify the code so that the legend and axes match, without using facetting!
I am able to modify so that the axes match:
ggplot(fwd, aes(displ, hwy, colour = class)) + geom_point() + xlim(c(0, 7)) + ylim(c(0, 45))
ggplot(rwd, aes(displ, hwy, colour = class)) + geom_point() + xlim(c(0, 7)) + ylim(c(0, 45))
yielding:
But, I'm unable to get the legends to match. This question comes before scale_colour_manual() is explained, so I don't think we're supposed to use that.
Any ideas about how to get the legends to match?
Thanks!

ggplot2: How to add linebreak to horizontal legend

Please consider the following R script (taken and slightly modified from here):
require(ggplot2)
x <- 1:10
y <- jitter(x^2)
DF <- data.frame(x, y)
p <- ggplot(DF, aes(x = x, y = y)) + geom_point() +
stat_smooth(method = 'lm', aes(colour = 'linear')) +
stat_smooth(method = 'lm', formula = y ~ poly(x,2),
aes(colour = 'polynomial')) +
stat_smooth(method = 'nls', formula = y ~ a * log(x) +b,
aes(colour = 'logarithmic')) +
stat_smooth(method = 'nls', formula = y ~ a*exp(b *x),
aes(colour = 'Exponential')) +
theme(legend.position = "top")
p <- p + guides(guide_legend(ncol=2,nrow=2,byrow=TRUE))
p
The legend is displayed at the top of the plot. I want to break this legend into two lines, with two keys in each line. Is this possible?
Please note that, as you may see, I already tried
p+guides(guide_legend(ncol=2,nrow=2,byrow=TRUE))
as suggested here and here, but it did not work for me. This suggestion basically displays the data and the legends of the linear and polynomial models and completely hides the logarithmic and exponential models.
As explained by eipi10,
You need specify which legend, in this case the colour legend: guides(colour=guide_legend(ncol=2,nrow=2,byrow=TRUE)).
To clarify, the aesthetic is defining the colour of each line. If fill were used, the line could be guides(fill=guide_legend(ncol=2,nrow=2,byrow=TRUE)).

Using aesthetics with multiple layers in ggplot

I am new to ggplot. I am trying to understand how to use ggplot. I am reading Wickham's book and still trying to wrap my head around how to use aes() function.
What's the difference between these two implementations of aes():
library(ggplot2)
ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
theme(legend.position = "none")
and
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(colour = class)) +
geom_smooth(method = "lm", se = FALSE) +
theme(legend.position = "none")
Both of them print significantly different graphs. Any help? I am really stuck.
In the first one you are mapping the aesthetics globally, ggplot will try to map these aesthetics to all other geom_xyz() layers.
While in the latter case, you are mapping aesethics to a specific ggplot layer (in your case geom_point())

Plot dashed regression line with geom_smooth in ggplot2

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()

Resources