Research for Global Development

Creating and exporting histograms with R


With the {ggplot2} and {cairo} packages in R, it’s a cinch to quickly export clean, simple histogram(s).

Once your data are loaded into a dataframe, simply run some variant of the below code on an interval-level variable (like Age). It will also export a PNG and SVG of your graphs. What’s great about using a programming language like R is that it becomes very easy to iterate through many variables if you want to make multiple histograms and style them consistently.

 

library("Cairo")
library("ggplot2")
#Histogram creation and styling
m = ggplot(newdf, aes(x=Age)) + opts(title = "Age distribution of respondents")
mOutput = m + geom_histogram( fill="#666666", binwidth=1) + opts(
axis.title.y = theme_text(size = 28, angle = 90, face ="bold"),
axis.title.x = theme_text(size = 28, face ="bold"),
plot.title = theme_text(size=32, face ="bold"),
axis.text.y = theme_text(size=20),
axis.text.x = theme_text(size=20),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),
panel.background = theme_blank(),
axis.line = theme_segment(size = .5),
axis.ticks = theme_segment(size = .5),
axis.ticks.length = unit(.05, "cm")
) + scale_y_continuous(breaks=seq(0, 70, 20), expand = c(0,0)) + scale_x_continuous(breaks=seq(0, 70, 20), expand = c(0,0))
#Export
Cairo(1600,1600,file="AgeHist.png",type="png",bg="transparent",pointsize=20, units="px",dpi=400)
mOutput
dev.off()
library(Cairo)
Cairo(1600,1600,file="AgeHist.svg",type="svg",bg="transparent",pointsize=20, units="px",dpi=400)
mOutput
dev.off()

InterMedia

Creating and exporting histograms with R


With the {ggplot2} and {cairo} packages in R, it’s a cinch to quickly export clean, simple histogram(s).

Once your data are loaded into a dataframe, simply run some variant of the below code on an interval-level variable (like Age). It will also export a PNG and SVG of your graphs. What’s great about using a programming language like R is that it becomes very easy to iterate through many variables if you want to make multiple histograms and style them consistently.

 

library("Cairo")
library("ggplot2")
#Histogram creation and styling
m = ggplot(newdf, aes(x=Age)) + opts(title = "Age distribution of respondents")
mOutput = m + geom_histogram( fill="#666666", binwidth=1) + opts(
axis.title.y = theme_text(size = 28, angle = 90, face ="bold"),
axis.title.x = theme_text(size = 28, face ="bold"),
plot.title = theme_text(size=32, face ="bold"),
axis.text.y = theme_text(size=20),
axis.text.x = theme_text(size=20),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),
panel.background = theme_blank(),
axis.line = theme_segment(size = .5),
axis.ticks = theme_segment(size = .5),
axis.ticks.length = unit(.05, "cm")
) + scale_y_continuous(breaks=seq(0, 70, 20), expand = c(0,0)) + scale_x_continuous(breaks=seq(0, 70, 20), expand = c(0,0))
#Export
Cairo(1600,1600,file="AgeHist.png",type="png",bg="transparent",pointsize=20, units="px",dpi=400)
mOutput
dev.off()
library(Cairo)
Cairo(1600,1600,file="AgeHist.svg",type="svg",bg="transparent",pointsize=20, units="px",dpi=400)
mOutput
dev.off()

Marketing Materials

Contact Us:

InterMedia Headquarters

1825 K Street, NW
Suite 650
Washington, D.C. 20006
+1.202.434.9310
FAX: +1 202 434 9560
Contact | View Map

InterMedia Africa

UN Avenue, Gigiri Nairobi
Box 10224
City Square 00200
Nairobi, Kenya
+254.720.109183
Contact | View Map