【问题标题】:ggplot2 for grayscale printoutsggplot2 用于灰度打印输出
【发布时间】:2012-11-21 20:07:11
【问题描述】:

ggplot2 为屏幕/彩色打印生成精美的图形,但灰色背景和颜色在将它们打印为灰度时会干扰。为了更好的可读性,我更喜欢禁用灰色背景并使用颜色生成器产生不同的灰色阴影或不同类型的填充笔触来区分组。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    ** 编辑 ** 更新代码:geom_bar 需要 stat

    theme_bw 可能是您所追求的。如果您要绘制具有填充(如条形)的几何图形,scale_fill_grey 函数将使您能够控制灰色阴影。如果您正在绘制具有颜色(例如线或点)的几何图形,scale_colour_grey 函数将为您提供控制权。据我所知, ggplot 不会绘制图案填充。假设您正在绘制条形图,以下内容将在灰色背景上绘制彩色条形图。

    library(ggplot2)
    
    data <- read.table(text = 
    "type Year  Value 
     A    2000  3
     B    2000  10
     C    2000  11
     A    2001  4
     B    2001  5
     C    2001  12", sep = "", header = TRUE)
    
    (p = ggplot(data = data, aes(x = factor(Year), y = Value)) +       
      geom_bar(aes(fill = type), stat="identity", position = "dodge"))
    

    以下将彩色条更改为灰色阴影。请注意,其中一个条在背景中消失了。

    (p = p + scale_fill_grey(start = 0, end = .9))
    

    以下删除灰色背景。

    (p = p + theme_bw())
    

    点有颜色,而不是填充。所以要在点上使用灰色阴影,你需要这样的东西。

    (p = ggplot(data = data, aes(x = factor(Year), y = Value)) +       
      geom_point(aes(colour = type), size = 5) +
      scale_colour_grey(start = 0, end = .9) +
      theme_bw())
    

    【讨论】:

      【解决方案2】:

      其他回答者专注于自动调整线条/条形颜色。但是,在印刷出版物中,我不希望有浅灰色线条等,因为它们难以识别和区分。这是一个自动调整线型的解决方案(直到一天前我才知道这是可能的)。

      library(ggplot2)
      theme_set(theme_bw())
      
      data <- read.table(text = 
      "type Year  Value 
       A    1998  6
       A    1999  8
       A    2000  6
       A    2001  7
       B    1998  4
       B    1999  5
       B    2000  10
       B    2001  5
       C    1998  8
       C    1999  6
       C    2000  9
       C    2001  8", sep = "", header = TRUE)
      
      p <- ggplot(data=data,
                  aes(x = factor(Year),
                      y = Value,
                      linetype = factor(type)))
      p <- p + geom_line(aes(group = factor(type)),
                         size=0.8)
      

      请注意,您可以easily customize ggplot themes。 这是使用my own minimalistic ggplot theme 的替代结果:

      (当导出为 pdf 时,抗锯齿看起来比这里 [in png] 更好。)

      【讨论】:

        【解决方案3】:

        这是您想要的一个简单示例

        library(ggplot2)
        
        data <- read.table(text = 
          "Letter      Year    Value 
         A           1998    5
                        B           1999    10
                        C           2000    15
                        A           2000    7
                        B           2001    15
                        C           2002    20", sep = "", header = TRUE)
        ggplot(data = data, aes(x = factor(Year), y = Value, colour = Letter)) +       
          geom_line(aes(group = Letter)) + scale_colour_grey() +
          theme(panel.background = element_rect(fill='white', colour='black'))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-04-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多