Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the ".." refer to in ggplot's "fill=..density.."?

Tags:

r

ggplot2

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.

like image 996
Anton Avatar asked Dec 18 '13 16:12

Anton


People also ask

What does density mean Ggplot?

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.

What is fill in ggplot2?

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?

What does level do in the ggplot2 :: stat_density2d () function call? level.. tells ggplot to reference that column in the newly build data frame.

What does counts mean in R?

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


1 Answers

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

like image 62
Richie Cotton Avatar answered Sep 29 '22 03:09

Richie Cotton