R控制绘图区域颜色 4

今天遇到一个有意思的问题,有人问我,如何生成一个黑白反转的图。

在R中,默认的图都是墨白的。咋弄呢?

其实很简单,把默认值改了就行了。

plot(1:5, 1:5)

plot of chunk unnamed-chunk-1

op <- par(bg="black", col="white", col.axis="white", 
          col.main="white", col.sub="white", col.sub="white")
plot(1:5, 1:5)

plot of chunk unnamed-chunk-1

par(op)

那么,在ggplot2中如何实现呢?

library(ggplot2)
theme_black <- function (base_size = 12, base_family = "") 
{
        theme(line = element_line(colour = "white", size = 0.5, 
                                  linetype = 1, lineend = "butt"), 
            rect = element_rect(fill = "black", colour = "black", 
                                size = 0.5, linetype = 1), 
            text = element_text(family = base_family, 
                                face = "plain", colour = "white", 
                                size = base_size, hjust = 0.5,
                                vjust = 0.5, angle = 0, 
                                lineheight = 0.9),
            panel.grid = element_blank(),
            panel.background = element_rect(fill="black"),
            panel.border = element_rect(color="white", fill=NA),
            axis.line = element_line(color = "white"), 
            axis.text.x=element_text(color="white"),
            axis.text.y=element_text(color="white"), 
            axis.ticks=element_line(color="white")
            )
}
ggplot(aes(x=cyl, y=mpg), data=mtcars) + geom_point(color="white")+ theme_black()

plot of chunk unnamed-chunk-2

4 thoughts on “R控制绘图区域颜色

  1. Reply jmzeng 12月 21,2015 3:49 上午

    why do this ? just for fun ?

  2. Reply 123123 10月 5,2016 9:13 下午

    我想问一个我最近遇到的问题,不显著的点是灰色的,显著的点是红色的,但是灰色的点太多导致红色的点都显示一半,就是说红色的点被灰色的点遮住了,这种情况下该怎么办。谢谢博主。

Leave a Reply

  

  

  

%d 博主赞过: