如何更改使用 ggplot2 绘制的图的背景颜色 [英] How do I change the background color of a plot made with ggplot2

查看:93
本文介绍了如何更改使用 ggplot2 绘制的图的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,ggplot2 生成带有灰色背景的绘图.如何更改绘图背景的颜色?

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

推荐答案

要更改面板的背景颜色,请使用以下代码:

To change the panel's background color, use the following code:

myplot + theme(panel.background = element_rect(fill = 'green', colour = 'red'))

要更改绘图的颜色(但不是面板的颜色),您可以:

To change the color of the plot (but not the color of the panel), you can do:

myplot + theme(plot.background = element_rect(fill = 'green', colour = 'red'))

请参阅此处了解更多主题详情图例、轴和主题的快速参考表.

See here for more theme details Quick reference sheet for legends, axes and themes.

这篇关于如何更改使用 ggplot2 绘制的图的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆