General Ggplot2 Tips

 

ggplot2 is an R library for creating graphics, based on the The Grammar of Graphics. It has been created by Hadley Wickham and is part of the tidyverse revolution. It can greatly improve the quality and aesthetics of your graphics, and

 

 will make you much more efficient in creating them. ggplot2 allows you to build almost any type of graphic. The R graph gallery focus on its utilisation, so almost every section there starts with ggplot2 examples.

 

This page is dedicated to general ggplot2 tips that you can apply to any chart, like customising a title, adding annotation, or using faceting. If you’re new to ggplot2, a good starting point is probably this online course.

 

Sponsors


 

Customization with theme()


With ggplot2, the appearance of the plot is controlled using the theme function. It allows you to control each of the elements of a graph: axis, background, legend, strip. Note that several built-in themes are available as well.

 

Marginal Plot


Marginal plots are not natively supported by ggplot2, but their realisation is straightforward thanks to the ggExtra library as illustrated in graph #277.

 

Annotations


Once you’ve realised your graphic, you probably want to annotate it to highlight a specific part of it. The graph #233 will teach you how to add lines, text, rectangles, arrows, and more.

 

Text Labels


You can add text to your graphic using the geom_text or geom_label functions. Note the existence of the ggrepel extension that allows to avoid overlapping of labels.

 

Title


 

Faceting


There are several ways to split your graphic window in several parts with ggplot2. If the split is made according to the value of a specific variable, you should use the facet_wrap or facet_grid functions as proposed below in graph #233:

 

However, if you want to arrange several graphics that are completely different in the same window, you will have to use the gridExtra library as follows:

 

Managing colors


Reordering


When working with discrete variables (also called factors), a common problem is how to manage the order of entities on the plot. Post #267 is dedicated to reordering, and here are a couple of examples showing of to deal with it.

 

From ggplot to interactivity.


Another awesome feature of ggplot2 is its link with the plotly library. If you know how to make a ggplot2 graphic, you are 10 seconds close to rendering an interactive version! Just call the ggplotly function, and you’re done. Visit the interactive graphic section of the gallery for more.

 

 

 

 

 

Hover, select a zone, click the legend, export…

 

Ggplot2 for maps


Ggplot2 is also a very good choice for maps. It provides specific functions to draw geographical shapes. It will allow you to plot any type of map (chloropleth, bubble, connection, hexbin or cartogram.)

 

 

 

 

Ggplot2 extension


If something is not doable with ggplot2… There is an extension to do it! Visit this dedicated webpage to see all of them. Here is an overview of the main ones.

 

 

A selection of ggplot2 graphics


Ggplot is skilled to realise any type of graphic. Here is a curated selection from the gallery. If you are looking for a specific type of chart, go to the welcome page of the gallery, and select the chart section that interests you; they all give the ggplot2 way to make it!

 

 

 

DataArt


Ggplot2 is frequently used by Data Artist. Have a look to the data art section of the gallery for stunning artwork, from generative art to BB-8 droids!

 


Looking for somethig else ? Try a search !

 

6
Leave a Reply

avatar
4 Comment threads
2 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
6 Comment authors
M. JudiHoltzyaeg szbaroewdon d fonSabarikannan Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
M. Judi
Guest
M. Judi

Hello, is there a plot that will show a line graph with different line thickness based on a measure value? Something line this: https://www.dropbox.com/s/kbn378xakkxpq0z/IMG_2100.jpg?dl=0

don d fon
Guest
don d fon

I really, really enjoyed these plots. Mind if I save a few?

yaeg szbaroew
Guest
yaeg szbaroew

Save them, they’re all yours, my man. haha.

Sabarikannan
Guest
Sabarikannan

More Exciting plot Samples

Anonymous
Guest
Anonymous

Excellent Plot Pallate