Skip to content

Jon Peltier uses several dummy series to create a variable width column chart in Excel. While some simple data manipulation is needed, making similar charts in ggplot2 is very easy.

I will be using the sample data provided to draw the above chart.

 ```> df <- data.frame(x = c("Alpha", "Beta", "Gamma", "Delta"), width = c(25, 50, 75, 100), height = c(100, 75, 50, 25))```

Now, I will calculate some additional variables to make plotting easier, and to center the text labels.

 ```> df\$w <- cumsum(df\$width) > df\$wm <- df\$w - df\$width > df\$wt <- with(df, wm + (w - wm)/2)```

Now we are ready to plot.

## Plotting

 ```> library(ggplot2) > p <- ggplot(df, aes(ymin = 0)) > p1 <- p + geom_rect(aes(xmin = wm, xmax = w, ymax = height, fill = x))``` Add text labels:

 ```> p2 <- p1 + geom_text(aes(x = wt, y = height * 0.5, label = x))``` Final formatting: apply theme + remove legend, and axis labels:

 ```> p2 + theme_bw() + opts(legend.position = "none") + labs(x = NULL, y = NULL)``` Advertisements
9 Comments leave one →
1. jonpeltier permalink
March 29, 2009 6:15 pm

I keep telling myself I have to start trying out R. I’ve even downloaded it.

There are so far two impediments:

1. Not enough hours in the day.
2. Not well aligned with client requirements.

If I’m able to address issue 1, I may find the answers to issue 2.

2. Bill S. permalink
April 14, 2010 8:05 am

Hi,

I’m new to R.

Trying to follow this exactly, but i get this error:

Error: No layers in plot

what did i do wrong?

• learnr permalink*
April 14, 2010 10:59 am

You do not specify after which command you get this error, but my guess would be that it happens around here:

```p <- ggplot(df, aes(ymin = 0)) p1 <- p + geom_rect(aes(xmin = wm, xmax = w, ymax = height, fill = x))```

The plot is not ready to be displayed until at least one layer is added. You will get an error by executing `p` and need to add a new `geom_rect` layer to the ggplot2 object, these two are now stored in `p1`, and executing `p1` should work fine.

3. Julie permalink
March 27, 2013 3:35 pm

Hi
I just made this plot and it works fine, but I have one problem. When i make this polt and put on labels on the x and y axis, the y-axe is fine but the label at the x-axe is sort of overlapping the numbers on the x-axe. How can I move the label down so there is more space between the numbers and the label?

• learnr permalink*
June 6, 2013 9:54 am

Please look at the ggplot2 theming options.

4. Jason permalink
July 17, 2015 10:31 pm

Thank you for this tutorial. I was wondering how one might go about making the same kind of chart but with x and y axes swapped. I work with soil data where we take a series of samples going down, and I would like to represent the variable width of the sampling depth on the y axis, and chemical values (like total carbon) on the x axis. How would one go about implementing this in ggplot2? Does the geom_rect command allow for this?

5. Caroline permalink
January 25, 2017 8:05 pm

This is excellent! I was wondering if there is a way to make it so there’s a little bit of space between each bar…is this possible?

6. January 4, 2018 2:12 am

There is now a package that you can use to make a variable width bar plot similar to the one above: https://cran.r-project.org/web/packages/mekko/vignettes/mekko-vignette.html

### Trackbacks

1. ggplot2: Marimekko/Mosaic Chart « Learning R