About ggplot2 for R

Posted by:

What is ggplot2

ggplot2 is a plotting system for R, based on the grammar of graphics, which tries to take the good parts of base and lattice graphics and none of the bad parts. It takes care of many of the fiddly details that make plotting a hassle (like drawing legends) as well as providing a powerful model of graphics that makes it easy to produce complex multi-layered graphics. – http://ggplot2.org/

Theme element functions

element_text()

Purpose: To control the drawing of labels and headings.

The table below lists the arguments of element_text() and their corresponding default values.

Argument Description Default value
family font family “”
face font face “plain”
colour font color “black”
size font size (pts) 10
hjust horizontal justification 0.5
vjust vertical justification 0.5
angle text angle 0
lineheight line height 1.1

element_line()

Purpose: To draw lines and segments such as graphics region boundaries, axis tick marks and grid lines.

Argument Description Default value
colour line color “black”
size line thickness 0.5
linetype type of line 1

element_rect()

Purpose: To draw rectangles. It is mostly used for background elements and legend keys.

Argument Description Default value
fill fill color NA (none)
colour border color “black”
size thickness of border line 0.5
linetype type of border line 1 (solid)

element_blank()

Purpose: To draw nothing.

Arguments: none.

The element_blank() function can be applied to any theme element controlled by a theme element function.

With these functions in place, we now turn to the theme elements themselves.

Theme elements

Axis attributes

The table below lists each theme element associated with the rendering of axes, the theme function or unit function that controls it, and a short description of the element.

Element Theme function Unit Description
axis.line line   line parallel to axis
axis.text text   tick labels
axis.text.x text   x-axis tick labels
axis.text.y text   y-axis tick labels
axis.title text   axis titles
axis.title.x text   x-axis title
axis.title.y text   y-axis title
axis.ticks line   axis tick marks
axis.ticks.length   cm length of tick marks
axis.ticks.margin*   cm width of axis tick margin

`axis.ticks.margin` is deprecated. Please set `margin` property of `axis.text` instead

Legend attributes

The following theme elements are associated with the appearance of legends:

Element Theme function Unit Default value Description
legend.background rect     legend background
legend.key rect     background of legend keys
legend.key.size   lines    
legend.key.height   cm   legend key height
legend.key.width   cm   legend key width
legend.margin   lines   legend margin
legend.text text     legend labels
legend.text.align     NULL legend label alignment
legend.title text     legend name
legend.title.align     NULL legend name alignment
legend.position     “right” position of legend
legend.direction horizontal/vertical   NULL direction of legend keys
legend.justification     “center” justification of legend
legend.box horizontal/vertical   NULL position of multiple legend boxes

The following table summarizes the possible values for each legend theme element associated with positioning:

Theme element Potential values
legend.direction, legend.box “vertical”, “horizontal”
legend.position “top”, “bottom”, “left”, “right” “none”
legend.justification 1st value: “left”, “center”, “right”
  2nd value: “top”, “center”, “bottom”

Panel attributes

Panel elements are associated with the graphics region in a ggplot. In faceted ggplots, these elements apply uniformly to each graphics panel.

Element Theme function Unit Description
panel.background rect   background of graphics region
panel.border rect   border of graphics region
panel.grid.major line   major grid lines
panel.grid.minor line   minor grid lines
panel.margin   lines margin between facets

Plot attributes

These elements pertain to the entire graphics region of a ggplot, but particularly outside a graphics panel (or the set of panels in a faceted plot).

Element Theme function Unit Description
plot.background rect   background of entire plot
plot.title text   plot title
plot.margin   cm vector of margins around plot

Sigmaplot Chart Standard

Title: Arial 14 Axis Title: Arial 12 Axis Text: Arial 10 Legend Text: Arial 10 All line thickness: 0.01in Major Tick Length: 0.05in Minor Tick Length: 0.03in

Dual axes

Follow instruction and code here. Notes: following packages needed:

legend does not work, fix with here and the following code:

Resources about themes

ggthemes for additional themes, a few publication theme for science, few, base and tufet details information about theme functions and elements here. A comprehensive tutorial is from here. yet another good site. inheritance tree can be ploted following here. default theme is theme_grey, theme_grey to list the full function code. theme_grey to list all the element.

modify theme easily

Random Notes

note-1

name what is it example code
lineheight space between lines g+theme(plot.title = element_text(size=20, face=”bold”, vjust=1, lineheight=0.6))
expression expression g<-g+labs(x=”Date”, y=expression(paste(“Temperature ( “, degree ~ F, ” )”)), title=”Temperature”)
vjust Move the labels away from the plot (and add color) g + theme(axis.title.x = element_text(color=”forestgreen”, vjust=-0.35),axis.title.y = element_text(color=”cadetblue” , vjust=0.35) )
ylim(), scale_x_continuous(), coord_cartesian() ylim(), scale_x_continuous(), coord_cartesian()  
name change legend name g+theme(legend.title = element_text(colour=”chocolate”, size=16, face=”bold”))+scale_color_discrete(name=”This color is\ncalled chocolate!?”)
legend.key legend background box g+theme(legend.key=element_rect(fill=’pink’))
guides(), guide_legend size of legend symbols g+guides(colour = guide_legend(override.aes = list(size=4)))
     

note-2

  1. axis ticks face inwards in ggplot2.

    ggolot.inward_tick.png

  2. debug and margin for text (has to set individually for each axis as of version 2.1)

    ggolot.debug.png

  3. border

    ggplot_border.png

Stat_Summary

Plot against index

0

Add a Comment