ggplot2 is a great library in R allowing for advanced control of your plots. The help page developed by the creator of the library (http://docs.ggplot2.org/current/) is a great place to start looking for some help! In this post I will go through the various command that allows you to control the theme of your ggplot
library(ggplot2)
library(grid)
data(mtcars)
# a basic scatterplot
p <- ggplot(mtcars, aes(x = disp, y = mpg))
p + geom_point()
Now there are several things that could be changed like the grey background, the color of the axis label, the size of the axis label. All these elements can be set using the theme command.
# each element in the graph as a name for example to change the axis label
# on the x axis we have to call the axis.text.x within the theme
p + geom_point() + theme(axis.text.x = element_text(color = "black"))
# the axis labels is text so it respond to the function element_text, the
# other function are element_line, element_rect and unit. Let's for
# exmaple change the grey background to white
p + geom_point() + theme(panel.background = element_rect(fill = "white", color = "black"))
# we can also save the theme arguments into an object and call it
# everytime we need it.
blanck_theme <- theme(axis.text = element_text(size = 12, color = "black"),
axis.title = element_text(size = 14), panel.background = element_rect(fill = "white"),
panel.grid = element_line(linetype = "blank"), axis.line = element_line(linetype = "solid"))
p + geom_point() + blanck_theme
Now another tricky part when building graph is the settings of the legend, let's see of this works with ggplot.
p <- ggplot(mtcars, aes(x = disp, y = mpg, color = factor(gear)))
# a standard plot
p + geom_point()
# changing some legend settings, the label size, the colors, the title,
# the key size
legend_theme <- theme(legend.text = element_text(size = 12, face = "italic"),
legend.title = element_text(size = 14), legend.key.size = unit(1, "cm"),
legend.key = element_rect(fill = NA))
# be carefull to be able to use the unit function you will need the grid
# library
p + geom_point() + legend_theme + scale_color_brewer(palette = "Set1", name = "Gear")
So I hope that with this you'll be able to build graph tailored for your need!