Open In App

Smooth data for a geom_area graph Using ggplot2 in R

Last Updated : 09 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The ggplot2 package is a powerful and widely used package for graphic visualization. It can be used to provide a lot of aesthetic mappings to the plotted graphs. This package is widely available in R. The package can be downloaded and installed into the working space using the following command :

install.packages("ggplot2")

The ggplot method can be used to create a ggplot object. The graphical object is used to create plots by providing the data and its respective points. The data can be plotted using both points as well as lines.

Syntax : ggplot(data, aes = )

Arguments :

  • data – The data to be plotted
  • aes – The aesthetic mappings

The geom_area method is used to create an area plot. It can be used as a component in the ggplot method. The alpha parameter in the geom_area method is used to depict the opacity of a genome, the value ranges from zero to one integral values. In case, we choose a lower value, this means that a more transparent color version will be chosen to depict the plot and its smoothness. We have used the value for the alpha parameter to be one by two means it is somewhat translucent in nature. 

Syntax : geom_area (alpha = )

Arguments : 

alpha – The parameter used to depict the opacity of the graph

The stat_smooth function in the ggplot component can be used to enhance the eye in seeing patterns when there already is a plot that has been plotted. If we wish to do over plotting on it, then the stat_smooth method can be useful. 

Syntax : stat_smooth ( geom = ‘area’ , method = ‘loess’ , span , alpha , fill)

Arguments : 

  • geom – The geometric object to use display the data
  • span – Controls the smoothing of the curve 
  • method – smoothing method. We have used loess because we have lesser number of observations 
  • fill – The colour to be filled in the overplotted curve

In this case, the alpha value is equivalent to 1/2 , therefore the smoothening curve on the top of the plot is nearly opaque in nature. 

R




# importing the required libraries
library("ggplot2")
library("data.table")
library(tibble)
  
# creating a data frame
data_frame <- tibble(
  col1 = 5:17,
  col2 = c(7,5,3,1,5,2,3,5,3,1,7,8,9)
)
  
# manipulating data frame
data_frame %>% 
  ggplot(aes(col1, col2)) + 
  geom_area(alpha = 1/2) + 
  stat_smooth(
    geom = 'area', method = 'loess', span = 1/3,
    alpha = 1/2, fill = "blue")


Output :

Smooth data for a geom_area graph - ggplot2

 

If we manipulate the value of alpha to be 1/12, the overplotting curve becomes translucent in nature. 

R




# manipulating data frame
data_frame %>% 
  ggplot(aes(col1, col2)) + 
  geom_area(alpha = 1/12) + 
  stat_smooth(
    geom = 'area', method = 'loess', span = 1/2,
    alpha = 1/12, fill = "blue")


Output : 

Smooth data for a geom_area graph - ggplot2

 

Now we can try different span and alpha values for better understanding. Let’s try the span = 1 

R




# manipulating data frame
data_frame %>% 
  ggplot(aes(col1, col2)) + 
  geom_area(alpha = 1/12) + 
  stat_smooth(
    geom = 'area', method = 'loess', span = 1,
    alpha = 1/12, fill = "green")


Output : 

Smooth data for a geom_area graph - ggplot2

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads