ggplot2 Getting started with ggplot2

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


This section provides an overview of what ggplot2 is, and why a developer might want to use it.

It should also mention any large subjects within ggplot2, and link out to the related topics. Since the Documentation for ggplot2 is new, you may need to create initial versions of those related topics.

Basic example of ggplot2

We show a plot similar to the showed at Linear regression on the mtcars dataset. First with defaults and the with some customization of the parameters.

fit <- lm(mpg ~ wt, data = mtcars)
bs <- round(coef(fit), 3) 
lmlab <- paste0("mpg = ", bs[1],
                ifelse(sign(bs[2])==1, " + ", " - "), abs(bs[2]), " wt ")

#with defaults
ggplot(aes(x=wt, y=mpg), data = mtcars) +  
  geom_point() +
  geom_smooth(method = "lm", se=FALSE, formula = y ~ x)

enter image description here

#some customizations
ggplot(aes(x=wt, y=mpg,colour="mpg"), data = mtcars) +  
geom_point(shape=21,size=4,fill = "blue",alpha=0.55, color="red") +
scale_x_continuous(breaks=seq(0,6, by=.5)) +
geom_smooth(method = "lm", se=FALSE, color="darkgreen", formula = y ~ x) +
geom_hline(yintercept=mean(mtcars$mpg), size=0.4, color="magenta") +
xlab("Weight (1000 lbs)") + ylab("Miles/(US) gallon") +
labs(title='Linear Regression Example',
     caption="Source: mtcars") +
annotate("text", x = 4.5, y = 21, label = "Mean of mpg") +
annotate("text", x = 4.8, y = 12, label = "Linear adjustment",color = "red") +

enter image description here

See other examples at ggplot2

How to install and run ggplot2

To install and load the current stable version of ggplot2 for your R installation use:

# install from CRAN

To install the development version from github use

# install.packages("devtools")

Load into your current R session, and make an example.

Got any ggplot2 Question?