- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change the partial plot background using ggplot2 in R?
To change the partial plot background, we can create a rectangle using geom_rect function by defining both the axes values and alpha for transparency, the color will be changed by using fill argument. The value of alpha will completely hide the grey background and we can play with its value based on our need.
Example
Consider the below data frame −
x<−rpois(20,5) y<−rpois(20,5) df<−data.frame(x,y) df
Output
x y 1 5 4 2 4 5 3 4 3 4 6 2 5 7 5 6 2 4 7 3 8 8 7 5 9 2 3 10 5 3 11 7 7 12 5 4 13 8 2 14 6 4 15 3 8 16 2 9 17 5 2 18 8 5 19 2 8 20 6 6
Loading ggplot2 package and creating a point chart between x and y −
Example
library(ggplot2) ggplot(df,aes(x,y))+geom_point()
Output
Creating a rectangle inside the plot to change the background of the plot −
Example
ggplot(df,aes(x,y))+geom_point()+geom_rect(aes(xmin=3,xmax=7,ymin=0,ymax=10),fill="green",alpha=0.05)
Output
- Related Articles
- How to change plot area margins using ggplot2 in R?
- How to change the border style of a plot using ggplot2 in R?
- How to change the background color of a plot created by using plot function in R?
- How to change the Y-axis values in a bar plot using ggplot2 in R?
- How to change the color of bars of a bar plot using ggplot2 in R?
- How to change the angle of annotated text in plot created by using ggplot2 in R?
- How to change the background color of legend in base R plot?
- How to put the plot title inside the plot using ggplot2 in R?
- How to change the background color of base R plot region?
- How to change legend values in a bar plot created by using ggplot2 in R?
- How to change the aspect ratio of a plot in ggplot2 in R?
- How to change the plot border color of a ggplot2 graph in R?
- How to write plot description outside plot in facetted plot using ggplot2 in R?
- How to change the legend shape using ggplot2 in R?
- How to change the tick size using ggplot2 in R?

Advertisements