I am working my way through The R Graphics Cookbook and ran into this set of code:
library(gcookbook)
library(ggplot2)
p <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
geom_point() +
stat_density2d(aes(alpha=..density.., fill=..density..), geom="tile", contour=FALSE)
It runs fine, but I don't understand what the ..
before and after density
is referring to. I can't seem to find it mentioned in the book either.
A density plot is a representation of the distribution of a numeric variable. It is a smoothed version of the histogram and is used in the same kind of situation. Here is a basic example built with the ggplot2 library.
Generally, fill defines the colour with which a geom is filled, whereas colour defines the colour with which a geom is outlined (the shape's "stroke", to use Photoshop language).
What does level do in the ggplot2 :: stat_density2d () function call? level.. tells ggplot to reference that column in the newly build data frame.
count() lets you quickly count the unique values of one or more variables: df %>% count(a, b) is roughly equivalent to df %>% group_by(a, b) %>% summarise(n = n()) .
Variable names beginning with ..
are possible in R, and are treated in the same way as any other variable. Trying creating one of your own.
..x.. <- 1:5
ggplot2
often creates appends extra columns to your data frame in order to draw the plot. (In ggplot2
terminology, this is "fortifying the data".) ggplot2
uses the naming convention ..something..
for these fortified columns.
This is partly because using ..something..
is unlikely to clash with existing variables in your dataset. Take that as a hint that you shouldn't name the columns in your dataset using that pattern.
The stat_density*
functions use ..density..
to represent the density of the x variable. Other fortified variable names include ..count..
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With