Skip to content

Instantly share code, notes, and snippets.

@aravindhebbali
Last active September 26, 2017 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aravindhebbali/f5cd1315826bd44466a15444456ea61d to your computer and use it in GitHub Desktop.
Save aravindhebbali/f5cd1315826bd44466a15444456ea61d to your computer and use it in GitHub Desktop.
Data Visualization: Scatter Plots
# scatter plot
plot(mtcars$disp, mtcars$mpg)
# add title and axis labels
plot(mtcars$disp, mtcars$mpg,
main = 'Displacement vs Miles Per Gallon',
xlab = 'Displacement', ylab = 'Miles Per Gallon')
# point shape
plot(mtcars$disp, mtcars$mpg, pch = 6)
# shape based on number of levels of a third variable
plot(mtcars$disp, mtcars$mpg, pch = nlevels(factor(mtcars$cyl)))
# shape based on a third categorical variable
plot(mtcars$disp, mtcars$mpg, pch = unclass(mtcars$cyl))
# point size
plot(mtcars$disp, mtcars$mpg, cex = 1.5)
# color
# shape between 0 and 21
plot(mtcars$disp, mtcars$mpg, pch = 5, col = 'blue', bg = 'red')
# shape between 22 and 25
plot(mtcars$disp, mtcars$mpg, pch = 24, col = 'red', bg = 'blue')
# color based on a third variable
plot(mtcars$disp, mtcars$mpg, pch = 5, col = factor(mtcars$cyl))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment