> For the complete documentation index, see [llms.txt](https://r-pedia.gitbook.io/cevi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r-pedia.gitbook.io/cevi/r-graphics-cookbook/untitled.md).

# ggplot2 intro

## 1. Understanding the Ggplot Syntax:

{% hint style="info" %}
The main difference is that, unlike base graphics, ggplot works with dataframes and not individual vectors.
{% endhint %}

```r
# Setup
options(scipen=999)  # turn off scientific notation like 1e+06
library(ggplot2)
data("midwest", package = "ggplot2")  # load the data

# Init Ggplot
ggplot(midwest, aes(x=area, y=poptotal))  # area and poptotal are columns in 'midwest'
```

{% hint style="info" %}
&#x20;`aes()` function is used to specify the X and Y axes. Even though the `x` and `y` are specified, there are no points or lines in it. This is because, ggplot doesn’t assume that you meant a scatterplot or a line chart to be drawn. [More about aes](https://ggplot2.tidyverse.org/articles/ggplot2-specs.html).&#x20;
{% endhint %}

## 2. How to Make a Simple Scatterplot:

&#x20;Adding points using a geom layer called `geom_point`. More about geom\_xxx : [List of geom\_xxx](https://ggplot2.tidyverse.org/reference/index.html)

```
ggplot(midwest, aes(x=area, y=poptotal)) + 
    geom_point() 
```

![](/files/-LNqw9rmuwUJSoSAd2n-)

Like `geom_point()`, there are many such geom layers which we will see in list.&#x20;

Example: a smoothing layer using `geom_smooth(method='lm')`.&#x20;

```r
g <- ggplot(midwest, aes(x=area, y=poptotal)) + 
  geom_point() + 
  geom_smooth(method="lm")  # set se=FALSE to turnoff confidence bands
plot(g)
```

![](/files/-LNqwkSiQSPnUz7ElL3A)

The majority of points lie in the bottom of the chart which doesn’t really look nice. So, let’s change the Y-axis limits to focus on the lower half.

## 3. Adjusting the X and Y axis limits:

#### **Method 1**: By deleting the points outside the range

```r
g <- ggplot(midwest, aes(x=area, y=poptotal)) + 
  geom_point() + 
  geom_smooth(method="lm")  # set se=FALSE to turnoff confidence bands
# Delete the points outside the limits
g + xlim(c(0, 0.1)) + ylim(c(0, 1000000))   # deletes points
```

#### **Method 2**: Zooming In

```r
g <- ggplot(midwest, aes(x=area, y=poptotal)) + 
  geom_point() + 
  geom_smooth(method="lm")  # set se=FALSE to turnoff confidence bands

# Zoom in without deleting the points outside the limits. 
# As a result, the line of best fit is the same as the original plot.
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:

```r
# set se=FALSE to turnoff confidence bands
g <- ggplot(midwest, aes(x=area, y=poptotal)) + 
  geom_point() + 
  geom_smooth(method="lm")  

# zooms in
g1 <- g + 
  coord_cartesian(xlim=c(0,0.1), ylim=c(0, 1000000))  

# Add Title and Labels
g1 + 
  labs(title="Area Vs Population", subtitle="From midwest dataset", y="Population", x="Area", caption="Midwest Demographics")
# or
g1 + 
  ggtitle("Area Vs Population", subtitle="From midwest dataset") + 
  xlab("Area") + 
  ylab("Population")
```

&#x20;**Here is the full function:**

```r
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 Point&#x73;**:**

### #How to Change the Color and Size To Static?

```r
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="green") +  # 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")
```

![](/files/-LNteK4I32tMN2F9iWxc)

### #How to Change the Color To Reflect Categories in Another Column?

```r
gg <- ggplot(midwest, aes(x=area, y=poptotal)) + 
  geom_point(aes(col=state), size=3) +  # Set color to vary based on state categories.
  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")
plot(gg)
```

![](/files/-LNteTb72QiS69YTYV5q)

&#x20;As an added benefit, the legend is added automatically. If needed, it can be removed by setting the `legend.position` to `None` from within a `theme()` function.

```r
gg <- ggplot(midwest, aes(x=area, y=poptotal)) + 
  geom_point(aes(col=state), size=3) +  # Set color to vary based on state categories.
  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")
plot(gg)
gg + theme(legend.position="None")  # remove legend
```

![](/files/-LNtfEbXAbl_bZ1IvlE5)

&#x20;Also, You can change the color palette entirely.

```r
gg + scale_colour_brewer(palette = "Set1")  # change color palette
```

![](/files/-LNtj-EH-lEJ0Jk3Bf06)

```r
More of such palettes can be found in the RColorBrewer package
library(RColorBrewer)
head(brewer.pal.info, 10)  # show 10 palettes
#>          maxcolors category colorblind
#> BrBG            11      div       TRUE
#> PiYG            11      div       TRUE
#> PRGn            11      div       TRUE
#> PuOr            11      div       TRUE
#> RdBu            11      div       TRUE
#> RdGy            11      div      FALSE
#> RdYlBu          11      div       TRUE
#> RdYlGn          11      div      FALSE
#> Spectral        11      div      FALSE
#> Accent           8     qual      FALSE
```

![](/files/-LNtjG27f35ua7pykKtY)

![](/files/-LNtjNExiha7XOWM4szW)

## 6. How to Change the X Axis Texts and Ticks Location:

### #How to Change the X and Y Axis Text and its Location?

&#x20;This involves two aspects: `breaks` and `labels`.

&#x20;**Step 1: Set the `breaks`**

&#x20;`scale_x_continuous` because, the X axis variable is a continuous variable. Had it been a date variable, `scale_x_date` could be used. Like `scale_x_continuous()` an equivalent `scale_y_continuous()` is available for Y axis.

```r
# Base plot
gg <- ggplot(midwest, aes(x=area, y=poptotal)) + 
  geom_point(aes(col=state), size=3) +  # Set color to vary based on state categories.
  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))
```

&#x20;**Step 2: Change the `labels`**&#x20;

```
#Let me demonstrate by setting the labels to alphabets 
#from a to k (though there is no meaning to it in this context).

# Base Plot
gg <- ggplot(midwest, aes(x=area, y=poptotal)) + 
  geom_point(aes(col=state), size=3) +  # Set color to vary based on state categories.
  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 + label
gg + scale_x_continuous(breaks=seq(0, 0.1, 0.01), labels = letters[1:11])
```

&#x20;If you need to reverse the scale, use `scale_x_reverse()`.

```r
gg <- ggplot(midwest, aes(x=area, y=poptotal)) + 
  geom_point(aes(col=state), size=3) +  # Set color to vary based on state categories.
  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 Write Customized Texts for Axis Labels, by Formatting the Original Values?

&#x20;\* **Method 1**: Using `sprintf()`

&#x20;\* **Method 2**: Using a custom user defined function. (Formatted 1000’s to 1K scale)

```r
# Base Plot
gg <- ggplot(midwest, aes(x=area, y=poptotal)) + 
  geom_point(aes(col=state), size=3) +  # Set color to vary based on state categories.
  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 Axis Texts
gg + scale_x_continuous(breaks=seq(0, 0.1, 0.01), labels = sprintf("%1.2f%%", seq(0, 0.1, 0.01))) + 
  scale_y_continuous(breaks=seq(0, 1000000, 200000), labels = function(x){paste0(x/1000, 'K')})
```

### #How to Customize the Entire Theme in One Shot using Pre-Built Themes?

&#x20;Finally, instead of changing the theme components individually.  We can change the entire theme itself using pre-built themes. The help page `?theme_bw` shows all the available built-in themes. We have 2 methode:  \* Use the theme\_set()  and  \* Draw the ggplot and then add the overall theme setting (eg. `theme_bw()`)

```r
# Base plot
gg <- ggplot(midwest, aes(x=area, y=poptotal)) + 
  geom_point(aes(col=state), size=3) +  # Set color to vary based on state categories.
  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

# method 2: Adding theme Layer itself.
gg + theme_bw() + labs(subtitle="BW Theme")
gg + theme_classic() + labs(subtitle="Classic Theme")
```

![](/files/-LNuDl0A_gom8PUHMeEE)
