Reader small image

You're reading from  Applied Data Visualization with R and ggplot2

Product typeBook
Published inSep 2018
Reading LevelIntermediate
Publisher
ISBN-139781789612158
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Dr. Tania Moulik
Dr. Tania Moulik
author image
Dr. Tania Moulik

Tania Moulik has a PhD in particle physics. She has worked at CERN, the European Organization for Nuclear Research, and on the Tevatron at Fermi National Accelerator Laboratory in IL, USA. She has years of programming experience in C++, Python, and R. She has also worked in the feld of big data and has worked with technologies such as grid computing. She has a passion for data analysis and would like to share her passion with others who would like to delve into the world of data analytics. She especially likes R and ggplot2 as a powerful analytics package.
Read more about Dr. Tania Moulik

Right arrow

Changing Styles and Colors


Aside from faceting, we can also produce a color differentiated plot. It can be advantageous to use a color differentiated plot when the shapes are very similar and there is some overlap. To see small differences, it is useful to use colors. For example, we can plot the Electricity consumption versus GDP by using different colors or shapes for the countries.

Using Different Colors to Group Points by a Variable

In this section, we'll produce a color differentiated scatter plot with respect to a third variable. Let's begin by implementing the following steps:

  1. Choose a subset of dataset 1 (gapminder) and select a few countries. Use the following subset command:
dfs <- subset(df,Country %in%c("Germany","India","China","United States"))
  1. Make a scatter plot of the two variables and change the x and y titles:
p1<- ggplot(df,aes_string(x=var1,y=var2))+)geom_point(color=2,shape=2)+xlim(0,10000)+xlab(name1)+ylab(name2)
  1. Then, change the colors and shapes of the points for...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Applied Data Visualization with R and ggplot2
Published in: Sep 2018Publisher: ISBN-13: 9781789612158

Author (1)

author image
Dr. Tania Moulik

Tania Moulik has a PhD in particle physics. She has worked at CERN, the European Organization for Nuclear Research, and on the Tevatron at Fermi National Accelerator Laboratory in IL, USA. She has years of programming experience in C++, Python, and R. She has also worked in the feld of big data and has worked with technologies such as grid computing. She has a passion for data analysis and would like to share her passion with others who would like to delve into the world of data analytics. She especially likes R and ggplot2 as a powerful analytics package.
Read more about Dr. Tania Moulik