Advanced ggplotting requires many extra libraries. They are loaded as needed.

Start with loading some basic libraries

library(dplyr)
library(ggplot2)

Lollipop chart

Lollipop chart conveys the same information as bar chart (refer Part 1 of tutorial) but in a different pictorial way. Here data is ordered by Mileage

data("mtcars")  # load data
require(forcats)    #for factor re-ordering
## Loading required package: forcats
mpg %>%
    group_by(manufacturer) %>%
    summarise(Mileage=mean(cty)) %>%
    #convert to factor and re-order using forcats:fct_order method at one go
    mutate(Make=fct_reorder(manufacturer, Mileage))%>%
    ggplot(aes(x=Make, y=Mileage)) + 
        geom_point(size=4, color="tomato3") + 
        geom_segment(aes(x=Make, 
                   xend=Make, 
                   y=0, 
                   yend=Mileage)) + 
        #print value for each bar as well
        geom_text(color="purple", size=4, vjust=-0.8, 
                  aes(label=sprintf("%0.1f", round(Mileage, digits = 2))))+
        labs(title="Lollipop Chart",
             subtitle="Make Vs Avg. Mileage") + 
        # change default limit to 30 else geom_text for largest point 24.4              prints halfway only
        ylim(0, 27)+
        theme(axis.text.x = element_text(angle=65, vjust=0.7, color="tomato3"))

Dumbbell Plot

Dumbbell plots are a nice way to visualize relative positions between two points in time and compare distance between two categories. To get the correct ordering of the dumbbells, the Y variable should be a factor.

Lets draw a basic dumbbell plot

library(ggalt)

health <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/health.csv")
health$Area <- factor(health$Area, levels=as.character(health$Area))
ggplot(health, aes(x=pct_2013, xend=pct_2014, y=Area)) + 
        geom_dumbbell()

Lets add a bit of decoration

ggplot(health, aes(x=pct_2013, xend=pct_2014, y=Area)) + 
        #create a thick line between x and xend instead of using defaut 
        #provided by geom_dubbell
        geom_segment(aes(x=pct_2013, 
                         xend=pct_2014, 
                         y=Area, 
                         yend=Area), 
                     color="#b2b2b2", size=1.5)+
        geom_dumbbell(color="light blue", 
                      size_x=3.5, 
                      size_xend = 3.5,
                      #Note: there is no US:'color' for UK:'colour' 
                      # in geom_dumbbel unlike standard geoms in ggplot()
                      colour_x="#edae52", 
                      colour_xend = "#9fb059")+
        labs(x=NULL, y=NULL, 
             title="Dumbbell Chart", 
             subtitle="Pct Change: 2013 vs 2014")+
        geom_text(color="black", size=2, hjust=-0.5,
                  aes(x=pct_2013, label=pct_2013))+
        geom_text(aes(x=pct_2014, label=pct_2014), 
                  color="black", size=2, hjust=1.5)

Treemap

Treemap is a way of displaying hierarchical data by using nested rectangles. We need to install an extra packge treemapify which provides the required base geom_treemap() for treemap plotting.

library(ggplot2) 
library(treemapify)  #for geom_treemap()

Lets start with a basic treemap using a basic example based on various programming language characteristics.

#Read formatted data from web
proglangs <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/proglanguages.csv")

ggplot(proglangs, aes(area=value, fill=parent, subgroup=parent)) + 
    geom_treemap()

Lets add a bit of decoration: text for each group/subgroup, parent group bordering etc.

ggplot(proglangs, aes(area=value, fill=parent, subgroup=parent)) + 
    geom_treemap()+
    #main group bordering
    geom_treemap_subgroup_border()+
    #subgroup heading in white
    geom_treemap_subgroup_text(color="white")+
    #all other group text in black
    geom_treemap_text(aes(label=id), color="black")+
    scale_x_continuous(expand = c(0, 0)) +
    scale_y_continuous(expand = c(0, 0)) +
    scale_fill_brewer(palette = "Dark2")

Bar chart

Bar chart is used for plotting frequency(y axis) of categorical data(x axis)

ggplot(mpg, aes(manufacturer))+
    geom_bar(aes(fill=class), width = 0.5)+
    theme(axis.text.x = element_text(angle=65, vjust=0.6))

Pie chart

We are going to draw a basic pie chart using mpg data. Note: each pie slice shows the count for the given class of vehicle.

Lets first draw a geom_bar chart with stat=“identity” (otherwise known as geom_col).

#Prep data
df <- as.data.frame(table(mpg$class))
colnames(df) <- c("class", "cnt")

ggplot(df, aes(x = "", y=cnt, fill = factor(class))) + 
    geom_bar(width = 1, stat="identity")+
    #coord_polar(theta = "y")+
    #TODO:showing count may be misleading, a better option is to show the 
    #%age for each slice
    geom_text(aes(label=cnt), 
              size=6,
              #ensure text is printed in the middle of slice: vjust=0.5
              position = position_stack(vjust = 0.5))

Lets convert this bar chart to pie chart by adding coord_polar()

require(dplyr)

as.data.frame(table(mpg$class)) %>%
    rename(class=Var1, cnt=Freq) %>%
    ggplot(mapping = aes(x = "", y=cnt, fill = factor(class))) + 
        geom_bar(width = 1, stat="identity")+
        geom_text(aes(label=cnt), 
              size=6,
              #ensure text is printed in the middle of slice: vjust=0.5
              position = position_stack(vjust = 0.5))+
        #theta/angle is proportionl to count i.e. y
        coord_polar(theta = "y")

Now, lets try showing percentage for each pie slice

#Pie chart with %age for eeach slice
mympg <- as.data.frame(table(mpg$class)) %>%
    rename(class=Var1, count=Freq) %>%
    mutate(Perc= 100*count/sum(count)) 

ggplot(mympg, aes(x = "", y=count, fill = factor(class))) + 
    #geom_bar(width = 1, stat="identity")+
    geom_col(width=1)+
    coord_polar(theta = "y")+
    geom_text(aes(label=paste0(sprintf("%0.1f", round(Perc, digits = 2)), "%")), 
              size=4,
              color="white",
              position = position_stack(vjust = 0.5))

A different way to achieve the same using geom_label() instead of geom_text(). This plot looks a bit more elegant.

ggplot(mympg, aes(x = "", y=count, fill = factor(class))) + 
    geom_col(position = 'fill', width=1)+
    coord_polar(theta = "y")+
    geom_label(aes(label = paste0(sprintf("%0.1f", round(Perc, digits = 2)), "%")), 
               position = position_fill(vjust = 0.4))