Study Guide Data Visualization With R
Study Guide Data Visualization With R
003 Software Tools — Data Science Afshine Amidi & Shervine Amidi
geom_boxplot(
Afshine Amidi and Shervine Amidi Box
x, y, params
plot
)
August 21, 2020
geom_tile(
General structure Heatmap x, y, params
r Overview – The general structure of the code that is used to plot figures is as follows: )
R
ggplot(...) +............# Initialization where the possible parameters are summarized in the table below:
..geom_function(...) +...# Main plot(s)
..facet_function(...) +..# Facets (optional) Command Description Use case
..labs(...) +............# Legend (optional)
..scale_function(...) +..# Scales (optional) color Color of a line / point / border ’red’
..theme_function(...)....# Theme (optional)
fill Color of an area ’red’
We note the following points: size Size of a line / point 4
• The ggplot() layer is mandatory. shape Shape of a point 4
• When the data argument is specified inside the ggplot() function, it is used as default in linetype Shape of a line ’dashed’
the following layers that compose the plot command, unless otherwise specified.
alpha Transparency, between 0 and 1 0.3
• In order for features of a data frame to be used in a plot, they need to be specified inside
the aes() function.
r Maps – It is possible to plot maps based on geometrical shapes as follows:
r Basic plots – The main basic plots are summarized in the table below:
geom_point(
Scatter x, y, params
plot )
geom_line(
Line x, y, params
plot ) The following table summarizes the main commands used to plot maps:
r Animations – Plotting animations can be made using the gganimate library. The following r Additional elements – We can add objects on the plot with the following commands:
command gives the general structure of the code:
# Main plot
ggplot() + geom_vline(
..... +
..transition_states(field, states_length) xintercept, linetype
)
# Generate and save animation
animate(plot, duration, fps, width, height, units, res, renderer)
anim_save(filename) Line
geom_hline(
yintercept, linetype
)
Advanced features
r Facets – It is possible to represent the data through multiple dimensions with facets using
the following commands:
geom_curve(
x, y, xend, yend
Type Command Illustration Curve
)
facet_grid(
Grid row_var ∼ column_var
(1 or 2D) ) geom_rect(
Rectangle xmin, xmax, ymin, ymax
)
facet_wrap(
Wrapped vars(x1, ..., xn),
nrow, ncol
)
Last touch
r Legend – The title of legends can be customized to the plot with the following command:
r Text annotation – Plots can have text annotations with the following commands:
R
Command Illustration plot + labs(params)
Remark: in order to fix the same appearance parameters for all plots, the theme_set() function
can be used.
r Scales and axes – Scales and axes can be changed with the following commands:
r Saving figure – It is possible to save figures with predefined parameters regarding the scale,
width and height of the output image with the following command:
R
Classic theme_classic()
ggsave(plot, filename, scale, width, height)
Minimal theme_minimal()
None theme_void()