- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 color of gridlines of a ggplot2 graph in R?
To change the color of gridlines of a ggplot2 graph in R, we can use theme function with panel.grid.major and panel.grid.minor arguments where we can set the minor and major gridlines color of the plot panel to desired color.
To understand how it can be done, check out the below Example.
Example
Following snippet creates a sample data frame −
x<-sample(0:9,20,replace=TRUE) y<-sample(0:9,20,replace=TRUE) df<-data.frame(x,y) df
The following dataframe is created
x y 1 5 7 2 7 5 3 1 5 4 5 9 5 6 4 6 2 5 7 4 2 8 7 7 9 6 7 10 7 2 11 1 3 12 2 9 13 7 0 14 6 9 15 5 7 16 6 6 17 5 9 18 5 7 19 4 5 20 4 4
To load ggplot2 package and create scatterplot between x and y on the above created data frame, add the following code to the above snippet −
x<-sample(0:9,20,replace=TRUE) y<-sample(0:9,20,replace=TRUE) df<-data.frame(x,y) library(ggplot2) ggplot(df,aes(x,y))+geom_point()
Output
If you execute all the above given snippets as a single program, it generates the following Output −
To create scatterplot between x and y with red color gridlines on the above created data frame, add the following code to the above snippet −
x<-sample(0:9,20,replace=TRUE) y<-sample(0:9,20,replace=TRUE) df<-data.frame(x,y) library(ggplot2) ggplot(df,aes(x,y))+geom_point()+theme(panel.grid.major=element_line(colour="re d"),panel.grid.minor=element_line(colour="red"))
Output
If you execute all the above given snippets as a single program, it generates the following Output −
- Related Articles
- How to change the plot border color of a ggplot2 graph in R?
- How to change the bars color to grey shade of a bar graph created by using ggplot2 in R?
- Create darker gridlines in theme_bw for a ggplot2 graph in R.
- How to change the title size of a graph using ggplot2 in R?
- How to change the gridlines of Y-axis on a chart created by using ggplot2 in R?
- How to change the color of points in a scatterplot using ggplot2 in R?
- How change the color of facet title using ggplot2 in R?
- How to change the color of points for ggplot2 scatterplot using color brewer in R?
- How to change the color of bars of a bar plot using ggplot2 in R?
- Change the color of X-axis line for a graph using ggplot2.
- How to change the color of X-axis label using ggplot2 in R?
- How to change the color of lines for a line chart using ggplot2 in R?
- How to change the border color of points in a scatterplot created with ggplot2 in R?
- Change the color of legend element border using ggplot2 in R.
- How to create a bar graph using ggplot2 without horizontal gridlines and Y-axes labels in R?
