R Data Visualization Recipes by Vitor Bianchi Lanzetta

R Data Visualization Recipes by Vitor Bianchi Lanzetta

Author:Vitor Bianchi Lanzetta [Vitor Bianchi Lanzetta]
Language: eng
Format: epub
Tags: COM089000 - COMPUTERS / Data Visualization, COM062000 - COMPUTERS / Data Modeling and Design, COM018000 - COMPUTERS / Data Processing
Publisher: Packt
Published: 2017-11-21T07:07:49+00:00


How it works...

Step 1 in takes care of plotting stacked proportional bars using ggplot2. It demands only little coding and most of it looks like Recipe Creating simple stacked bars, except for the position argument in the geom_bar() function. This argument is now inputted with the string 'fill'. A great deal of geom_*() functions do accept 'fill' as an input to position argument. This input usually makes the geometry about proportions.

Argument position makes very easy to ask for proportions using ggplot2. Improvement can be delivered by Hadley's scale package. If you have already got it installed, there is no need to restart your R Session, but if that's not the case, do so and run the following code to improve the y-axis labels:

> if( !require(scales)){ install.packages('scales')}

> library(ggplot2)

> gg2_sal <- ggplot( data = car::Salaries, aes(x = rank))

> gg2_sal + geom_bar(position = 'fill', aes(fill = sex)) +

> scale_y_continuous(labels = scales::percent_format())

The resulting plot displays the y-axis labels in the percentage format. Changes like that are very useful, keep scales package nearby. Check the result in the following image (Figure 5.4):



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.