Learn Scatter Plot in R using ggplot2 with Examples

Out[1]:

Out[2]:

scatter plot in R ggplot2

 

Example 3: Scatter Plot Size in ggplot2

Out[4]:

The geom_point function allows coloring the points based on some conditions. In the below examples the color of the points are set based on the range uptake variable of the CO2 dataset.

In [5]:

ggplot(CO2, mapping = aes(x = conc, y =uptake)) +
geom_point(size=3, aes(colour = uptake > 25 & uptake < 40), show.legend = FALSE)

Out[5]:

scatter plot in R ggplot2 Example

 

The shapes of the scatter plot in ggplot2 can be changed quite easily with the shape argument in geom_point(). The various shapes have got a specific number that you need to pass to geom_point() for your needs. The various shapes and their corresponding number are shown below –

Scatter plot with different shapes in R ggplot2
(source)
Out[10]:
We can also add a more aesthetic look to the title, subtitle, caption by adding another layer of theme and assigning text size, color, etc. as shown in below example.
Out[11]:
Out[12]:
Out[13]:
Out[14]:

 

  • Veer Kumar

    I am passionate about Analytics and I am looking for opportunities to hone my current skills to gain prominence in the field of Data Science.

    View all posts

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *