English 中文(简体)
ggplot2 - Diverging Charts
  • 时间:2024-12-22

ggplot2 - Diverging Charts


Previous Page Next Page  

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 −

Diverging Charts

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 Charts

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 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 Lolppop Chart

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()
Diverging Dot Plot

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