Scatter plots

By Data Tricks, 1 June 2018

Last modified 5 September 2019

First run the following code to set-up the data and set some theme options.

rm(list=ls())
library(ggplot2)
set.seed(555)
data <- data.frame(
Value1 = rnorm(1000, 500, 200),
Category = rep(c("One", "Two", "Three", "Four"), each=1, times=250),
Random = rnorm(1000, 100, 100),
Size = sample(1:10, 1000, replace = TRUE)
)
data$Value2 <- data$Value1 + data$Random
data$Size2 <- data$Value1 * data$Size
theme <- theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line(color="#919191", size = 0.1),
axis.line.y = element_line(color="#919191", size = 0.1))

Basic scatter plot

#Basic scatter chart
gg <- ggplot(data)
gg <- gg + geom_point(aes(x = Value1, y = Value2), stat = "identity")
gg <- gg + xlab("") + ylab("") + theme
print(gg)

Custom colours scatter plot

#Custom colour scatter chart
gg <- ggplot(data)
gg <- gg + geom_point(aes(x = Value1, y = Value2), colour="#62a4d1", stat = "identity")
gg <- gg + xlab("") + ylab("") + theme
print(gg)

Custom sized markers scatter plot

gg <- ggplot(data)
gg <- gg + geom_point(aes(x = Value1, y = Value2, size = Value1), alpha=0.1, colour="#ff0000", stat = "identity")
gg <- gg + xlab("") + ylab("") + theme
print(gg)

Tags: , ,

Leave a Reply

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

Please note that your first comment on this site will be moderated, after which you will be able to comment freely.