Understanding GGPLOT function

Milind DESAI

October 19, 2018

    Scatter Plots using Geom_point
  • Simple plot showing city Mileage Vs Displacement by Model

    p <- ggplot(data=mpg, aes(x=displ, y=cty, color=model))
    p + geom_point(size=4)
  • Simple plot showing city Mileage Vs Displacement by Fuel type

    p1 <- ggplot(data=mpg, aes(x=displ, y=cty, color=fl))
    p1 + geom_point(size=4)
  • Simple plot showing city Mileage Vs Displacement by class of vehicle

    p2 <- ggplot(data=mpg, aes(x=displ, y=cty, color=class))
    p2 + geom_point(size=4)

  • Adding a geom Layer

    p <- ggplot(data=mpg, aes(x=displ, y=cty, color=class))
    p+geom_blank()

    p+geom_point()

    p+geom_jitter()

    p+geom_count()
  • Connecting points in the plot

    p+geom_line()

    p+geom_step()

    p+geom_path()
  • Displaying data and statistical summary

    p <- ggplot(data=mpg, aes(x=displ, y=cty))
    p+geom_point() + geom_smooth() # Smooth fit line
    ## `geom_smooth()` using method = 'loess'

    p+geom_point() + geom_smooth(method = "lm") # Regression line

  • Displaying Bar graphs

    p<- ggplot(data=mpg, aes(x=class))
    p+geom_bar()

    p<-ggplot(data=mpg,aes(x=model))
    p+geom_bar(color="blue", fill="light blue")

  • Constructing box plots

    p <- ggplot(data=mpg, aes(x=class, y=cty))
    p+geom_boxplot()

    p+geom_violin()
  • Density plot using ggplot

    p <- ggplot(data=mpg, aes(x=cty))
    p+geom_density(fill="light blue")

  • Histogram plot using ggplot

    p <- ggplot(data=mpg, aes(x=cty))
    p+geom_histogram(binwidth = 1.8, color="blue", fill="light blue")
  • Add or remove aesthetic mapping

    • Add aesthetic mapping

      p<-ggplot(data=mpg,aes(x=displ, y=cty, color=fl))
      p + geom_point(aes(shape=fl)) + geom_smooth(method = "lm", se=FALSE)
    • Remove aesthetic mapping

      p<-ggplot(data=mpg,aes(x=displ, y=cty, color=fl))
      p + geom_point(aes(color=NULL)) + geom_smooth(method = "lm", se=FALSE)
  • parameter setting Vs Aesthetic mapping

    p<-ggplot(data=mpg,aes(x=displ, y=cty))
    p + geom_point(color="red") # color Parameter set to red

    p + geom_point(aes(color=fl)) # Aesthetic mapping
  • Adjusting the positions of Bars in the bar plot

    p <- ggplot(data=mpg, aes(x=cty, fill=class))
    p + geom_bar()

    p + geom_bar(position="stack")

    p + geom_bar(position="dodge")

    p + geom_bar(position="fill")

  • Adjusting the width of the Bars in the bar plot

    p <- ggplot(data=mpg, aes(x=cty))
    p + geom_bar()

    p + geom_bar(width = 0.9) # default

    p + geom_bar(width = 0.95)

    p + geom_bar(width = 0.5)
  • Adjusting the position of points

    p <- ggplot(data=mpg, aes(x=displ, y=cty))
    p + geom_point()

    p + geom_jitter()            
  • Adjusting symbol size shape jitter and transparency

    p <- ggplot(data=mpg, aes(x=displ, y=cty))
    p + geom_point(size = 3, alpha=1/2, color="red")

    p + geom_point(size = 3, alpha=1/10, color="red")

    p + geom_jitter(size = 4, alpha=1/8, color="red")  

    p + geom_point(shape=2, size = 3, alpha=1/2, color="red")

    p + geom_point(shape=4, size = 2, alpha=1/5, color="red")
  • Changing the coordinate system

    p <- ggplot(data=mpg, aes(x=factor(1), fill=class))
    p + geom_bar()

    # Polar coordinate system
    p + geom_bar() + coord_polar(theta = "y")

    # flipping coordinates
    p + geom_bar() + coord_flip()

    p + geom_bar() + coord_polar(theta = "y", direction = -1)
  • Each plot layer may contain data from different dataframes

    mpg_midsize <- subset(mpg, class == "midsize")
    mpg_pickup <- subset(mpg, class == "pickup")
    mpg_suv <- subset(mpg, class == "suv")
    
    # Combine plots of two data frames
    p <- ggplot(data = mpg_midsize, aes(x=displ, y=cty))
    p + geom_jitter(color="blue") + geom_jitter(data = mpg_suv, color="red") + geom_jitter(data = mpg_pickup, color="green")

    mpg_midsize_suv <- rbind(mpg_midsize,mpg_suv)
    
    # Plotting combined data 
    p <- ggplot(data = mpg_midsize_suv, aes(x=displ, y=cty, color=class))
    p + geom_jitter()
  • Adding Labels to your plot: Labeling points on scatter plot

    p <- ggplot(data=mpg_midsize, aes(x=displ,y=cty))
    p + geom_point(size=3) + geom_text(aes(label=model), nudge_y = 0.4, nudge_x =0.12 , size=3) + xlim(2,9) + ylim(6, 36)
    ## Warning: Removed 2 rows containing missing values (geom_point).
    ## Warning: Removed 2 rows containing missing values (geom_text).

    p + geom_point(size=3) + geom_label(aes(label=model), nudge_x = 0.12,nudge_y = 0.3)
  • Adding title to the plot

     p + geom_point(size=3) + geom_label(aes(label=model), nudge_x = 0.12,nudge_y = 0.3) + ggtitle("City Mileage Vs Displacement")

    # Non overlapping labels
    p <- ggplot(data=mpg_midsize, aes(x=displ,y=cty))
    p + geom_point(size=3) + geom_label(aes(label=model), label.size = 0.2) # need to check !
  • Using Scale functions

    p <- ggplot(data=mpg, aes(x=cty, fill=class))
    p + geom_bar(color="black") + scale_fill_discrete()

    p + geom_bar(color="black") + scale_fill_brewer()
  • choosing a color palette

    library(RColorBrewer)
    display.brewer.all()

    p <- ggplot(data=mpg, aes(x=cty, fill=class))
    p + geom_bar(color="black") + scale_fill_brewer(palette = "Accent")

    p + geom_bar(color="black") + scale_fill_brewer(palette = "Spectral")
  • Position Scales

    p <- ggplot(data=mpg, aes(x=displ,y=cty))
    p + geom_jitter()

    # reversing the y scale
    p + geom_jitter() + scale_y_reverse()

    # changing the y-axis scale to log10
    p + geom_jitter() + scale_y_log10()

    # setting y axis labels
    p + geom_jitter() + scale_y_log10(breaks=c(0.5, 8, 20), labels=c(0.5,8,20))
  • Theme: Titles, Tick Marks, and Tick Labels

    p <- ggplot(data=mpg, aes(x=displ,y=cty))
    q <- p + geom_jitter() + ggtitle("City Mileage Vs Displacement") + xlab("Engine Displacement") + ylab("City Mileage")+scale_x_continuous(breaks = seq(1.2,7, by=0.25)) 
    q + theme(plot.title = element_text(colour = "blue", size = 20, hjust=0.5), axis.title = element_text(color = "blue" , size = 10))
  • Using Facets

    p <- ggplot(data=mpg, aes(x=displ,y=cty))
    p + geom_jitter() + facet_grid(.~class)