Familiarize Yourself Again With the Mtcars Dataset Using Str().

Stats with geoms

ggplot2, form ii

  • statistics
  • coordinates
  • facets
  • information visualization all-time practices

statistics layer

  • two categories of functions
    • chosen from inside a geom
    • called independently
  • stats_

non-parametric model is default - loess

Smoothing

To practice on the remaining layers (statistics, coordinates and facets), we'll continue working on several datasets from the first course.

The mtcars dataset contains information for 32 cars from Motor Trends magazine from 1974. This dataset is small, intuitive, and contains a variety of continuous and categorical (both nominal and ordinal) variables.

In the previous class you learned how to effectively use some basic geometries, such equally point, bar and line. In the first chapter of this form y'all'll explore statistics associated with specific geoms, for instance, smoothing and lines.

            library(ggplot2)  # View the structure of mtcars str(mtcars)          
            ## 'data.frame':    32 obs. of  eleven variables: ##  $ mpg : num  21 21 22.8 21.4 18.vii 18.1 14.3 24.four 22.8 xix.ii ... ##  $ cyl : num  6 half-dozen 4 6 8 6 eight 4 four half dozen ... ##  $ disp: num  160 160 108 258 360 ... ##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ... ##  $ drat: num  3.ix iii.9 3.85 3.08 3.15 2.76 three.21 3.69 3.92 3.92 ... ##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ... ##  $ qsec: num  16.5 17 18.half dozen 19.4 17 ... ##  $ vs  : num  0 0 i 1 0 ane 0 1 1 one ... ##  $ am  : num  i 1 1 0 0 0 0 0 0 0 ... ##  $ gear: num  4 4 four 3 3 3 three 4 4 four ... ##  $ carb: num  4 4 1 1 2 i 4 2 two four ...          
            # Using mtcars, draw a scatter plot of mpg vs. wt ggplot(mtcars, aes(x = wt, y = mpg)) +   geom_point()          

            # Amend the plot to add a smoothen layer ggplot(mtcars, aes(x = wt, y = mpg)) +   geom_point() +   geom_smooth()          
            ## `geom_smooth()` using method = 'loess' and formula 'y ~ ten'          

            # Amend the plot. Use lin. reg. smoothing; plough off std err ribbon ggplot(mtcars, aes(10 = wt, y = mpg)) +   geom_point() +   geom_smooth(method = "lm", se = Fake)          
            ## `geom_smooth()` using formula 'y ~ x'          

            # Amend the plot. Bandy geom_smooth() for stat_smooth(). ggplot(mtcars, aes(x = wt, y = mpg)) +   geom_point() +   stat_smooth(method = "lm", se = FALSE)          
            ## `geom_smooth()` using formula 'y ~ x'          

Good job! You can use either stat_smooth() or geom_smooth() to utilize a linear model. Remember to always call back virtually how the examples and concepts we discuss throughout the information viz courses can exist applied to your own datasets!

Group variables

We'll continue with the previous exercise by because the situation of looking at sub-groups in our dataset. For this we'll encounter the invisible group aesthetic.

mtcars has been given an extra column, fcyl, that is the cyl column converted to a proper cistron variable.

            mtcars$fcyl <- every bit.factor(mtcars$cyl)  # Using mtcars, plot mpg vs. wt, colored by fcyl ggplot(mtcars, aes(10 = wt, y = mpg, color = fcyl)) +   # Add a point layer   geom_point() +   # Add a smooth lin reg stat, no ribbon   stat_smooth(method = "lm", se = False)          
            ## `geom_smooth()` using formula 'y ~ x'          

            # Ameliorate the plot to add another smooth layer with dummy group ggplot(mtcars, aes(x = wt, y = mpg, color = fcyl)) +   geom_point() +   stat_smooth(method = "lm", se = Faux) +   stat_smooth(aes(group = 1), method = "lm", se = False)          
            ## `geom_smooth()` using formula 'y ~ x' ## `geom_smooth()` using formula 'y ~ 10'          

Good task! Notice that the color aesthetic defined an invisible grouping aesthetic. Defining the group aesthetic for a specific geom means we tin can overwrite that. Hither, nosotros use a dummy variable to calculate the smoothing model for all values.

Modifying stat_smooth

In the previous exercise we used se = Simulated in stat_smooth() to remove the 95% Confidence Interval. Hither we'll consider another argument, bridge, used in LOESS smoothing, and we'll take a look at a overnice scenario of properly mapping different models.

            ggplot(mtcars, aes(x = wt, y = mpg)) +   geom_point() +   # Add together 3 shine LOESS stats, varying bridge & colour   stat_smooth(color = "carmine", span = 0.9, se = Imitation) +   stat_smooth(color = "greenish", span = 0.6, se = Imitation) +   stat_smooth(color = "blue", span = 0.3, se = Fake)          
            ## `geom_smooth()` using method = 'loess' and formula 'y ~ x' ## `geom_smooth()` using method = 'loess' and formula 'y ~ 10' ## `geom_smooth()` using method = 'loess' and formula 'y ~ x'          

            # Amend the plot to color by fcyl ggplot(mtcars, aes(x = wt, y = mpg)) +   geom_point() +   # Add a smoothen LOESS stat, no ribbon   stat_smooth(se = FALSE) +   # Add a smooth lin. reg. stat, no ribbon   stat_smooth(method = "lm", se = FALSE)          
            ## `geom_smooth()` using method = 'loess' and formula 'y ~ x'          
            ## `geom_smooth()` using formula 'y ~ x'          

            # Meliorate the plot ggplot(mtcars, aes(ten = wt, y = mpg, color = fcyl)) +   geom_point() +   # Map color to dummy variable "All"   stat_smooth(aes(color = "All"), se = Imitation) +   stat_smooth(method = "lm", se = Faux)          
            ## `geom_smooth()` using method = 'loess' and formula 'y ~ ten' ## `geom_smooth()` using formula 'y ~ x'          

Spantastic! The default span for LOESS is 0.9. A lower span will outcome in a improve fit with more item; only don't overdo it or you'll end up over-fitting!

Modifying stat_smooth (2)

In this exercise we'll have a look at the standard error ribbons, which show the 95% confidence interval of smoothing models. ggplot2 and the Vocab information frame are already loaded for you.

Vocab has been given an actress column, year_group, splitting the dates into earlier and after 1995.

            head(Vocab)          
                          year    sex didactics vocabulary  year_group 19870607 1987 Female        12          5 [1974,1995] 19900423 1990 Female        14          seven [1974,1995] 19962610 1996 Female person        17          6 (1995,2016] 20141153 2014   Male        14          5 (1995,2016] 19840045 1984 Female person        12          8 [1974,1995] 20140694 2014   Male        14          5 (1995,2016]          
            # Using Vocab, plot vocabulary vs. education, colored by year group ggplot(Vocab, aes(x = education, y = vocabulary, color = year_group)) +   # Add together jittered points with transparency 0.25   geom_jitter(alpha = 0.25) +   # Add a shine lin. reg. line (with ribbon)   stat_smooth(method = "lm")          

            # Improve the plot ggplot(Vocab, aes(x = education, y = vocabulary, color = year_group)) +   geom_jitter(alpha = 0.25) +   # Map the fill colour to year_group, set the line size to 2   stat_smooth(aes(fill up = year_group), method = "lm", size = 2)          

Y'all take a vast plotting vocabulary! Find that since 1995, education has relatively smaller outcome on increasing vocabulary.

Stats: sum and quantile

Quantiles

Here, we'll keep with the Vocab dataset and employ stat_quantile() to apply a quantile regression.

Linear regression predicts the mean response from the explanatory variables, quantile regression predicts a quantile response (e.g. the median) from the explanatory variables. Specific quantiles can be specified with the quantiles statement.

Specifying many quantiles and color your models according to year can brand plots too busy. We'll explore ways of dealing with this in the side by side affiliate.

            ggplot(Vocab, aes(x = education, y = vocabulary)) +   geom_jitter(alpha = 0.25) +   # Add a quantile stat, at 0.05, 0.five, and 0.95   stat_quantile(quantiles = c(0.05, 0.v, 0.95))          

            # Better the plot to colour by year_group ggplot(Vocab, aes(x = didactics, y = vocabulary, colour = year_group)) +   geom_jitter(alpha = 0.25) +   stat_quantile(quantiles = c(0.05, 0.v, 0.95))          

Quick quantiles! Quantile regression is a peachy tool for getting a more detailed overview of a large dataset.

Using stat_sum

In the Vocab dataset, instruction and vocabulary are integer variables. In the offset course, you saw that this is one of the four causes of overplotting. Yous'd get a single point at each intersection between the two variables.

Ane solution, shown in the footstep 1, is jittering with transparency. Another solution is to use stat_sum(), which calculates the total number of overlapping observations and maps that onto the size artful.

stat_sum() allows a special variable, ..prop.., to show the proportion of values within the dataset.

            # Run this, look at the plot, then update it ggplot(Vocab, aes(10 = education, y = vocabulary)) +   # Replace this with a sum stat   geom_jitter(alpha = 0.25)          

            # update information technology ggplot(Vocab, aes(x = education, y = vocabulary)) +   # Replace this with a sum stat   stat_sum(alpha = 0.25)          

            ggplot(Vocab, aes(x = education, y = vocabulary)) +   stat_sum() +   # Add a size scale, from 1 to 10   scale_size(range = c(ane, 10))          

            # Ameliorate the stat to use proportion sizes ggplot(Vocab, aes(x = instruction, y = vocabulary)) +   stat_sum(aes(size = ..prop..))          

            # Ameliorate the plot to grouping by didactics ggplot(Vocab, aes(x = teaching, y = vocabulary, grouping = education)) +   stat_sum(aes(size = ..prop..))          

Superb stat summing! If a few information points overlap, jittering is dandy. When y'all have lots of overlaps (particularly where continuous data has been rounded), using stat_sum() to count the overlaps is more useful.

Stats outside geoms

Preparations

In the following exercises, we'll aim to make the plot shown in the viewer. Here, we'll plant our positions and base layer of the plot.

Establishing these items as independent objects will permit us to recycle them easily in many layers, or plots.

  • position_jitter() adds jittering (e.g. for points).
  • position_dodge(http://www.rdocumentation.org/packages/ggplot2/functions/position_dodge) dodges geoms, (e.grand. bar, col, boxplot, violin, errorbar, pointrange).
  • position_jitterdodge(http://www.rdocumentation.org/packages/ggplot2/functions/position_jitterdodge) jitters and dodges geoms, (e.g. points).

Every bit before, we'll employ mtcars, where fcyl and fam are proper cistron variables of the original cyl and am variables.

            mtcars$fam <- as.cistron(mtcars$am)  # Ascertain position objects # 1. Jitter with width 0.ii posn_j <- position_jitter(width = 0.2)  # 2. Contrivance with width 0.i posn_d <- position_dodge(width = 0.one)  # 3. Jitter-dodge with jitter.width 0.2 and dodge.width 0.ane posn_jd <- position_jitterdodge(jitter.width = 0.2, dodge.width = 0.i)  # Create the plot base: wt vs. fcyl, colored by fam p_wt_vs_fcyl_by_fam <- ggplot(mtcars, aes(x = fcyl, y = wt, color = fam))  # Add a point layer p_wt_vs_fcyl_by_fam +   geom_point()          

Patient preparation! The default positioning of the points is highly susceptible to overplotting.

Using position objects

Now that the position objects have been created, yous can apply them to the base plot to encounter their effects. You do this by adding a betoken geom and setting the position statement to the position object.

The variables from the final exercise, posn_j, posn_d, posn_jd, and p_wt_vs_fcyl_by_fam are bachelor in your workspace.

            # Add together jittering but p_wt_vs_fcyl_by_fam +   geom_point(position=posn_j)          

            # Add together dodging but p_wt_vs_fcyl_by_fam +   geom_point(position=posn_d)          

            # Add jittering and dodging p_wt_vs_fcyl_by_fam +   geom_point(position=posn_jd)          

Perfect positioning! Although you tin set up position by setting the position statement to a string (for example position = "contrivance"), defining objects promotes consistency between layers.

Plotting variations

The preparation is washed; now allow'south explore stat_summary().

Summary statistics refers to a combination of location (mean or median) and spread (standard deviation or confidence interval).

These metrics are calculated in stat_summary() past passing a role to the fun.data argument. mean_sdl(), calculates multiples of the standard deviation and mean_cl_normal() calculates the t-corrected 95% CI.

Arguments to the data part are passed to stat_summary()'s fun.args argument as a list.

The position object, posn_d, and the plot with jittered points, p_wt_vs_fcyl_by_fam_jit, are available.

            p_wt_vs_fcyl_by_fam_jit <- p_wt_vs_fcyl_by_fam +   geom_point(position=posn_j)  p_wt_vs_fcyl_by_fam_jit +   # Add together a summary stat of std divergence limits   stat_summary(fun.data = mean_sdl, fun.args = list(mult = 1), position = posn_d)          

            p_wt_vs_fcyl_by_fam_jit +   # Alter the geom to be an errorbar   stat_summary(fun.information = mean_sdl, fun.args = list(mult = one), position = posn_d, geom="errorbar")          

            p_wt_vs_fcyl_by_fam_jit +   # Add a summary stat of normal confidence limits   stat_summary(fun.data = mean_cl_normal, position = posn_d)          

Good job! You tin always assign your ain function to the fun.data argument every bit long as the result is a information frame and the variable names match the aesthetics that you will demand for the geom layer.

Coordinates

Coordinates layer

  • controls plot dimensions
  • coord_
    • e.chiliad.coord_cartesian()

Zooming in

  • coord_cartesian(xlim = ...)
  • scale_x_continuous(limits = ...)
  • xlim(...)

Aspect ratio

  • height to width ratio
  • watch out for deception!
  • no universal standard and then far
  • typically use 1:i if information is on the aforementioned scale

Zooming In

In the video, you saw unlike means of using the coordinates layer to zoom in. In this exercise, we'll compare zooming past changing scales and past irresolute coordinates.

The large difference is that the calibration functions change the underlying dataset, which affects calculations fabricated by computed geoms (like histograms or shine trend lines), whereas coordinate functions make no changes to the dataset.

A scatter plot using mtcars with a LOESS smoothed trend line is provided. Take a look at this before updating information technology.

            ggplot(mtcars, aes(ten = wt, y = hp, colour = fam)) +   geom_point() +   geom_smooth()                      
            ## `geom_smooth()` using method = 'loess' and formula 'y ~ x'          

            ggplot(mtcars, aes(x = wt, y = hp, color = fam)) +   geom_point() +   geom_smooth() +   # Add a continuous ten scale from 3 to 6   scale_x_continuous(limits=c(3, vi))          
            ## `geom_smooth()` using method = 'loess' and formula 'y ~ ten'          
            ## Warning: Removed 12 rows containing non-finite values (stat_smooth).          
            ## Warning in simpleLoess(y, x, west, span, degree = degree, parametric = ## parametric, : bridge too small-scale. fewer data values than degrees of freedom.          
            ## Alarm in simpleLoess(y, x, due west, span, degree = caste, parametric = ## parametric, : at three.168          
            ## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = ## parametric, : radius 4e-006          
            ## Alarm in simpleLoess(y, x, due west, span, degree = degree, parametric = ## parametric, : all data on boundary of neighborhood. make span bigger          
            ## Warning in simpleLoess(y, ten, w, span, degree = degree, parametric = ## parametric, : pseudoinverse used at 3.168          
            ## Alert in simpleLoess(y, ten, westward, span, degree = caste, parametric = ## parametric, : neighborhood radius 0.002          
            ## Warning in simpleLoess(y, ten, westward, span, degree = degree, parametric = ## parametric, : reciprocal condition number 1          
            ## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = ## parametric, : at 3.572          
            ## Alarm in simpleLoess(y, 10, westward, span, caste = degree, parametric = ## parametric, : radius 4e-006          
            ## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = ## parametric, : all data on purlieus of neighborhood. make bridge bigger          
            ## Warning in simpleLoess(y, x, due west, bridge, degree = degree, parametric = ## parametric, : At that place are other near singularities as well. 4e-006          
            ## Alarm in simpleLoess(y, x, w, span, degree = degree, parametric = ## parametric, : zero-width neighborhood. make span bigger  ## Warning in simpleLoess(y, x, w, bridge, caste = caste, parametric = ## parametric, : zippo-width neighborhood. make span bigger          
            ## Alarm: Computation failed in `stat_smooth()`: ## NA/NaN/Inf in foreign part call (arg 5)          
            ## Warning: Removed 12 rows containing missing values (geom_point).          

            ggplot(mtcars, aes(x = wt, y = hp, color = fam)) +   geom_point() +   geom_smooth() +   # Add Cartesian coordinates with x limits from 3 to 6   coord_cartesian(xlim = c(3, six))          
            ## `geom_smooth()` using method = 'loess' and formula 'y ~ x'          

Zesty zooming! Using the calibration role to zoom in meant that there wasn't plenty data to calculate the tendency line, and geom_smooth() failed. Whencoord_cartesian() was applied, the full dataset was used for the trend adding.

Aspect ratio I: i:1 ratios

We can set up the aspect ratio of a plot with coord_fixed(), which uses ratio = 1 equally a default. A one:1 attribute ratio is most appropriate when 2 continuous variables are on the same calibration, as with the iris dataset.

All variables are measured in centimeters, so it only makes sense that one unit on the plot should be the same physical distance on each centrality. This gives a more truthful depiction of the relationship between the two variables since the aspect ratio can change the angle of our smoothing line. This would give an erroneous impression of the data. Of class the underlying linear models don't modify, but our perception can exist influenced by the angle drawn.

A plot using the iris dataset, of sepal width vs. sepal length colored by species, is shown in the viewer.

            ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +   geom_jitter() +   geom_smooth(method = "lm", se = FALSE) +   # Set the coordinate ratio   coord_fixed()          
            ## `geom_smooth()` using formula 'y ~ x'          

Awe-inspiring aspect amending! A 1:1 aspect ratio is helpful when your axes testify the same scales.

Aspect ratio 2: setting ratios

When values are not on the same scale information technology can exist a bit tricky to set up an advisable aspect ratio. A archetype William Cleveland (inventor of dot plots) example is the sunspots data set. We accept 3200 observations from 1750 to 2016.

sun_plot is a plot without any set aspect ratio. Information technology fills upward the graphics device.

To brand aspect ratios clear, we've drawn an orangish box that is 75 units high and 75 years wide. Using a 1:1 attribute ratio would make the box square. That aspect ratio would brand things harder to come across the oscillations: it is better to force a wider ratio.

            # Fix the aspect ratio to 1:one sun_plot +   coord_fixed()          

            # Change the attribute ratio to 20:1 sun_plot +   coord_fixed(ratio=xx)          

Fun plots with sunspots! Making a wide plot by calling coord_fixed() with a high ratio is often useful for long time serial.

Aggrandize and clip

The coord_*() layer functions offer two useful arguments that work well together: expand and prune.

  • aggrandize sets a buffer margin around the plot, so data and axes don't overlap. Setting expand to 0 draws the axes to the limits of the data.
  • prune decides whether plot elements that would lie outside the plot panel are displayed or ignored ("clipped").

When done properly this can brand a great visual outcome! We'll use theme_classic() and modify the axis lines in this example.

            ggplot(mtcars, aes(wt, mpg)) +   geom_point(size = 2) +   theme_classic() +   # Add Cartesian coordinates with zippo expansion   coord_cartesian(aggrandize = 0)          

            ggplot(mtcars, aes(wt, mpg)) +   geom_point(size = 2) +   # Turn clipping off   coord_cartesian(expand = 0, clip = "off") +   theme_classic() +   # Remove axis lines   theme(centrality.line = element_blank())          

Cool clipping! These arguments brand clean and accurate plots by not cutting off data.

Coordinates vs. scales

Log-transforming scales

Using scale_y_log10() and scale_x_log10() is equivalent to transforming our bodily dataset before getting to ggplot2.

Using coord_trans(), setting 10 = "log10" and/or y = "log10" arguments, transforms the data after statistics have been calculated. The plot will look the same equally with using scale_*_log10(), but the scales will be different, meaning that nosotros'll see the original values on our log10 transformed axes. This can be useful since log scales can be somewhat unintuitive.

Let'southward see this in activeness with positively skewed information - the brain and body weight of 51 mammals from the msleep dataset.

            # Produce a scatter plot of brainwt vs. bodywt ggplot(msleep, aes(x=bodywt, y=brainwt)) +   geom_point() +   ggtitle("Raw Values")          
            ## Warning: Removed 27 rows containing missing values (geom_point).          

            # Add together scale_*_*() functions ggplot(msleep, aes(bodywt, brainwt)) +   geom_point() +   scale_x_log10() +   scale_y_log10() +   ggtitle("Scale_ functions")          
            ## Warning: Removed 27 rows containing missing values (geom_point).          

            # Perform a log10 coordinate system transformation ggplot(msleep, aes(bodywt, brainwt)) +   geom_point() +   coord_trans(10 = "log10", y = "log10")          
            ## Alert: Removed 27 rows containing missing values (geom_point).          

            # Plot with transformed coordinates ggplot(msleep, aes(bodywt, brainwt)) +   geom_point() +   geom_smooth(method = "lm", se = Imitation) +   # Add a log10 coordinate transformation for x and y axes    coord_trans(x = "log10", y = "log10")          
            ## `geom_smooth()` using formula 'y ~ 10'          
            ## Alert: Removed 27 rows containing not-finite values (stat_smooth).  ## Warning: Removed 27 rows containing missing values (geom_point).          

Terrific transformations! Each transformation method has implications for the plot's interpretability. Think about your audience when choosing a method for applying transformations.

Calculation stats to transformed scales

In the last exercise, we saw the usefulness of the coord_trans() function, but be careful! Remember that statistics are calculated on the untransformed information. A linear model may end up looking not-and then-linear afterwards an axis transformation. Let's revisit the two plots from the previous exercise and compare their linear models.

            # Plot with a scale_*_*() function: ggplot(msleep, aes(bodywt, brainwt)) +   geom_point() +   geom_smooth(method = "lm", se = FALSE) +   # Add a log10 x scale   scale_x_log10() +   # Add a log10 y calibration   scale_y_log10() +   ggtitle("Scale functions")          
            ## `geom_smooth()` using formula 'y ~ x'          
            ## Alert: Removed 27 rows containing non-finite values (stat_smooth).          
            ## Warning: Removed 27 rows containing missing values (geom_point).          

            # Plot with transformed coordinates ggplot(msleep, aes(bodywt, brainwt)) +   geom_point() +   geom_smooth(method = "lm", se = FALSE) +   # Add a log10 coordinate transformation for x and y axes    coord_trans(x = "log10", y = "log10")          
            ## `geom_smooth()` using formula 'y ~ x'          
            ## Alarm: Removed 27 rows containing non-finite values (stat_smooth).  ## Warning: Removed 27 rows containing missing values (geom_point).          

Loopy lines! The smooth trend line is calculated after scale transformations but non coordinate transformations, so the second plot doesn't brand sense. Be careful when using the coord_trans() function!

Double and flipped axes

Typical axis modifications

  • aspect ratios
    • adapt for best perspective
  • transformation functions
    • accommodate if original scale is inappropriate
  • double x or y axes
    • add raw and transformed values
  • flipped axes
    • change direction of dependencies
    • change geometry orientation

Useful double axes

Double ten and y-axes are a contentious topic in information visualization. We'll revisit that discussion at the end of chapter iv. Here, I desire to review a peachy employ instance where double axes actually practise add value to a plot.

Our goal plot is displayed in the viewer. The two axes are the raw temperature values on a Fahrenheit calibration and the transformed values on a Celsius calibration.

You can imagine a like scenario for Log-transformed and original values, miles and kilometers, or pounds and kilograms. A scale that is unintuitive for many people tin can be made easier by calculation a transformation as a double axis.

            airquality$Date <- as.Date(paste('1973', airquality$Month, airquality$Day), '%Y %m %d')    # Using airquality, plot Temp vs. Appointment ggplot(airquality, aes(ten=Date, y=Temp)) +   # Add together a line layer   geom_line() +   labs(ten = "Date (1973)", y = "Fahrenheit")          

            # Define breaks (Fahrenheit) y_breaks <- c(59, 68, 77, 86, 95, 104)  # Convert y_breaks from Fahrenheit to Celsius y_labels <- (y_breaks - 32) * 5 / 9  # Create a secondary x-axis secondary_y_axis <- sec_axis(   # Employ identity transformation   trans = identity,   name = "Celsius",   # Define breaks and labels as higher up   breaks = y_breaks,   labels = y_labels )  # Examine the object secondary_y_axis          
            ## <ggproto object: Class AxisSecondary, gg> ##     centrality: NULL ##     break_info: function ##     breaks: 59 68 77 86 95 104 ##     create_scale: function ##     detail: 1000 ##     empty: function ##     guide: waiver ##     init: function ##     labels: 15 20 25 30 35 40 ##     make_title: role ##     mono_test: function ##     name: Celsius ##     trans: part ##     transform_range: function ##     super:  <ggproto object: Class AxisSecondary, gg>          
            # Update the plot ggplot(airquality, aes(Date, Temp)) +   geom_line() +   # Add the secondary y-axis    scale_y_continuous(sec.axis = secondary_y_axis) +   labs(10 = "Date (1973)", y = "Fahrenheit")          

Dazzling double axes! Double axes are nearly useful when you want to brandish the same value in two differnt units.

Flipping axes I

Flipping axes means to opposite the variables mapped onto the x and y aesthetics. Nosotros tin can only change the mappings in aes(), but nosotros can as well apply the coord_flip() layer function.

There are two reasons to use this function:

  • Nosotros want a vertical geom to exist horizontal, or
  • We've completed a long series of plotting functions and want to flip it without having to rewrite all our commands.
            # Plot fcyl bars, filled past fam ggplot(mtcars, aes(fcyl, fill = fam)) +   # Place bars adjacent   geom_bar(position = "contrivance")          

            ggplot(mtcars, aes(fcyl, fill = fam)) +   geom_bar(position = "dodge") +   # Flip the x and y coordinates   coord_flip()          

            ggplot(mtcars, aes(fcyl, fill = fam)) +   # Set a dodge width of 0.5 for partially overlapping bars   geom_bar(position = position_dodge(width=0.v)) +   coord_flip()          

Flipping fantastic! Horizontal confined are especially useful when the axis labels are long.

Flipping axes II

In this exercise, nosotros'll continue to utilize the coord_flip() layer function to opposite the variables mapped onto the x and y aesthetics.

Within the mtcars dataset, car is the name of the car and wt is its weight.

            mtcars$car <- row.names(mtcars)  # Plot of wt vs. car ggplot(mtcars, aes(ten=machine, y=wt)) +   # Add a point layer   geom_point() +   labs(ten = "car", y = "weight")          

            # Flip the axes to set auto to the y axis ggplot(mtcars, aes(auto, wt)) +   geom_point() +   labs(x = "auto", y = "weight") +   coord_flip()          

Even funkier flips! Discover how much more interpretable the plot is subsequently flipping the axes.

##Polar coordinates

Polar coordinates

  • Cartesian (2d)
    • orthogonal x and y-axes
  • Maps
    • many projections
  • Polar
    • transformed Cartesian space

Pie charts

The coord_polar() function converts a planar x-y Cartesian plot to polar coordinates. This can exist useful if y'all are producing pie charts.

Nosotros can imagine two forms for pie charts - the typical filled circle, or a colored ring.

Typical pie charts omit all of the non-data ink, which we saw in the themes chapter of the final course. Pie charts are not really better than stacked bar charts, but nosotros'll come dorsum to this point in the next affiliate.

A bar plot using mtcars of the number of cylinders (as a factor), fcyl, is shown in the plot viewer.

            ggplot(mtcars, aes(ten = 1, fill = fcyl)) +   geom_bar()          

            ggplot(mtcars, aes(ten = 1, fill = fcyl)) +   geom_bar() +   # Add a polar coordinate system   coord_polar(theta="y")          

            ggplot(mtcars, aes(x = 1, fill = fcyl)) +   # Reduce the bar width to 0.i   geom_bar(width=0.1) +   coord_polar(theta = "y") +   # Add together a continuous x calibration from 0.5 to one.v   scale_x_continuous(limits=c(0.5, ane.5))          

Super-fly pie! Polar coordinates are particularly useful if you are dealing with a cycle, like yearly data, that you would like to come across represented as such.

Air current rose plots

Polar coordinate plots are well-suited to scales similar compass direction or time of twenty-four hour period. A popular example is the "current of air rose".

The wind dataset is taken from the openair package and contains hourly measurements for windspeed (ws) and direction (wd) from London in 2003. Both variables are factors.

            library(openair) library(forcats) library(dplyr)          
            ##  ## Attaching package: 'dplyr'          
            ## The following objects are masked from 'package:stats': ##  ##     filter, lag          
            ## The following objects are masked from 'package:base': ##  ##     intersect, setdiff, setequal, union          
            rose_breaks <- c(0, 360/32, (i/32 + (one:15 / 16)) * 360, 360) rose_labs <- c(   "N", "NNE", "NE", "ENE", "Due east", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", "Northward") ws_labs <- c("0 - two", "ii - 4", "4 - 6", "half-dozen - 8", "8 - 10", "10 - 12", "12 - 14") air current <- selectByDate(mydata[c("appointment", "ws", "wd")], start = "2003-01-01", end = "2003-12-31") air current$ws <- equally.cistron(cut(current of air$ws, breaks = c(0,2,iv,6,8,10,12,14), labels = ws_labs)) wind$wd <- as.gene(cut(wind$wd, breaks = rose_breaks, labels = rose_labs)) wind <- air current[consummate.cases(air current),]  # Using wind, plot wd filled by ws ggplot(wind, aes(x=wd, fill=ws)) +   # Add together a bar layer with width ane   geom_bar(width=ane)          

            # Convert to polar coordinates: ggplot(wind, aes(wd, fill = ws)) +   geom_bar(width = 1) +   coord_polar()          

            # Catechumen to polar coordinates: ggplot(wind, aes(wd, fill up = ws)) +   geom_bar(width = 1) +   coord_polar(start = -pi/sixteen)          

Perfect polar coordinates! They are non common, just polar coordinate plots are really useful.

The facets layer

Facets

  • straight-forward all the same useful
  • concept of pocket-sized multiples
    • popularized by Edward Tufte
    • visualization of quantitative information, 1983

Facet layer basics

Faceting splits the data up into groups, according to a categorical variable, so plots each group in its own panel. For splitting the data by 1 or two categorical variables, facet_grid() is all-time.

Given categorical variables A and B, the code pattern is

            plot +   facet_grid(rows = vars(A), cols = vars(B))          

This draws a console for each pairwise combination of the values of A and B.

Here, we'll use the mtcars data set to do. Although cyl and am are not encoded as cistron variables in the data set, ggplot2 will coerce variables to factors when used in facets.

            ggplot(mtcars, aes(wt, mpg)) +    geom_point() +   # Facet rows by am   facet_grid(rows=vars(am))          

            ggplot(mtcars, aes(wt, mpg)) +    geom_point() +   # Facet columns past cyl   facet_grid(cols=vars(cyl))          

            ggplot(mtcars, aes(wt, mpg)) +    geom_point() +   # Facet rows by am and columns past cyl   facet_grid(rows=vars(am), cols=vars(cyl))          

Fantastic faceting! Compare the different plots that effect and see which one makes near sense.

Many variables

In improver to aesthetics, facets are some other style of encoding cistron (i.e. categorical) variables. They can be used to reduce the complexity of plots with many variables.

Our goal is the plot in the viewer, which contains 7 variables.

Two variables are mapped onto the colour artful, using hue and lightness. To achieve this we combined fcyl and fam into a single interaction variable, fcyl_fam. This will allow u.s. to accept reward of Color Brewer'due south Paired colour palette.

            mtcars$fcyl_fam <- interaction(mtcars$fcyl, mtcars$fam, sep=":")  # See the interaction column mtcars$fcyl_fam          
            ##  [1] half-dozen:ane 6:i four:1 half dozen:0 viii:0 6:0 8:0 4:0 iv:0 6:0 6:0 8:0 eight:0 8:0 8:0 viii:0 viii:0 iv:1 4:1 ## [xx] 4:1 4:0 8:0 8:0 eight:0 8:0 four:1 4:1 4:1 eight:one 6:1 8:ane 4:1 ## Levels: 4:0 half-dozen:0 eight:0 4:one 6:1 8:1          
            # Color the points past fcyl_fam ggplot(mtcars, aes(10 = wt, y = mpg, color = fcyl_fam)) +   geom_point() +   # Use a paired color palette   scale_color_brewer(palette = "Paired")          

            # Update the plot to map disp to size ggplot(mtcars, aes(x = wt, y = mpg, color = fcyl_fam, size = disp)) +   geom_point() +   scale_color_brewer(palette = "Paired")          

            # Update the plot ggplot(mtcars, aes(x = wt, y = mpg, colour = fcyl_fam, size = disp)) +   geom_point() +   scale_color_brewer(palette = "Paired") +   # Grid facet on gear and vs   facet_grid(rows = vars(gear), cols = vars(vs))          

Expert job! The last plot y'all've created contains 7 variables (iv categorical, 3 continuous). Useful combinations of aesthetics and facets help to achieve this.

Formula notation

As well as the vars() annotation for specifying which variables should be used to split the dataset into facets, there is also a traditional formula notation. The 3 cases are shown in the table.

Modern notation Formula notation
facet_grid(rows = vars(A)) facet_grid(A ~ .)
facet_grid(cols = vars(B)) facet_grid(. ~ B)
facet_grid(rows = vars(A), cols = vars(B)) facet_grid(A ~ B)

mpg_by_wt is available again. Rework the previous plots, this time using formula note.

            ggplot(mtcars, aes(wt, mpg)) +    geom_point() +   # Facet rows by am using formula notation   facet_grid(am ~ .)          

            ggplot(mtcars, aes(wt, mpg)) +    geom_point() +   # Facet columns by cyl using formula notation   facet_grid(. ~ cyl)          

            ggplot(mtcars, aes(wt, mpg)) +    geom_point() +   # Facet rows past am and columns past cyl using formula annotation   facet_grid(am ~ cyl)          

Fortunate formula formulation! While many ggplots still utilize the traditional formula notation, using vars() is now preferred.

Facet labels and gild

Labeling facets

If your factor levels are not articulate, your facet labels may exist confusing. Yous can assign proper labels in your original data before plotting (see next practice), or you can use the labeller argument in the facet layer.

The default value is

  • label_value: Default, displays only the value

Common alternatives are:

  • label_both: Displays both the value and the variable name
  • label_context: Displays simply the values or both the values and variables depending on whether multiple factors are faceted
            # Plot wt by mpg ggplot(mtcars, aes(wt, mpg)) +   geom_point() +   # The default is label_value   facet_grid(cols = vars(cyl))          

            # Plot wt by mpg ggplot(mtcars, aes(wt, mpg)) +   geom_point() +   # Displaying both the values and the variables   facet_grid(cols = vars(cyl), labeller = label_both)          

            # Plot wt by mpg ggplot(mtcars, aes(wt, mpg)) +   geom_point() +   # Label context   facet_grid(cols = vars(cyl), labeller = label_context)          

            # Plot wt by mpg ggplot(mtcars, aes(wt, mpg)) +   geom_point() +   # Two variables   facet_grid(cols = vars(vs, cyl), labeller = label_context)          

Lovely labels! Make sure there is no ambiguity in interpreting plots by using proper labels.

Setting order

If yous want to change the order of your facets, it's best to properly define your factor variables before plotting.

Let'due south come across this in action with the mtcars transmission variable am. In this instance, 0 = "automatic" and i = "transmission".

Here, we'll brand am a factor variable and relabel the numbers to proper names. The default lodge is alphabetical. To rearrange them we'll call fct_rev() from the forcats package to contrary the order.

            # Make cistron, set proper labels explictly mtcars$fam <- factor(mtcars$am, labels = c(`0` = "automatic",                                            `one` = "manual"))  # Default order is alphabetical ggplot(mtcars, aes(wt, mpg)) +   geom_point() +   facet_grid(cols = vars(fam))          

            # Make cistron, set proper labels explictly, and # manually set up the label order mtcars$fam <- factor(mtcars$am,                      levels = c(one, 0),                      labels = c("manual", "automated"))  # View again ggplot(mtcars, aes(wt, mpg)) +   geom_point() +   facet_grid(cols = vars(fam))          

Outstanding ordering! Arrange your facets in an intuitive order for your data.

Facet plotting spaces

Variable plotting spaces I: continuous variables

By default every facet of a plot has the same axes. If the data ranges vary wildly between facets, it can be clearer if each facet has its own scale. This is achieved with the scales argument to facet_grid().

  • "fixed" (default): axes are shared betwixt facets.
  • "free": each facet has its ain axes.
  • "free_x": each facet has its own ten-axis, simply the y-axis is shared.
  • "free_y": each facet has its own y-centrality, but the x-axis is shared.

When faceting by columns, "free_y" has no consequence, but we tin adapt the ten-axis. In dissimilarity, when faceting by rows, "free_x" has no effect, but we can adjust the y-axis.

            ggplot(mtcars, aes(wt, mpg)) +   geom_point() +    # Facet columns by cyl    facet_grid(cols = vars(cyl))          

            ggplot(mtcars, aes(wt, mpg)) +   geom_point() +    # Update the faceting to free the x-axis scales   facet_grid(cols = vars(cyl),             scales = "free_x")          

            ggplot(mtcars, aes(wt, mpg)) +   geom_point() +    # Bandy cols for rows; free the y-axis scales   facet_grid(rows = vars(cyl), scales = "free_y")          

Freedom! Shared scales brand it like shooting fish in a barrel to compare betwixt facets, merely can exist confusing if the data ranges are very dissimilar. In that case, used complimentary scales.

Variable plotting spaces II: categorical variables

When you take a categorical variable with many levels which are not all present in each sub-group of some other variable, it'south usually desirable to drop the unused levels.

By default, each facet of a plot is the same size. This behavior tin can be inverse with the spaces statement, which works in the aforementioned way as scales: "free_x" allows different sized facets on the x-axis, "free_y", allows different sized facets on the y-axis, "complimentary" allows different sizes in both directions.

            ggplot(mtcars, aes(ten = mpg, y = machine, color = fam)) +   geom_point() +   # Facet rows by gear   facet_grid(rows=vars(gear))          

            ggplot(mtcars, aes(ten = mpg, y = auto, color = fam)) +   geom_point() +   # Free the y scales and space   facet_grid(rows = vars(gear),                            scales = "free_y",              space = "free_y")          

Super spaces! Freeing the y-calibration to remove blank lines helps focus attention on the actual data present.

Facet wrap & margins

Using facet_wrap()

Use cases:

  1. When you want both x and y axes to be gratis on every individual plot
  • i.e. non just per row or cavalcade every bit per facet_grid()
  1. When your categorical (factor) variable has many groups (levels)
  • i.e. too many sub plots for column or row-wise faceting
  • a more typical scenario

Wrapping for many levels

facet_grid() is fantastic for chiselled variables with a pocket-size number of levels. Although it is possible to facet variables with many levels, the resulting plot volition be very wide or very tall, which tin can make it hard to view.

The solution is to use facet_wrap() which separates levels forth one axis only wraps all the subsets across a given number of rows or columns.

For this plot, we'll use the Vocab dataset that we've already seen. The base layer is provided.

Since we have many years, information technology doesn't make sense to utilise facet_grid(), then let's try facet_wrap() instead.

            ggplot(Vocab, aes(x = educational activity, y = vocabulary)) +   stat_smooth(method = "lm", se = False) +   # Create facets, wrapping by year, using vars()   facet_wrap(vars(year))          

            ggplot(Vocab, aes(x = education, y = vocabulary)) +   stat_smooth(method = "lm", se = FALSE) +   # Create facets, wrapping by year, using a formula   facet_wrap(~ year)          

            ggplot(Vocab, aes(10 = education, y = vocabulary)) +   stat_smooth(method = "lm", se = FALSE) +   # Update the facet layout, using eleven columns   facet_wrap(~ year, ncol = 11)          

Margin plots

Facets are great for seeing subsets in a variable, but sometimes you desire to see both those subsets and all values in a variable.

Hither, the margins argument to facet_grid() is your friend.

  • Simulated(default): no margins.
  • Truthful: add margins to every variable existence faceted past.
  • c("variable1", "variable2"): only add margins to the variables listed.

To make it easier to follow the facets, we've created 2 factor variables with proper labels — fam for the transmission type, and fvs for the engine type, respectively.

            library(forcats) # Make factor, prepare proper labels explictly mtcars$fam <- cistron(mtcars$am, labels = c('0' = "automatic", 'i' = "manual")) mtcars$fvs <- gene(mtcars$vs, labels = c('0' = "V-shaped", '1' = "straight"))  ggplot(mtcars, aes(x = wt, y = mpg)) +    geom_point() +   # Facet rows past fvs and cols by fam   facet_grid(rows = vars(fvs, fam), col = vars(gear))          

            ggplot(mtcars, aes(x = wt, y = mpg)) +    geom_point() +   # Update the facets to add together margins   facet_grid(rows = vars(fvs, fam), cols = vars(gear), margins = TRUE)          

            ggplot(mtcars, aes(x = wt, y = mpg)) +    geom_point() +   # Update the facets to only show margins on fam   facet_grid(rows = vars(fvs, fam), cols = vars(gear), margins = "fam")          

            ggplot(mtcars, aes(x = wt, y = mpg)) +    geom_point() +   # Update the facets to simply testify margins on gear and fvs   facet_grid(rows = vars(fvs, fam), cols = vars(gear), margins = c("gear", "fvs"))          

Magic margins! It tin be really helpful to evidence the full margin plots!

All-time practices: bar plots

In this chapter

  • mutual pitfalls in data viz
  • best way to represent data
    • for constructive explanatory (advice), and
    • for effective exploratory (investigation) plots

bar plots

  • two types
    • absolute values
    • distributions

Bar plots: dynamite plots

In the video nosotros saw many reasons why "dynamite plots" (bar plots with mistake confined) are not well suited for their intended purpose of depicting distributions. If y'all really want error bars on bar plots, you tin of course go them, but you lot'll need to set the positions manually. A signal geom will typically serve yous much better.

Nevertheless, y'all should know how to handle these kinds of plots, and then allow's requite information technology a endeavor.

            # Plot wt vs. fcyl ggplot(mtcars, aes(x = fcyl, y = wt)) +   # Add a bar summary stat of ways, colored skyblue   stat_summary(fun.y = mean, geom = "bar", fill = "skyblue") +   # Add together an errorbar summary stat std divergence limits   stat_summary(fun.data = mean_sdl, fun.args = listing(mult = 1), geom = "errorbar", width = 0.1)          
            ## Warning: `fun.y` is deprecated. Use `fun` instead.          

Excellent errors! Recall, we can specify any office in fun.data or fun.y and we tin besides specify any geom, as long as it's appropriate to the data type.

Bar plots: position dodging

In the previous practice we used the mtcars dataset to draw a dynamite plot most the weight of the cars per cylinder blazon.

In this exercise we volition add a distinction between transmission blazon, fam, for the dynamite plots and explore position dodging (where bars are side-past-side).

            # Update the aesthetics to colour and fill by fam ggplot(mtcars, aes(x = fcyl, y = wt, colour = fam, fill = fam)) +   stat_summary(fun.y = hateful, geom = "bar") +   stat_summary(fun.data = mean_sdl, fun.args = listing(mult = 1), geom = "errorbar", width = 0.1)          
            ## Warning: `fun.y` is deprecated. Employ `fun` instead.          

            # For each summary stat, set the position to contrivance ggplot(mtcars, aes(ten = fcyl, y = wt, colour = fam, fill = fam)) +   stat_summary(fun.y = mean, geom = "bar", position = "dodge", alpha = 0.5) +   stat_summary(fun.data = mean_sdl, fun.args = listing(mult = 1), geom = "errorbar", position = "dodge", width = 0.1)          
            ## Warning: `fun.y` is deprecated. Use `fun` instead.          

            # Define a dodge position object with width 0.9 posn_d <- position_dodge(width = 0.9)  # For each summary stat, update the position to posn_d ggplot(mtcars, aes(x = fcyl, y = wt, colour = fam, fill = fam)) +   stat_summary(fun.y = mean, geom = "bar", position = posn_d, alpha = 0.5) +   stat_summary(fun.information = mean_sdl, fun.args = list(mult = one), width = 0.1, position = posn_d, geom = "errorbar")          
            ## Alarm: `fun.y` is deprecated. Utilize `fun` instead.          

Bar plots two.0! Slightly overlapping bar plots are common in the pop press and add a bit of style to your data viz.

Bar plots: Using aggregated information

If it is appropriate to use bar plots (run into the video!), then it nice to give an impression of the number of values in each grouping.

stat_summary() doesn't continue runway of the count. stat_sum() does (that's the whole signal), just it's difficult to access. Information technology's more straightforward to calculate exactly what nosotros want to plot ourselves.

Here, nosotros've created a summary information frame chosen mtcars_by_cyl which contains the average (mean_wt), standard deviations (sd_wt) and count (n_wt) of auto weights, for each cylinder group, cyl. Information technology as well contains the proportion (prop) of each cylinder represented in the entire dataset. Utilize the console to familiarize yourself with the mtcars_by_cyl information frame.

            library(dplyr) mtcars_by_cyl <- mtcars %>%                   select(wt, cyl) %>%                   group_by(cyl) %>%                   summarize(mean_wt = round(mean(wt), 2), sd_wt=round(sd(wt), 3), n_wt=n()) %>%                   mutate(prop=round(n_wt/sum(n_wt), iii))          
            ## `summarise()` ungrouping output (override with `.groups` argument)          
            mtcars_by_cyl          
            ## # A tibble: 3 10 v ##     cyl mean_wt sd_wt  n_wt  prop ##   <dbl>   <dbl> <dbl> <int> <dbl> ## 1     4    two.29 0.570    11 0.344 ## 2     half dozen    three.12 0.356     7 0.219 ## 3     viii    4    0.759    fourteen 0.438          
            ggplot(mtcars_by_cyl, aes(x = cyl, y = mean_wt)) +   # Bandy geom_bar() for geom_col()   geom_bar(stat = "identity", fill = "skyblue")          

            ggplot(mtcars_by_cyl, aes(x = cyl, y = mean_wt)) +   # Swap geom_bar() for geom_col()   geom_col(fill = "skyblue")          

            ggplot(mtcars_by_cyl, aes(ten = cyl, y = mean_wt)) +   # Prepare the width artful to prop   geom_col(aes(width = prop), fill = "skyblue")          
            ## Warning: Ignoring unknown aesthetics: width          

            ggplot(mtcars_by_cyl, aes(x = cyl, y = mean_wt)) +   geom_col(aes(width = prop), fill = "skyblue") +   # Add an errorbar layer   geom_errorbar(     # ... at mean weight plus or minus 1 std dev     aes(ymin=mean_wt-sd_wt,         ymax=mean_wt+sd_wt),     # with width 0.1     width=0.1)          
            ## Warning: Ignoring unknown aesthetics: width          

Awesome Aggregrates! This is a good showtime, but information technology's difficult to adjust the spacing between the confined.

Heatmaps use case scenario

  • color on a continuous scale is problematic
    • color depends on context

  • precision of perception is increased, while speed of perceiving trends is decreased

  • precision of perception is decreased, while speed of perceiving trends is increased

Heat maps

Since oestrus maps encode colour on a continuous calibration, they are difficult to accurately decode, a topic we discussed in the first course. Hence, estrus maps are near useful if you have a small number of boxes and/or a clear design that allows y'all to overcome decoding difficulties.

To produce them, map two chiselled variables onto the x and y aesthetics, along with a continuous variable onto fill. The geom_tile() layer adds the boxes.

We'll produce the heat map we saw in the video (in the viewer) with the born barley dataset. The barley dataset is in the lattice package and has already been loaded for y'all. Utilise str() to explore the structure.

            library(lattice) library(RColorBrewer) str(barley)          
            ## 'data.frame':    120 obs. of  4 variables: ##  $ yield  : num  27 48.9 27.4 39.nine 33 ... ##  $ variety: Gene w/ 10 levels "Svansota","No. 462",..: 3 iii 3 3 3 three vii 7 7 7 ... ##  $ twelvemonth   : Factor w/ 2 levels "1932","1931": 2 2 2 two ii 2 2 2 two 2 ... ##  $ site   : Cistron west/ 6 levels "1000 Rapids",..: iii 6 4 five 1 2 3 6 iv 5 ...          
            # Using barley, plot variety vs. year, filled by yield ggplot(barley, aes(x=year, y=variety, fill=yield)) +   # Add a tile geom   geom_tile()          

            # Previously defined ggplot(barley, aes(ten = year, y = diverseness, fill up = yield)) +   geom_tile() +    # Facet, wrapping by site, with ane column   facet_wrap(facets = vars(site), ncol = one) +   # Add a fill calibration using an 2-colour gradient   scale_fill_gradient(depression = "white", loftier = "red")          

            # A palette of 9 reds red_brewer_palette <- brewer.pal(9, "Reds")  # Update the plot ggplot(barley, aes(x = year, y = diverseness, fill = yield)) +   geom_tile() +    facet_wrap(facets = vars(site), ncol = i) +   # Update scale to utilise n-colors from red_brewer_palette   scale_fill_gradientn(colors=red_brewer_palette)          

Expert job! You can continue past using breaks, limits and labels to modify the fill calibration and update the theme, just this is a pretty good start.

Heat map alternatives

In that location are several alternatives to oestrus maps. The best option actually depends on the information and the story you want to tell with this data. If there is a fourth dimension component, the nigh obvious choice is a line plot.

            # The heat map we want to supplant # Don't remove, information technology's here to aid you! ggplot(barley, aes(x = year, y = diversity, fill up = yield)) +   geom_tile() +   facet_wrap( ~ site, ncol = i) +   scale_fill_gradientn(colors = brewer.pal(nine, "Reds"))          

            # Using barley, plot yield vs. year, colored and grouped by variety ggplot(barley, aes(x=yr, y=yield, color=variety, group=variety)) +   # Add a line layer   geom_line() +   # Facet, wrapping by site, with i row   facet_wrap( ~ site, nrow = ane)          

            # Using barely, plot yield vs. year, colored, grouped, and filled by site ggplot(barley, aes(x = twelvemonth, y = yield, colour = site, group = site, fill up = site)) +   # Add a line summary stat aggregated by mean   stat_summary(fun.y = hateful, geom = "line") +   # Add a ribbon summary stat with 10% opacity, no color   stat_summary(fun.information = mean_sdl, fun.args = listing(mult = 1), geom = "ribbon", alpha = 0.1, colour = NA)          
            ## Warning: `fun.y` is deprecated. Utilize `fun` instead.          

Skilful task! Whenever you lot see a estrus map, inquire yourself it it's actually necessary. Many people utilize them because they look fancy and complicated - signs of poor communication skills.

When expert data makes bad plots

**Bad plots: fashion

  • color
    • not color-blind-friendly (e.g. primarily carmine and green)
    • wrong palette for information type (remember sequential, qualitative, and divergent)
    • indistinguishable groups (i.e. colors are too similar)
    • ugly (high saturation primary colors)
  • text
    • illegible (e.g. too small, poor resolution)
    • non-descriptive (e.g. "length" – of what? which units?)
    • missing
    • inappropriate (e.g. comic sans)

Guidelines not rules

  • employ your mutual sense
    • is there annihilation on my plot that obscures a articulate reading of the data or the take-habitation bulletin?

Suppression of the origin

Suppression of the origin refers to not showing 0 on a continuous calibration. It is inappropriate to suppress the origin when the scale has a natural zero, like elevation or distance - but it'due south not strictly necessary and not always appropriate.

Colour blindness

Red-Green color incomprehension is surprisingly prevalent, which means that function of your audience will not be able to fix your plot if yous are relying on colour aesthetics.

It would be appropriate to use ruby-red and dark-green in a plot when red and dark-green have different intensities (e.chiliad. light ruby-red and dark green).

If you lot really want to use red and green, this is a way to make them accessible to colour blind people, since they will notwithstanding be able to distinguish intensity. It's not as salient equally hue, but information technology still works.

Typical issues

When you first encounter a data visualization, either from yourself or a colleague, you always desire to critically ask if it's obscuring the data in any way.

Let'due south have a look at the steps we could take to produce and ameliorate the plot in the view.

The data comes from an experiment where the outcome of ii different types of vitamin C sources, orange juice or ascorbic acid, were tested on the growth of the odontoblasts (cells responsible for tooth growth) in 60 guinea pigs.

The information is stored in the TG information frame, which contains three variables: dose, len, and supp.

            library(datasets) TG <- ToothGrowth  # Initial plot growth_by_dose <- ggplot(TG, aes(dose, len, color = supp)) +   stat_summary(fun.information = mean_sdl,                fun.args = list(mult = one),                position = position_dodge(0.1)) +   theme_gray(3)  # View plot growth_by_dose          

            # Modify theme growth_by_dose <- ggplot(TG, aes(dose, len, colour = supp)) +   stat_summary(fun.information = mean_sdl,                fun.args = listing(mult = 1),                position = position_dodge(0.1)) +   theme_classic()  # View plot growth_by_dose          

            # Alter type TG$dose <- every bit.numeric(as.character(TG$dose))  # Plot growth_by_dose <- ggplot(TG, aes(dose, len, color = supp)) +   stat_summary(fun.data = mean_sdl,                fun.args = list(mult = 1),                position = position_dodge(0.2)) +   theme_classic()  # View plot growth_by_dose          

            # Change type TG$dose <- every bit.numeric(as.grapheme(TG$dose))  # Plot growth_by_dose <- ggplot(TG, aes(dose, len, color = supp)) +   stat_summary(fun.data = mean_sdl,                fun.args = list(mult = one),                position = position_dodge(0.2)) +   # Utilise the right geometry   stat_summary(fun.y = mean,                geom = "line",                position = position_dodge(0.1)) +   theme_classic()          
            ## Alert: `fun.y` is deprecated. Apply `fun` instead.          
            # View plot growth_by_dose          

            # Change blazon TG$dose <- as.numeric(equally.character(TG$dose))  # Plot growth_by_dose <- ggplot(TG, aes(dose, len, color = supp)) +   stat_summary(fun.information = mean_sdl,                fun.args = list(mult = ane),                position = position_dodge(0.2)) +   stat_summary(fun.y = mean,                geom = "line",                position = position_dodge(0.1)) +   theme_classic() +   # Adjust labels and colors:   labs(x = "Dose (mg/day)", y = "Odontoblasts length (mean, standard divergence)", colour = "Supplement") +   scale_color_brewer(palette = "Set1", labels = c("Orangish juice", "Ascorbic acid")) +   scale_y_continuous(limits = c(0,35), breaks = seq(0, 35, v), expand = c(0,0))          
            ## Warning: `fun.y` is deprecated. Apply `fun` instead.          
            # View plot growth_by_dose          

milonesightle.blogspot.com

Source: https://rstudio-pubs-static.s3.amazonaws.com/702476_e817744f1f5047688be45193e9d5794e.html

0 Response to "Familiarize Yourself Again With the Mtcars Dataset Using Str()."

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel