Book Image

Applied Data Visualization with R and ggplot2

By : Dr. Tania Moulik
Book Image

Applied Data Visualization with R and ggplot2

By: Dr. Tania Moulik

Overview of this book

Applied Data Visualization with R and ggplot2 introduces you to the world of data visualization by taking you through the basic features of ggplot2. To start with, you’ll learn how to set up the R environment, followed by getting insights into the grammar of graphics and geometric objects before you explore the plotting techniques. You’ll discover what layers, scales, coordinates, and themes are, and study how you can use them to transform your data into aesthetical graphs. Once you’ve grasped the basics, you’ll move on to studying simple plots such as histograms and advanced plots such as superimposing and density plots. You’ll also get to grips with plotting trends, correlations, and statistical summaries. By the end of this book, you’ll have created data visualizations that will impress your clients.
Table of Contents (10 chapters)

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...