Using aesthetics with multiple layers in ggplot - r

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

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!

difference between the two ways of using aes in ggplot?

I recently started learning R but am confused with the aes feature in ggplot2.
I have seen two different places where aes is placed in the code.
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point()
What is the difference between the two?
Can't find a dupe, so here's an answer:
Aesthetics specified in ggplot() are inherited by subsequent layers. Aesthetics specified in particular layers are specific only to that layer. Here's an example:
library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
geom_smooth()
ggplot(mtcars) +
geom_point(aes(wt, mpg)) +
geom_smooth() # error, geom_smooth needs its own aesthetics
This is mostly useful when you want different layers to have different specifications, for example these two plots are different, you have to decide which you want:
ggplot(mtcars, aes(wt, mpg, color = factor(cyl))) +
geom_point() +
geom_smooth()
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(color = factor(cyl))) +
geom_smooth()
On individual layers, you can use inherit.aes = FALSE to turn off inheritance for that layer. This is very useful if most of your layers use the same aesthetics, but a few do not.

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

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

Using colors in aes() function in ggplot2

I am new to ggplot2. 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. In a related thread, we discussed that we should try to avoid using variables inside aes() i.e. "Don't put constants inside aes() - only put mappings to actual data columns."
My objective is to observe the behavior of ggplots when we have color inside aes() for labeling (as described in Wickham's book) and also override the color to print the color.
I started with this:
library(ggplot2)
data(mpg)
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(aes(colour = "loess"), method = "loess", se = FALSE) +
geom_smooth(aes(colour = "lm"), method = "lm", se = FALSE) +
labs(colour = "Method")
This nicely plots graphs and labels them. However, I am unhappy with the colors used. So, I experimented with using overriding colors again:
windows()
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(aes(colour = "loess"), method = "loess", se = FALSE, color = "magenta") +
geom_smooth(aes(colour = "lm"), method = "lm", se = FALSE, color = "red") +
labs(colour ="Method")
I added color = "red" and we can see that labs() or aes(color()) doesn't have any effect. Why does this happen? I'm curious. I'd appreciate thoughts.
When you specify, the colour outside aes() gg_plot does not consider the color information being part of the data (and it overwrites previous information) , so there is no legend to display anymore.
If you want to specify your own colors and keep the colour information as "relevant data" and not "display information", you should add a scale_colour_manual() command to specify the legend colours and leave the colour attribute in aes:
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(aes(colour = "loess"), method = "loess", se = FALSE) +
geom_smooth(aes(colour = "lm"), method = "lm", se = FALSE) +
labs(colour ="Method") + scale_colour_manual(values = c("loess" = "magenta", "lm" = "red"))

Resources