-
Book Overview & Buying
-
Table Of Contents
R for Data Science Cookbook
By :
Geometric objects are elements that we mark on the plot. One can use the geometric object in ggplot2 to create either a line, bar, or box chart. Moreover, one can integrate these simple geometric objects and aesthetic mapping to create a more professional plot. In this recipe, we introduce how to use geometric objects to create various charts.
Ensure you have completed the previous steps by storing sample_sum in your R environment.
Perform the following steps to create a geometric object in ggplot2:
First, create a scatterplot with the geom_point function:
> g <- ggplot(data=sample_sum, mapping=aes(x=Year_Month, y=Total_Sales, col=Province )) + ggtitle('Scatter Plot') > g + geom_point()
Use the geom_line function to plot a line chart:
> g+ geom_line(linetype="dashed")

Figure 9: Scatterplot and dashed line chart
Use the geom_bar function to make a stack bar chart:
> g+geom_bar(stat = "identity", aes(fill=Province) , position...
Change the font size
Change margin width
Change background colour