geom_lollipop() by the Chartettes

April 8, 2016

(This article was first published on R –, and kindly contributed to R-bloggers)

I make a fair share of bar charts throughout the day and really like switching to lollipop charts to mix things up a bit and enhance the visual appeal. They’re easy to do in ggplot2, just use your traditional x & y mapping for geom_point() and then use (you probably want to call this first, actually) geom_segment() mapping the yend aesthetic to 0 and the xend aesthetic to the same thing you used for the x aesthetic. But, that’s alot of typing. Hence, the need for geom_lollipop().

I’ll build this example from one provided by Stephanie Evergreen (that one’s in Excel). It’s not much code:

df <- read.csv(text="category,pct
South Asian/South Asian Americans,0.12
S Asian/Asian Americans,0.25
Muslim Observance,0.29
Africa/Pan Africa/African Americans,0.34
Gender Equity,0.34
Disability Advocacy,0.49
European/European Americans,0.52
Pacific Islander/Pacific Islander Americans,0.59
Non-Traditional Students,0.61
Religious Equity,0.64
Caribbean/Caribbean Americans,0.67
Middle Eastern Heritages and Traditions,0.73
Trans-racial Adoptee/Parent,0.76
Mixed Race,0.80
Jewish Heritage/Observance,0.85
International Students,0.87", stringsAsFactors=FALSE, sep=",", header=TRUE)
# devtools::install_github("hrbrmstr/ggalt")
gg <- ggplot(df, aes(reorder(category, pct), pct))
gg <- gg + geom_lollipop(point.colour="steelblue", point.size=3)
gg <- gg + scale_y_continuous(expand=c(0,0), labels=percent,
                              breaks=seq(0, 1, by=0.2), limits=c(0, 1))
gg <- gg + coord_flip()
gg <- gg + labs(x=NULL, y=NULL, 
                title="SUNY Cortland Multicultural Alumni survey results",
                subtitle="Ranked by race, ethnicity, home land and orientationnamong the top areas of concern",
                caption="Data from")
gg <- gg + theme_minimal(base_family="Arial Narrow")
gg <- gg + theme(panel.grid.major.y=element_blank())
gg <- gg + theme(panel.grid.minor=element_blank())
gg <- gg + theme(axis.line.y=element_line(color="#2b2b2b", size=0.15))
gg <- gg + theme(axis.text.y=element_text(margin=margin(r=-5, l=0)))
gg <- gg + theme(plot.margin=unit(rep(30, 4), "pt"))
gg <- gg + theme(plot.title=element_text(face="bold"))
gg <- gg + theme(plot.subtitle=element_text(margin=margin(b=10)))
gg <- gg + theme(plot.caption=element_text(size=8, margin=margin(t=10)))

And, I’ll reiterate Stephanie’s note that the data is fake.


Compare it with it’s sister bar chart:


to see which one you think works better (it really does come down to personal aesthetics choice).

You can find it in the development version of ggalt. The API is not locked in yet so definitely provide feedback in the issues.

To leave a comment for the author, please follow the link and comment on their blog: R – offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)