R Data Visualization Recipes
上QQ阅读APP看书,第一时间看更新

How it works...

The entire plot is designed in a very interactive/layered way. Our ggplot begins with core_plot object going all the way through h1, h2, h3, and finally h4. Readers may like to call them separately to see the changes taking place. Do not feel intimidated. A publication plot does require more code but it's not difficult to understand it, and once you have created your routine, they are not that difficult to craft.

First step is a very short one, it simply relies on xlab() and ylab() to make axes and titles account for complete information (measure units included). Next, step 2 manually picking points' shapes and colors (inside and contour). All this is done by the scale_*_manual() functions.

Values bring the reference thing (color, shape, or fill) that we want to modify. Each vector element is named after a iris$Species level. The name argument sets the legends' titles. If two or more functions do not display matching name arguments, there will be two or more separated legends.

Step 3 grows the axes using scale_*_continuous(), once both the axes display continuous variables. Arguments labels and breaks must have the exact same length. First one labels each break while the second picks the breaks. At scale_y_continuous() function a little trick was deployed-the sprintf() function was used to make sure every label showed two digits after the dot (%f.2).

Also, minor_breaks was set to zero so there won't be minor breaks. Last step can be considered lengthy but not difficult. The layer theme() conducts a wide range of changes, but all the argument names are very intuitive. This step resizes texts, relocates the legends to fit inside the figure, and creates a rectangle behind legends.

These are pretty much basics of what almost every ggplot must go through in order to be seen as publication material. Each case shall display its own nuts and bolts, so be creative. This closes for now the speech about scatterplots; can't wait to see you in the next chapter. Following section gives some useful links.