background preloader

Ggplot

Facebook Twitter

R Graph Catalog. Top 50 ggplot2 Visualizations (With R Code) What type of visualization to use for what sort of problem?

Top 50 ggplot2 Visualizations (With R Code)

This tutorial helps you choose the right type of chart for your specific objectives and how to implement it in R using ggplot2. This is part 3 of a three part tutorial on ggplot2, an aesthetically pleasing (and very popular) graphics framework in R. This tutorial is primarily geared towards those having some basic knowledge of the R programming language and want to make complex and nice looking charts with R ggplot2. Part 1: Introduction to ggplot2, covers the basic knowledge about constructing simple ggplots and modifying the components and aesthetics.Part 2: Customizing the Look and Feel, is about more advanced customization like manipulating legend, annotations, multiplots with faceting and custom layoutsPart 3: Top 50 ggplot2 Visualizations - The Master List, applies what was learnt in part 1 and 2 to construct other types of ggplots such as bar charts, boxplots etc.

Top 50 ggplot2 Visualizations - The Master List 1. 2. 3. 4. Visualizing ggplot2 internals. Ggplot2 extensions. (ggplot2) Exercising with (ggalt) dumbbells. Ggfreehand. Gcc_ggplot. Ggbrush. Population pyramids with ggplot2. Population Pyramids - ggplot2. Making Back-to-Back Histograms. A colleage of mine asked me how to do back to back histograms (instead of on top of each other).

Making Back-to-Back Histograms

I feel as though there should be a function like voilin plot from the vioplot package. Voilin plots are good for displaying data, but the violin must have the left and right (or top and bottom) of the violin to be from the same distribution, and therefore are symmetrical. Many times people want to compare two distributions. ggplot implementation: I simply simulated 2 normal distributions of 100 points and then plotted them. Using coord_flip plots back-to-back histograms horizontally. Base implementation Not everyone likes ggplot2 so I figured I would provide in implementation in base graphics. Ggplot2 Theme Builder. See the output R script, "ggplot_styling.R", for example code to build these charts x NOTE: This script is intentionally built to limit themes to 9 colors of fewer.

ggplot2 Theme Builder

Intro to ggthemes. Library("ggplot2") library("ggthemes") p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + ggtitle("Cars") p2 <- ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(gear))) + geom_point() + ggtitle("Cars") p3 <- p2 + facet_wrap(~ am) Tufte theme and geoms Minimal theme and geoms based on plots in The Visual Display of Quantitative Information. The ggthemr package. Theming ggplot figure output The default colour themes in ggplot2 are beautiful.

The ggthemr package

Your figures look great, the colours match, and you have the characteristic “R” look and feel. The author of ggplot2, Hadley Wickham, has done a fantastic job. For the tinkerers, there’s methods to change every part of the look and feel of your figures. In practice however, changing all of the defaults can feel laborious and too much work when you want a quick change to look and feel.

The ggthemr package was developed by a friend of mine, Ciarán Tobin, who works with me at KillBiller and Edgetier. Ggplot2 tech themes, scales, and geoms. Hrbrthemes: Opinionated, typographic-centric ggplot2 themes and theme components. Scientific Journal and Sci-Fi Themed Color Palettes for ggplot2. How to format plots for publication using ggplot2 (with some help from Inkscape)

The following is the code from a presentation made by Rosemary Hartman to the Davis R Users’ Group.

How to format plots for publication using ggplot2 (with some help from Inkscape)

I’ve run the code through the spin function in knitr to produce this post. Download the script to walk through here. First, make your plot. Change fonts in ggplot2, and create xkcd style graphs. Installing and changing fonts in your plots comes now easy with the extrafonts-package.

Change fonts in ggplot2, and create xkcd style graphs

There is a excellent tutorial on the extrafonts github site, still I will shortly demonstrate how it worked for me. First, install the package and load it. You can now install the desired system fonts (at the moment only TrueType fonts): The pattern argument just specifies the fonts to be installed; if you leave it out, the function will search automatically and install all fonts (see the help function for font_import in R.

Plotting lm and glm models with ggplot. Update I followed the advice from Tim’s comment and changed the scaling in the sjPlotOdds-function to logarithmic scaling.

Plotting lm and glm models with ggplot

The screenshots below showing the plotted glm’s have been updated. Make your ggplots shareable, collaborative, and with D3. Editor's note: This is a guest post from Matt Sundquist form the Plot.ly team.

Make your ggplots shareable, collaborative, and with D3

You can access the source code for this post at Ggplotly and Plotly's R API let you make ggplot2 plots, add py$ggplotly(), and make your plots interactive, online, and drawn with D3. Let's make some. Shading between two lines – ggplot. First one to say geom_ribbon loses.

Shading between two lines – ggplot

I was plotting some data for a colleague, had two lines (repeated experiment) per person (time on the x axis) facetted by id, I thought it’d be nice to shade the area between the two lines so that when they were deviating you’d see a large shaded area, and when they were close there would be little shading, just to aid the visual of the separation between repeats. Setting individual axis limits with facet_wrap and scales = "free" in ggplot2.

Add Text Annotations to ggplot2 Faceted Plot. In my experience with R learners there are two basic types.

Add Text Annotations to ggplot2 Faceted Plot

The “show me the code and what it does and let me play” type and the “please give me step by step directions” type. I’ve broken the following tutorial on plotting text on faceted ggplot2 plots into 2 sections:The Complete Code and Final OutcomeA Bit of Explanation Hopefully, whatever learner you are you’ll be plotting text on faceted graphics in no time. Section 1: The Complete Code and Final Outcome Section 2: A Bit of Explanation. Annotating select points on an X-Y plot using ggplot2. Or, Is the Seattle Mariners outfield a disaster?

The Backstory Earlier this week (2013-06-10), a blog post by Dave Cameron appeared at USS Mariner under the title “Maybe It's Time For Dustin Ackley To Play Some Outfield”. In the first paragraph, Cameron describes to the Seattle Mariners outfield this season as “a complete disaster” and Raul Ibanez as “nothing short of a total disaster”. To back up the Ibanez assertion, the article included a link to a Fangraphs table showing the defensive metrics for all MLB outfielders with a minimum of 200 innings played to date, sorted in ascending order of UZR.150 (UZR is generally recognized as the best defensive metric). And there, at the top (or bottom) of the list, Raul Ibanez. But surely, I thought, Ibanez's offensive production – starting with the 11 home runs he had hit at the time, now up to 13 – off-sets to some degree the lack of defense. Change colour of density plots in ggplot2.

How to add texture to fill colors in ggplot2? Remove leading 0 with ggplot2. I recently had an occasion while working on a three variable interaction plot for a paper where I wanted to remove the leading 0's in the x-axis text labels using ggplot2. This was primarily due to some space concerns I had for the x-axis labels. Unfortunately, I did not find an obvious way to do this in my first go around. After tickering a bit, I've found a workaround. The process is walked through below. First, some simulated data. simdata <- data.frame(x = runif(2400, min = .032, max = .210), y = c(rnorm(2000, mean = 0, sd = .1), rnorm(400, mean = 1, sd = .25)), group = c(sample(1:2, 1600, replace = TRUE), rep(1, 400), rep(2, 400)), facet = rep(1:3, each = 800)) As shown below, initially there is no group differences, but there are facet differences. With(simdata, tapply(y, group, mean)) Ggplot2 Quick Ref: size. Most geoms have a "size" parameter.

For points, the size corresponds to their diameter. For lines, the size corresponds to their width. For text, the size corresponds to the height of their font. Using great circles and ggplot2 to map arrival/departure of 2014 US Open Tennis Players – Adventures in Analytics and Visualization. This was one of the few visualizations I worked on during the 2014 US Open Tennis Championships. Thanks to Alex Bresler and Aragorn Technologies for the data and opportunity to work with a great group of people. In this one, we’ll take a look at player’s countries, particularly the potential flight paths to and from New York City. For this, connections between countries and NYC were created using great circles. We will also modify the opacity of connecting arcs based on the number of players traveling from a particular country.

Can R and ggvis help solve Serial's murder? Recreating the vaccination heatmaps in R. Plotting tables alongside charts in R. # Create some sample data Mean <- 65. A Couple of Handy ggplot Tricks – Using Environmental Variables and Saving Charts. Why I use ggplot2. If you’ve read my blog, taken one of my classes, or sat next to me on an airplane, you probably know I’m a big fan of Hadley Wickham’s ggplot2 package, especially compared to base R plotting. Not everyone agrees. Among the anti-ggplot2 crowd is JHU Professor Jeff Leek, who yesterday wrote up his thoughts on the Simply Statistics blog: …one place I lose tons of street cred in the data science community is when I talk about ggplot2… ggplot2 is an R package/phenomenon for data visualization. Supreme Annotations. This is a follow up to a twitter-gist post & to the annotation party we’re having this week. Images as x-axis labels.

They say "if you want to find an answer on the internet, just present a wrong one as fact. Math Expressions with Facets in ggplot2. Creating Function Plots Using ggplot2.