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.
Related
Context: I am trying to convert facet_grid to facet_wrap to add ncols. I know the cols=conference will change, but I am not sure how.
graph <- ggplot(data, aes(week, positivity_rate, color=school)) +
geom_line() +
facet_grid(cols=vars(conference))
graph
library(tidyverse)
ggplot(iris, aes(Petal.Length, Petal.Width, color=Species)) +
geom_line() +
facet_grid(cols=vars(Species))
ggplot(iris, aes(Petal.Length, Petal.Width, color=Species)) +
geom_line() +
facet_wrap(~Species,ncol = 2)
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")
I'd like to add a line below the x-axis where its color is dependant on a factor that is not plotted.
In this example, I'm creating a box plot and would like to add a line that indicates another variable.
Using the cars data set as an example and then physically dawing in what I'm trying to do:
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) +
geom_boxplot()
My thought was to create a bar, column, or geom_tile plot and then arrange it below the boxplot. This is how I would do it in base R. Is there a way to add in these kinds of color labels in ggplot2?
The natural way in ggplot2 to do this sort of thing would to be facet on the categorical variable to create subplots. However if you want to keep everything on the same graph you could try using a geom_tile() layer something like this:
df <-data.frame(x = factor(c(4,6,8)), colour = factor(c(1,2,1)))
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) +
geom_boxplot() +
geom_tile(data=df, aes(x = x, y = 8, fill = colour))
Alternatively as you suggest you could align an additional plot underneath it. You could use ggarrange() in the ggpubr package for this:
plot1 <- ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) +
geom_boxplot() +
geom_tile(data=df, aes(x = x, y = 10, fill = colour))
theme(legend.position = 'none')
plot2 <- ggplot(df, aes(x=x, y=1, fill = colour)) +
geom_tile() +
theme_void() +
scale_fill_manual(values=c('orange', 'green', 'orange')) +
theme(legend.position = 'none')
library(ggpubr)
ggarrange(plot1, plot2, nrow = 2, heights = c(10, 1), align = 'h')
I plot the average mpg in the mtcar data frame using ggplot. I get several points for each cylinder class denoting the mean value, categorized by the vs variable.
library(ggplot2)
ggplot(mtcars, aes(cyl, mpg)) + geom_point(aes(color = factor(vs)), stat = "summary", fun.y = "mean")
If I overlay these averages on top of the raw data by adding + geom_point (below) the averages differ from what they originally were above. What am I doing wrong? Why aren't the means consistent?
ggplot(mtcars, aes(cyl, mpg)) + geom_point() + geom_point(aes(color = factor(vs)), stat = "summary", fun.y = "mean")
How embarassing. I didn't even look at the scale of the Y-axis. Thank you aosmith. There is no inconsistency in stat_summary.
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())