Introducing geometric objects
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.
Getting ready
Ensure you have completed the previous steps by storing sample_sum
in your R environment.
How to do it…
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")
Use the
geom_bar
function to make a stack bar chart:> g+geom_bar(stat = "identity", aes(fill=Province) , position...