Performing transformations
Besides mapping particular variables to either the x or y axis, one can first perform statistical transformations on variables, and then remap the transformed variable to a specific position. In this recipe, we introduce how to perform variable transformations with ggplot2
.
Getting ready
Ensure you have completed the previous steps by storing sum_price_by_province
, and sample_sum
in your R environment.
How to do it…
Perform the following steps to perform statistical transformation in ggplot2
:
First, create a dataset named
sample_sum2
by filtering sales data fromAlberta
andBritish Columbia
:> sample_sum2 <- sum_price_by_province %>% filter(Province %in% c('Alberta', 'British Columbia' ) )
Create a line plot with a regression line, using the
geom_point
andgeom_smooth
functions:> g <- ggplot(data=sample_sum2, mapping=aes(x=Year_Month, y=Total_Sales, col=Province )) > g + geom_point(size=5) + geom_smooth() + ggtitle('Adding Smoother')