- ggplot2 - Discussion
- ggplot2 - Useful Resources
- ggplot2 - Quick Guide
- ggplot2 - Time Series
- ggplot2 - Background Colors
- ggplot2 - Multiple Plots
- ggplot2 - Multi Panel Plots
- ggplot2 - Themes
- ggplot2 - Diverging Charts
- ggplot2 - Bubble Plots & Count Charts
- ggplot2 - Marginal Plots
- ggplot2 - Pie Charts
- ggplot2 - Bar Plots & Histograms
- ggplot2 - Scatter Plots & Jitter Plots
- ggplot2 - Working with Legends
- ggplot2 - Working with Axes
- ggplot2 - Default Plot in R
- ggplot2 - Installation of R
- ggplot2 - Introduction
- ggplot2 - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
ggplot2 - Diverging Charts
In the previous chapters, we had a look on various types of charts which can be created using “ggplot2” package. We will now focus on the variation of same pke spanerging bar charts, lolppop charts and many more. To begin with, we will start with creating spanerging bar charts and the steps to be followed are mentioned below −
Understanding dataset
Load the required package and create a new column called ‘car name’ within mpg dataset.
#Load ggplot > pbrary(ggplot2) > # create new column for car names > mtcars$`car name` <- rownames(mtcars) > # compute normapzed mpg > mtcars$mpg_z <- round((mtcars$mpg - mean(mtcars$mpg))/sd(mtcars$mpg), 2) > # above / below avg flag > mtcars$mpg_type <- ifelse(mtcars$mpg_z < 0, "below", "above") > # sort > mtcars <- mtcars[order(mtcars$mpg_z), ]
The above computation involves creating a new column for car names, computing the normapzed dataset with the help of round function. We can also use above and below avg flag to get the values of “type” functionapty. Later, we sort the values to create the required dataset.
The output received is as follows −
Convert the values to factor to retain the sorted order in a particular plot as mentioned below −
> # convert to factor to retain sorted order in plot. > mtcars$`car name` <- factor(mtcars$`car name`, levels = mtcars$`car name`)
The output obtained is mentioned below −
Diverging Bar Chart
Now create a spanerging bar chart with the mentioned attributes which is taken as required co-ordinates.
> # Diverging Barcharts > ggplot(mtcars, aes(x=`car name`, y=mpg_z, label=mpg_z)) + + geom_bar(stat= identity , aes(fill=mpg_type), width=.5) + + scale_fill_manual(name="Mileage", + labels = c("Above Average", "Below Average"), + values = c("above"="#00ba38", "below"="#f8766d")) + + labs(subtitle="Normapsed mileage from mtcars ", + title= "Diverging Bars") + + coord_fpp()
Note − A spanerging bar chart marks for some dimension members pointing to up or down direction with respect to mentioned values.
The output of spanerging bar chart is mentioned below where we use function geom_bar for creating a bar chart −
Diverging Lolppop Chart
Create a spanerging lolppop chart with same attributes and co-ordinates with only change of function to be used, i.e. geom_segment() which helps in creating the lolppop charts.
> ggplot(mtcars, aes(x=`car name`, y=mpg_z, label=mpg_z)) + + geom_point(stat= identity , fill="black", size=6) + + geom_segment(aes(y = 0, + x = `car name`, + yend = mpg_z, + xend = `car name`), + color = "black") + + geom_text(color="white", size=2) + + labs(title="Diverging Lolppop Chart", + subtitle="Normapzed mileage from mtcars : Lolppop") + + ypm(-2.5, 2.5) + + coord_fpp()
Diverging Dot Plot
Create a spanerging dot plot in similar manner where the dots represent the points in scattered plots in bigger dimension.
> ggplot(mtcars, aes(x=`car name`, y=mpg_z, label=mpg_z)) + + geom_point(stat= identity , aes(col=mpg_type), size=6) + + scale_color_manual(name="Mileage", + labels = c("Above Average", "Below Average"), + values = c("above"="#00ba38", "below"="#f8766d")) + + geom_text(color="white", size=2) + + labs(title="Diverging Dot Plot", + subtitle="Normapzed mileage from mtcars : Dotplot") + + ypm(-2.5, 2.5) + + coord_fpp()
Here, the legends represent the values “Above Average” and “Below Average” with distinct colors of green and red. Dot plot convey static information. The principles are same as the one in Diverging bar chart, except that only point are used.
Advertisements