#How to create your own custom ggplot theme
ggplot2 is a great plotting library for R. However the
default styling is not the best. Fortunately it is fairly easy to create your own. I
made a new them called theme_ice
which has better margins and uses nicer Ubuntu fonts.
Have a look here http://docs.ggplot2.org/current/theme.html for more details on the parameters available.
library("ggplot2");
library("grid");
theme_ice <- function ()
{
theme(
text = element_text(size=16, family="Ubuntu Condensed"),
plot.title = element_text(size=16, family="Ubuntu Condensed"),
legend.title = element_text(size=14, family="Ubuntu Condensed", face = "italic"),
legend.position = c(.9, .5),
legend.key = element_rect(fill = "#f0f1f2",size=2),
panel.background = element_rect(fill = "#f0f1f2"),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(colour = "#d3d3d3"),
panel.grid.minor = element_blank(),
plot.background = element_rect(fill = "#f0f1f2"),
axis.ticks = element_line(color = "black"),
axis.line = element_line(colour = "black"),
axis.line.y = element_blank(),
axis.ticks.margin = unit(0.2, "cm"),
axis.ticks.length = unit(0.2, "cm"),
axis.ticks.y = element_blank(),
plot.margin = unit(c(1.5, 0.8, 0.8, 0.8), "cm"),
axis.title.x = element_text(vjust=-0.3),
axis.title.y = element_text(vjust=1.5)
)
}
An example using the theme:
<img style=”display:block;margin:20px auto;” src=/img/mpg.png>
The above can be generated from the following:
ggplot(mpg, aes(x = cty, y = hwy, color = factor(cyl))) +
geom_jitter() +
labs(x = "City mileage/gallon",
y = "Highway mileage/gallon",
color = "Cylinders") + theme_ice()