Remove Fill from ggplot2 Polygon Plot in R (Example)

 

In this R tutorial you’ll learn how to draw a ggplot2 polygon plot without filling color.

Table of contents:

Let’s start right away…

 

Exemplifying Data, Packages & Default Graphic

Let’s first create some example data:

data <- data.frame(x = c(2, 3, 12, 2),    # Create example data frame
                   y = c(4, 3, 6, 2))
data                                      # Print example data frame

 

table 1 data frame remove fill from ggplot2 polygon r

 

Table 1 illustrates the output of the RStudio console that got returned after executing the previous R code and shows that our example data contains four rows and two columns.

If we want to plot our data using the ggplot2 package, we also need to install and load ggplot2 to RStudio:

install.packages("ggplot2")               # Install & load ggplot2
library("ggplot2")

Next, we can draw our data using the geom_polygon function:

ggplot(data, aes(x, y)) +                 # Draw polygon plot with default colors
  geom_polygon()

 

r graph figure 1 remove fill from ggplot2 polygon r

 

The output of the previous code is illustrated in Figure 1 – A polygon plot with default fill.

 

Example: Remove Filling Color from ggplot2 Polygon Graph

This example demonstrates how to avoid the filling color in a polygon plot created by the ggplot2 package.

To accomplish this, we have to specify the color argument within the geom_polygon function, and we have to set the fill argument to NA.

Let’s do this:

ggplot(data, aes(x, y)) +                 # Draw polygon plot without filling
  geom_polygon(color = "black",
               fill = NA)

 

r graph figure 2 remove fill from ggplot2 polygon r

 

Figure 2 shows the output of the previous code: A ggplot2 polygon graph without fillings.

 

Video, Further Resources & Summary

Would you like to know more about the creation of a ggplot2 polygon plot without filling color? Then you could watch the following video that I have published on my YouTube channel. I’m explaining the R programming codes of this article in the video.

 

The YouTube video will be added soon.

 

Furthermore, you could have a look at the other articles on my website.

 

In this article you have learned how to create a ggplot2 polygon plot without a filling color in the R programming language. Let me know in the comments below, if you have further questions.

 

Subscribe to the Statistics Globe 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.

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

Menu
Top