12  Introduction to Data Visualization with ggplot2

This section is still under development

12.0.1 Making Graphs with the plot() Function

To see a graph of the tomato_weight object, pass it to plot() function as the first argument x. x stands for x-axis, and a scattered plot graph is produced as shown in ?fig-td.

#plot(x = tomato_weight)

The graph can be customized by passing values into other arguments within the plot() function. Arguments such as col changes the color while pch changes the shape of each point, ?fig-plot-cust. You can also use hist() to see the distribution of the data, ?fig-td-hist.

#| label: fig-plot-cust
#| eval: false
#| fig-cap: |
#|    Simple plot customization: point shape changed to cross and color changed to red


#plot(tomato_weight, col = "red", pch = 3)
#hist(tomato_weight, col = "coral3", xlab = "Tomato Diameter", main = "Distribution of Tomato Plant Diameter")

The xlab argument is used to customize the label of the x-axis, while the y-axis is customized with ylab. The argument main, is used to change the title of the graph. More on how to make visualization with R will be discussed in Chapter 12 and Chapter 13.

The code used to produce the graph is clear, but as you write more complex codes, readability reduces. The code could improve by writing one or two arguments in a line rather than all in a line. The code above can also be written as:

# hist(
#   tomato_weight,
#   col = "coral3",
#   xlab = "Tomato Diameter",
#   main = "Distribution of Tomato Plant Diameter"
# )

12.1 Introduction to the grammar of graphics

12.2 When to use a plot type