More ggplot Steve Bagley somgen223.stanford.edu 1
More ggplot somgen223.stanford.edu 2
iris %>% ggplot ( aes (Petal.Length)) + geom_histogram () Histograms 20 count 10 0 2 4 6 Petal.Length • A histogram only needs a single variable (column). It guesses the size of a bin and counts the number of occurrences in each bin. somgen223.stanford.edu 3
iris %>% ggplot ( aes (Petal.Length)) + geom_histogram (binwidth = 1) Adjusting the binwidth 30 count 20 10 0 2 4 6 Petal.Length somgen223.stanford.edu 4
iris %>% ggplot ( aes (Petal.Length)) + geom_histogram (binwidth = 0.1) Adjusting the binwidth 10 count 5 0 2 4 6 Petal.Length • This histogram includes all the measurements from all the species. somgen223.stanford.edu 5
ggplot ( aes (Petal.Length)) + geom_histogram () + facet_wrap ( vars (Species)) iris %>% Histogram by species with facets setosa versicolor virginica 20 count 10 0 2 4 6 2 4 6 2 4 6 Petal.Length somgen223.stanford.edu 6
iris %>% ggplot ( aes (Petal.Length, fill = Species)) + geom_histogram (color = "black", alpha = 0.5, position = "identity") Histogram by species with overlays 20 Species setosa count versicolor 10 virginica 0 2 4 6 Petal.Length • position = "identity" means plot the three histograms on the same plot. • color = "black" makes the histogram border color be black. fill is the color of the contents of the bar. • alpha = 0.5 makes the histogram bars partially transparent. Note that the colors in overlapping regions add. somgen223.stanford.edu 7
ggplot ( aes (Petal.Length, color = Species)) + geom_freqpoly () iris %>% Frequency polygon (better for overlapping distributions) 20 Species setosa count versicolor 10 virginica 0 2 4 6 Petal.Length somgen223.stanford.edu 8
Changing the color scale • Scales control the mapping from the data to some visual property of the graph. • In ggplot, color generally refers to the color of a point or line, or the color of the boundary of a solid object, such as the bar of a histogram. • fill refers to the color filling a solid object. • Some scales apply to discrete-valued data. Some scales apply to continuous-valued data. • alpha is a number between 0 and 1 that describes the transparency of a geom, with 1 being completely opaque. somgen223.stanford.edu 9
BOD %>% ggplot ( aes (Time, demand)) + geom_col () Changing the bar color 20 15 demand 10 5 0 2 4 6 Time • geom_col makes a barplot when you have the actual y-value to plot. (Compare geom_bar .) somgen223.stanford.edu 10
BOD %>% ggplot ( aes (Time, demand)) + geom_col (color = "red") geom_col with color 20 15 demand 10 5 0 2 4 6 Time somgen223.stanford.edu 11
BOD %>% ggplot ( aes (Time, demand)) + geom_col (fill = "red") geom_col with fill 20 15 demand 10 5 0 2 4 6 Time somgen223.stanford.edu 12
BOD %>% ggplot ( aes (Time, demand)) + geom_col (fill = "red", color = "black") geom_col with fill and color 20 15 demand 10 5 0 2 4 6 Time somgen223.stanford.edu 13
Colors for discrete scales • For data with a discrete number of values (and usually not a large number), you can use the default colors, or change them. • scale_color_brewer has a number of different color palettes. • They are documented here: R color brewer palettes • The khroma package can help with palettes for those with color blindness. somgen223.stanford.edu 14
ggplot (iris, aes (Petal.Width, Petal.Length)) + geom_point ( aes (color = Species)) Example using default colors 6 Species Petal.Length setosa 4 versicolor virginica 2 0.0 0.5 1.0 1.5 2.0 2.5 Petal.Width somgen223.stanford.edu 15
geom_point ( aes (color = Species)) + scale_color_brewer (palette = "Set1") ggplot (iris, aes (Petal.Width, Petal.Length)) + Example using color brewer 6 Species Petal.Length setosa 4 versicolor virginica 2 0.0 0.5 1.0 1.5 2.0 2.5 Petal.Width somgen223.stanford.edu 16
geom_point ( aes (color = Species)) + scale_color_manual (values = c ("purple", "red", "orange")) ggplot (iris, aes (Petal.Width, Petal.Length)) + Example using your own list of colors 6 Species Petal.Length setosa 4 versicolor virginica 2 0.0 0.5 1.0 1.5 2.0 2.5 Petal.Width somgen223.stanford.edu 17
geom_point ( aes (color = Species)) + scale_x_log10 () ggplot (iris, aes (Petal.Width, log10 (Petal.Length))) + Changing scale of an axis 0.8 log10(Petal.Length) 0.6 Species setosa 0.4 versicolor virginica 0.2 0.0 0.1 0.3 1.0 Petal.Width • x-axis: original data, log scale • y-axis: log of original data somgen223.stanford.edu 18
Add annotations • Annotations are text or graphics that are added to the graph, usually to highlight some point or region. • geom_text adds text to each point. • annotate adds text (or other things) to a small number of points. somgen223.stanford.edu 19
BOD %>% ggplot ( aes (Time, demand)) + geom_point () + annotate ("text", x = 3, y = 18.5, label = "outlier?", color = "red") Annotation example 20.0 outlier? 17.5 15.0 demand 12.5 10.0 2 4 6 Time somgen223.stanford.edu 20
Reading • Read: 10 Mastering the grammar | ggplot2: Elegant Graphics for Data Analysis • Skim: as much of the rest of the book as you can • Skim: 28 Graphics for communication | R for Data Science • Interesting: Top 50 ggplot2 Visualizations - The Master List (With Full R Code) somgen223.stanford.edu 21
Recommend
More recommend