Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)
edited by

By default, ggplot2 produces plots with a grey background. How do I change the color of the background of the plot?

For example, a plot produced by the following code:

library(ggplot2)

myplot<-ggplot(data=data.frame(a=c(1,2,3), b=c(2,3,4)), aes(x=a, y=b)) + geom_line()

myplot

1 Answer

0 votes
by
edited by

You can use the following code to change the background color of the plot in ggplot2:

To change the background color of the panel:

library("ggplot2")

data(mtcars)

ggplot(mtcars,aes(disp, qsec)) + geom_line()+

  theme(panel.background = element_rect(fill = 'yellow', colour = 'red'))

image

To change the background color of the plot and not the panel:

ggplot(mtcars,aes(disp, qsec)) + geom_line()+

  theme(plot.background = element_rect(fill = 'yellow', colour = 'red'))

image

Browse Categories

...