

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A step-by-step guide on constructing ggplots using R's ggplot2 library. It covers various aspects such as creating a scatterplot, adjusting axis limits, changing colors, and customizing themes. The document also introduces other types of plots like bubble charts and ordered bar charts.
Typology: Study Guides, Projects, Research
1 / 25
This page cannot be seen from the preview
Don't miss anything!
# Setup options (scipen= 999 ) # turn off scientific notation like 1e+ library (ggplot2) data ("midwest", package = "ggplot2") _# load the data
ggplot (midwest, aes (x=area, y=poptotal)) # area and poptotal are columns in 'midw est'
2. How to Make a Simple Scatterplot
library (ggplot2) ggplot (midwest, aes (x=area, y=poptotal)) + geom_point ()
# g + xlim(0, 0.1) + ylim(0, 1000000) # deletes points
Method 2: Zooming In
library (ggplot2) g <- ggplot (midwest, aes (x=area, y=poptotal)) + geom_point () + geom_smooth (method= "lm") _# set se=FALSE to turnoff confidence bands
g1 <- g + coord_cartesian (xlim= c ( 0 ,0.1), ylim= c ( 0 , 1000000 )) # zooms in plot (g1)
4. How to Change the Title and Axis Labels
library (ggplot2) g <- ggplot (midwest, aes (x=area, y=poptotal)) + geom_point () + geom_smooth (method= "lm") # set se=FALSE to turnoff confidence bands g1 <- g + coord_cartesian (xlim= c ( 0 ,0.1), ylim= c ( 0 , 1000000 )) _# zooms in
g1 + labs (title="Area Vs Population", subtitle="From midwest dataset", y="Populati on", x="Area", caption="Midwest Demographics")
# Full Plot call library (ggplot2) ggplot (midwest, aes (x=area, y=poptotal)) + geom_point () + geom_smooth (method="lm") + coord_cartesian (xlim= c ( 0 ,0.1), ylim= c ( 0 , 1000000 )) + labs (title="Area Vs Population", subtitle="From midwest dataset", y="Population" , x="Area", caption="Midwest Demographics")
5. How to Change the Color and Size of Points
library (ggplot2) ggplot (midwest, aes (x=area, y=poptotal)) + geom_point (col="steelblue", size= 3 ) + # Set static color and size for points geom_smooth (method="lm", col="firebrick") + # change the color of line coord_cartesian (xlim= c ( 0 , 0.1), ylim= c ( 0 , 1000000 )) + labs (title="Area Vs Population", subtitle="From midwest dataset", y="Population" , x="Area", caption="Midwest Demographics")
gg + scale_colour_brewer (palette = "Set1") # change color palette
6. How to Change the X Axis Texts and Ticks Location How to Change the X and Y Axis Text and its Location?
Step 1: Set the breaks
library (ggplot2) # Base plot gg <- ggplot (midwest, aes (x=area, y=poptotal)) + geom_point ( aes (col=state), size= 3 ) + # Set color to vary based on state categor ies. geom_smooth (method="lm", col="firebrick", size= 2 ) + coord_cartesian (xlim= c ( 0 , 0.1), ylim= c ( 0 , 1000000 )) + labs (title="Area Vs Population", subtitle="From midwest dataset", y="Population" , x="Area", caption="Midwest Demographics") # Change breaks gg + scale_x_continuous (breaks= seq ( 0 , 0.1, 0.01))
library (ggplot2) gg <- ggplot (midwest, aes (x=area, y=poptotal)) + geom_point ( aes (col=state), size= 3 ) + # Set color to vary based on state categor ies. geom_smooth (method="lm", col="firebrick", size= 2 ) + coord_cartesian (xlim= c ( 0 , 0.1), ylim= c ( 0 , 1000000 )) + labs (title="Area Vs Population", subtitle="From midwest dataset", y="Population" , x="Area", caption="Midwest Demographics") # Reverse X Axis Scale gg + scale_x_reverse ()
How to Customize the Entire Theme in One Shot using Pre-Built Themes?
library (ggplot2) # Base plot gg <- ggplot (midwest, aes (x=area, y=poptotal)) + geom_point ( aes (col=state), size= 3 ) + # Set color to vary based on state categor ies. geom_smooth (method="lm", col="firebrick", size= 2 ) + coord_cartesian (xlim= c ( 0 , 0.1), ylim= c ( 0 , 1000000 )) + labs (title="Area Vs Population", subtitle="From midwest dataset", y="Population" , x="Area", caption="Midwest Demographics") gg <- gg + scale_x_continuous (breaks= seq ( 0 , 0.1, 0.01)) # method 1: Using theme_set() theme_set ( theme_classic ()) _# not run gg
gg + theme_bw () + labs (subtitle="BW Theme")
Bubble plot
# load package and data library (ggplot2) data (mpg, package="ggplot2") # mpg <- read.csv("http://goo.gl/uEeRGu") mpg_select <- mpg[mpg$manufacturer %in% c ("audi", "ford", "honda", "hyundai"), ] # Scatterplot theme_set ( theme_bw ()) # pre-set the bw theme. g <- ggplot (mpg_select, aes (displ, cty)) + labs (subtitle="mpg: Displacement vs City Mileage", title="Bubble chart") g + geom_jitter ( aes (col=manufacturer, size=hwy)) + geom_smooth ( aes (col=manufacturer), method="lm", se=F)
Histogram
library (ggplot2) theme_set ( theme_classic ()) # Histogram on a Continuous (Numeric) Variable g <- ggplot (mpg, aes (displ)) + scale_fill_brewer (palette = "Spectral") g + geom_histogram ( aes (fill=class), binwidth =. 1 , col="black", size=. 1 ) + # change binwidth labs (title="Histogram with Auto Binning", subtitle="Engine Displacement across Vehicle Classes")