Zoom into ggplot2 Plot without Removing Data in R (Example)

 

In this R tutorial you’ll learn how to set axis limits of a ggplot2 plot without removing data points.

The post looks as follows:

Here’s the step-by-step process…

 

Example Data, Add-On Packages & Basic Plot

As first step, let’s define some example data:

data <- data.frame(x = 1:4,                        # Create example data
                   y = 1:8,
                   group = rep(letters[1:4], 2))
data                                               # Print example data
#   x y group
# 1 1 1     a
# 2 2 2     b
# 3 3 3     c
# 4 4 4     d
# 5 1 5     a
# 6 2 6     b
# 7 3 7     c
# 8 4 8     d

The previous output of the RStudio console shows that our example data has three columns. The variables x and y are numeric and the variable group defines the groups of our data points.

If we want to create a graphic of the data with the ggplot2 package, we also have to install and load ggplot2:

install.packages("ggplot2")                        # Install ggplot2 package
library("ggplot2")                                 # Load ggplot2 package

Now, we can plot our data as shown below:

ggp <- ggplot(data, aes(x, y, group = group)) +    # Create ggplot2 plot without zoom
  geom_line()
ggp                                                # Draw plot

 

r graph figure 1 zoom ggplot2 without removing data r

 

The output of the previous R programming syntax is shown in Figure 1 – A basic ggplot2 line plot without any zoom.

Let’s try to zoom in with the next part of the code:

ggp +                                              # Zoom in & cut off values
  ylim(3, 10)
# Warning:
# Removed 2 rows containing missing values (geom_path).

 

r graph figure 2 zoom ggplot2 without removing data r

 

The RStudio console returns a warning message: Removed 2 rows containing missing values (geom_path). Furthermore, some lines of our data are removed entirely from our second graph, even though they should be shown in the zoomed plot…

 

Example: Zoom In without Cutting Off Data

This Example illustrates how to zoom in properly, without losing any data points. For this task, we have to use the coord_cartesian argument. Within the coord_cartesian argument, we are defining the axis limits of our ggplot2 plot:

ggp +                                              # Zoom without cutting values
  coord_cartesian(ylim = c(3, 10))

 

r graph figure 3 zoom ggplot2 without removing data r

 

As you can see based on our third plot, this time we haven’t lost any lines of our data, i.e. the first and the second line are still shown.

 

Video, Further Resources & Summary

Do you need further explanations on the R programming syntax of this article? Then you could watch the following video of the Statistics Globe YouTube channel. I’m explaining the content of this page in the video:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

In addition, you could read the other R programming tutorials on this homepage. I have published numerous articles already:

 

In this tutorial, I explained how to zoom into the area of a plot without deleting data in R. In case you have any additional comments and/or questions, let me know in the comments below.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top