0 votes
1 view
in R Programming by (5.1k points)

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 (24.7k points)

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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...