commit
02223d296d
|
@ -51,7 +51,7 @@ One of the most useful tools in this quest are the values themselves, the values
|
||||||
|
|
||||||
The distribution of a variable reveals information about the probabilities associated with the variable. As you collect more data, the proportion of observations that occur at a value (or in an interval) will match the probability that the variable will take that value (or take a value in that interval) in a future measurement.
|
The distribution of a variable reveals information about the probabilities associated with the variable. As you collect more data, the proportion of observations that occur at a value (or in an interval) will match the probability that the variable will take that value (or take a value in that interval) in a future measurement.
|
||||||
|
|
||||||
In theory, it is easy to visualize the distribution of a variable; simply display how many observations occur at each value of the variable. In practice, how you do this will depend on the type of variable that you wish to visualize.
|
In theory, it is easy to visualize the distribution of a variable: simply display how many observations occur at each value of the variable. In practice, how you do this will depend on the type of variable that you wish to visualize.
|
||||||
|
|
||||||
##### Discrete distributions
|
##### Discrete distributions
|
||||||
|
|
||||||
|
@ -139,7 +139,7 @@ Several geoms exist to help you visualize continuous distributions. They almost
|
||||||
* `binwidth` - the width to use for the bins in the same units as the x variable
|
* `binwidth` - the width to use for the bins in the same units as the x variable
|
||||||
* `origin` - origin of the first bin interval
|
* `origin` - origin of the first bin interval
|
||||||
* `right` - if `TRUE` bins will be right closed (e.g. points that fall on the border of two bins will be counted with the bin to the left)
|
* `right` - if `TRUE` bins will be right closed (e.g. points that fall on the border of two bins will be counted with the bin to the left)
|
||||||
* `breaks` - a vector of actual bin breaks to use. If you set the breaks argument, it will overide the binwidth and origin arguments.
|
* `breaks` - a vector of actual bin breaks to use. If you set the breaks argument, it will override the binwidth and origin arguments.
|
||||||
|
|
||||||
Use `geom_histogram()` to make a traditional histogram. The height of each bar reveals how many observations fall within the width of the bar.
|
Use `geom_histogram()` to make a traditional histogram. The height of each bar reveals how many observations fall within the width of the bar.
|
||||||
|
|
||||||
|
@ -155,7 +155,7 @@ ggplot(data = diamonds) +
|
||||||
geom_histogram(aes(x = carat), binwidth = 1)
|
geom_histogram(aes(x = carat), binwidth = 1)
|
||||||
```
|
```
|
||||||
|
|
||||||
Notice how different binwidths reveal different information. The plot above shows that the availability of diamonds decreases quickly as carat size increases. The plot below shows that there are more diamonds than you would expect at whole carat sizes (and common fractions of carat sizes). Moreover, for each popular size, there are more diamonds that are slightly larger than the size than there are that are slightly smaller than the size.
|
Notice how different binwidths reveal different information. The plot above shows that the availability of diamonds decreases quickly as carat size increases. The plot below shows that there are more diamonds than you would expect at whole carat sizes (and common fractions of carat sizes). Moreover, for each popular size, there are more diamonds slightly larger than the size than diamonds slightly smaller than the size.
|
||||||
|
|
||||||
|
|
||||||
```{r}
|
```{r}
|
||||||
|
@ -288,7 +288,7 @@ You've probably heard that "correlation (covariation) does not prove causation."
|
||||||
|
|
||||||
Visualization is one of the best ways to spot covariation. How you look for covariation will depend on the structural relationship between two variables. The simplest structure occurs when two continuous variables have a functional relationship, where each value of one variable corresponds to a single value of the second variable.
|
Visualization is one of the best ways to spot covariation. How you look for covariation will depend on the structural relationship between two variables. The simplest structure occurs when two continuous variables have a functional relationship, where each value of one variable corresponds to a single value of the second variable.
|
||||||
|
|
||||||
In this scenario, covariation will appear as a pattern in the relationship. If two variables o not covary, their functional relationship will look like a random walk.
|
In this scenario, covariation will appear as a pattern in the relationship. If two variables do not covary, their functional relationship will look like a random walk.
|
||||||
|
|
||||||
The variables `date` and `unemploy` in the `economics` data set have a functional relationship. The `economics` data set comes with `ggplot2` and contains various economic indicators for the United States between 1967 and 2007. The `unemploy` variable measures the number of unemployed individuals in the United States in thousands.
|
The variables `date` and `unemploy` in the `economics` data set have a functional relationship. The `economics` data set comes with `ggplot2` and contains various economic indicators for the United States between 1967 and 2007. The `unemploy` variable measures the number of unemployed individuals in the United States in thousands.
|
||||||
|
|
||||||
|
@ -451,7 +451,7 @@ Control the appearance of the labels with the following arguments. You can also
|
||||||
* `hjust` - horizontal adjustment
|
* `hjust` - horizontal adjustment
|
||||||
* `vjust`- vertical adjustment
|
* `vjust`- vertical adjustment
|
||||||
|
|
||||||
Scatterplots do not work well with large data sets because individual points will begin to occlude each other. As a result, you cannot tell where the mass of the data lies. Does a black region contain a single layer of points? Or hundreds of points stacked on top of each other.
|
Scatterplots do not work well with large data sets because individual points will begin to occlude each other. As a result, you cannot tell where the mass of the data lies. Does a black region contain a single layer of points? Or hundreds of points stacked on top of each other?
|
||||||
|
|
||||||
You can see this type of plotting in the `diamonds` data set. The data set only contains 53,940 points, but the points overplot each other in a way that we cannot fix with jittering.
|
You can see this type of plotting in the `diamonds` data set. The data set only contains 53,940 points, but the points overplot each other in a way that we cannot fix with jittering.
|
||||||
|
|
||||||
|
@ -462,7 +462,7 @@ ggplot(data = diamonds) +
|
||||||
|
|
||||||
For large data, it is more useful to plot summary information that describes the raw data than it is to plot the raw data itself. Several geoms can help you do this.
|
For large data, it is more useful to plot summary information that describes the raw data than it is to plot the raw data itself. Several geoms can help you do this.
|
||||||
|
|
||||||
The simplest way to summarize covariance between two variables is with a model line. The model line displays the trend of the relationship between the variables.
|
The simplest way to summarise covariance between two variables is with a model line. The model line displays the trend of the relationship between the variables.
|
||||||
|
|
||||||
Use `geom_smooth()` to display a model line between any two variables. As with `geom_rug()`, `geom_smooth()` works well as a second layer for a plot (See Section 3 for details).
|
Use `geom_smooth()` to display a model line between any two variables. As with `geom_rug()`, `geom_smooth()` works well as a second layer for a plot (See Section 3 for details).
|
||||||
|
|
||||||
|
@ -476,7 +476,7 @@ ggplot(data = diamonds) +
|
||||||
|
|
||||||
`geom_smooth()` will also plot a standard error band around the model line. You can remove the standard error band by setting the `se` argument of `geom_smooth()` to `FALSE`.
|
`geom_smooth()` will also plot a standard error band around the model line. You can remove the standard error band by setting the `se` argument of `geom_smooth()` to `FALSE`.
|
||||||
|
|
||||||
Use the `model` argument of `geom_smooth()` to adda specific type of model line to your data. `model` takes the name of an R modeling function. `geom_smooth()` will use the function to calculate the model line. For example, the code below uses R's `lm()` function to fit a linear model line to the data.
|
Use the `method` argument of `geom_smooth()` to add a specific type of model line to your data. `method` takes the name of an R modeling function. `geom_smooth()` will use the function to calculate the model line. For example, the code below uses R's `lm()` function to fit a linear model line to the data.
|
||||||
|
|
||||||
```{r}
|
```{r}
|
||||||
ggplot(data = diamonds) +
|
ggplot(data = diamonds) +
|
||||||
|
@ -511,7 +511,7 @@ Useful arguments for `geom_smooth()` are:
|
||||||
* `level` - Confidence level to use for standard error ribbon
|
* `level` - Confidence level to use for standard error ribbon
|
||||||
* `method` - Smoothing function to use, a model function in R
|
* `method` - Smoothing function to use, a model function in R
|
||||||
* `n` - The number of points to evaluate smoother at (defaults to 80)
|
* `n` - The number of points to evaluate smoother at (defaults to 80)
|
||||||
* `se` - If TRUE` (the default), `geom_smooth()` will include a standard error ribbon
|
* `se` - If `TRUE` (the default), `geom_smooth()` will include a standard error ribbon
|
||||||
|
|
||||||
Be careful, `geom_smooth()` will overlay a trend line on every data set, even if the underlying data is uncorrelated. You can avoid being fooled by also inspecting the raw data or calculating the correlation between your variables, e.g. `cor(diamonds$carat, diamonds$price)`.
|
Be careful, `geom_smooth()` will overlay a trend line on every data set, even if the underlying data is uncorrelated. You can avoid being fooled by also inspecting the raw data or calculating the correlation between your variables, e.g. `cor(diamonds$carat, diamonds$price)`.
|
||||||
|
|
||||||
|
@ -540,7 +540,7 @@ Useful arguments for `geom_quantile()` are:
|
||||||
* `formula` - the formula to use in the smoothing function
|
* `formula` - the formula to use in the smoothing function
|
||||||
* `quantiles` - Conditional quantiles of $y$ to display. Each quantile is displayed with a line.
|
* `quantiles` - Conditional quantiles of $y$ to display. Each quantile is displayed with a line.
|
||||||
|
|
||||||
`geom_smooth()` and `geom_quantile()` summarize the relationship between two variables as a function, but you can also summarize the relationship as a bivariate distribution.
|
`geom_smooth()` and `geom_quantile()` summarise the relationship between two variables as a function, but you can also summarise the relationship as a bivariate distribution.
|
||||||
|
|
||||||
`geom_bin2d()` divides the coordinate plane into a two dimensional grid and then displays the number of observations that fall into each bin in the grid. This technique let's you see where the mass of the data lies; bins with a light fill color contain more data than bins with a dark fill color. Bins with no fill contain no data at all.
|
`geom_bin2d()` divides the coordinate plane into a two dimensional grid and then displays the number of observations that fall into each bin in the grid. This technique let's you see where the mass of the data lies; bins with a light fill color contain more data than bins with a dark fill color. Bins with no fill contain no data at all.
|
||||||
|
|
||||||
|
@ -565,7 +565,7 @@ Useful arguments for `geom_bin2d()` are:
|
||||||
* `binwidth` - A vector like `c(0.1, 500)` that gives the binwidths to use in the horizontal and vertical directions. Overrides `bins` when set.
|
* `binwidth` - A vector like `c(0.1, 500)` that gives the binwidths to use in the horizontal and vertical directions. Overrides `bins` when set.
|
||||||
* `drop` - If `TRUE` (default) `geom_bin2d()` removes the fill from all bins that contain zero observations.
|
* `drop` - If `TRUE` (default) `geom_bin2d()` removes the fill from all bins that contain zero observations.
|
||||||
|
|
||||||
`geom_hex()` works similarly to `geom_bin2d()`, but it divides the coordinate plain into hexagon shaped bins. This can reduce visual artifacts that are introduced by the aligning edges of rectangular bins.
|
`geom_hex()` works similarly to `geom_bin2d()`, but it divides the coordinate plane into hexagon shaped bins. This can reduce visual artifacts that are introduced by the aligning edges of rectangular bins.
|
||||||
|
|
||||||
```{r}
|
```{r}
|
||||||
ggplot(data = diamonds) +
|
ggplot(data = diamonds) +
|
||||||
|
@ -574,7 +574,7 @@ ggplot(data = diamonds) +
|
||||||
|
|
||||||
`geom_hex()` requires the `hexbin` package, which you can install with `install.packages("hexbin")`.
|
`geom_hex()` requires the `hexbin` package, which you can install with `install.packages("hexbin")`.
|
||||||
|
|
||||||
`geom_density2d()` uses density contours to display similar information. It is the two dimensional equivalent of `geom_density()`. Interpret a two dimensional density plot the same way you would interpret a contour map. Each line connects an area of equal density, which makes changes of slope easy to see.
|
`geom_density2d()` uses density contours to display similar information. It is the two dimensional equivalent of `geom_density()`. Interpret a two dimensional density plot the same way you would interpret a contour map. Each line connects points of equal density, which makes changes of slope easy to see.
|
||||||
|
|
||||||
As with other summary geoms, `geom_density2d()` makes a useful second layer.
|
As with other summary geoms, `geom_density2d()` makes a useful second layer.
|
||||||
|
|
||||||
|
@ -600,7 +600,7 @@ Useful arguments for `geom_density2d()` are:
|
||||||
|
|
||||||
##### Visualize correlations between three variables
|
##### Visualize correlations between three variables
|
||||||
|
|
||||||
There are two ways to add three (or more) variables to a two dimensional plot. You can map additional variables to aesthics within the plot, or you can use a geom that is designed to visualize three variables.
|
There are two ways to add three (or more) variables to a two dimensional plot. You can map additional variables to aesthetics within the plot, or you can use a geom that is designed to visualize three variables.
|
||||||
|
|
||||||
`ggplot2` provides three geoms that are designed to display three variables: `geom_raster()`, `geom_tile()` and `geom_contour()`. These geoms generalize `geom_bin2d()` and `geom_density()` to display a third variable instead of a count, or a density.
|
`ggplot2` provides three geoms that are designed to display three variables: `geom_raster()`, `geom_tile()` and `geom_contour()`. These geoms generalize `geom_bin2d()` and `geom_density()` to display a third variable instead of a count, or a density.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue