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

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

问题描述

默认情况下,ggplot2产生灰色背景的图。如何更改剧情背景的颜色?



例如,由以下代码产生的情节:

  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




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

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

要改变图的颜色(但不是面板的颜色),你可以这样做:

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

在这里获取更多主题详情传奇,斧头和主题的快速参考表

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天全站免登陆