【问题标题】:How can I add hatches, stripes or another pattern or texture to a barplot in ggplot?如何在 ggplot 的条形图中添加阴影、条纹或其他图案或纹理?
【发布时间】:2020-10-05 03:45:19
【问题描述】:

假设我有一个包含序数变量和分类变量的数据:

set.seed(35)
df <- data.frame(Class = factor(rep(c(1,2),times = 80), labels = c("Math","Science")),
                 StudyTime = factor(sort(sample(1:4, 16, prob = c(0.25,0.3,0.3,0.15), replace = TRUE)),labels = c("<5","5-10","10-20",">20")),
                 Nerd = factor(sapply(rep(c(0.1,0.3,0.5,0.8),c(30,50,50,30)), function(x)sample(c("Nerd","NotNerd"),size = 1, prob = c(x,1-x))),levels = c("NotNerd","Nerd")))

可以使用ggplotgeom_barxfillalpha(或color)美学映射来可视化这些变量之间的关系。

ggplot(data = df, aes(x = Class, fill = StudyTime, alpha = Nerd)) + 
  geom_bar(position = "dodge", color = "black") + 
  scale_alpha_manual(values = c(Nerd = 0.5, NotNerd = 1)) +
  scale_fill_manual(values = colorRampPalette(c("#0066CC","#FFFFFF","#FF8C00"))(4)) +
  labs(x = "Class", y = "Number of Students", alpha = "Nerd?") +
  theme(legend.key.height = unit(1, "cm"))

但是,alphacolor 并不理想。更好的选择可能是应用条纹或交叉影线等图案。

10 多年前对this question 的公认答案是使用颜色,而the most upvoted answer(虽然聪明)使用超过 100 行代码。

This question 收到了一些赞成票,但没有新的答案。

有没有更好的选择来添加一个模式,比如可以在这里看到?

【问题讨论】:

    标签: r ggplot2 bar-chart


    【解决方案1】:

    一种方法是使用 Mike FC(无从属关系)编写的 ggpattern 包:

    library(ggplot2)
    #remotes::install_github("coolbutuseless/ggpattern")
    library(ggpattern)
    ggplot(data = df, aes(x = Class, fill = StudyTime, pattern = Nerd)) +
      geom_bar_pattern(position = position_dodge(preserve = "single"),
                       color = "black", 
                       pattern_fill = "black",
                       pattern_angle = 45,
                       pattern_density = 0.1,
                       pattern_spacing = 0.025,
                       pattern_key_scale_factor = 0.6) + 
      scale_fill_manual(values = colorRampPalette(c("#0066CC","#FFFFFF","#FF8C00"))(4)) +
      scale_pattern_manual(values = c(Nerd = "stripe", NotNerd = "none")) +
      labs(x = "Class", y = "Number of Students", pattern = "Nerd?") + 
      guides(pattern = guide_legend(override.aes = list(fill = "white")),
             fill = guide_legend(override.aes = list(pattern = "none")))
    

    该软件包似乎支持许多常见的几何形状。以下是使用geom_tile 将连续变量与分类变量组合的示例:

    set.seed(40)
    df2 <- data.frame(Row = rep(1:9,times=9), Column = rep(1:9,each=9),
                       Evaporation = runif(81,50,100),
                       TreeCover = sample(c("Yes", "No"), 81, prob = c(0.3,0.7), replace = TRUE))
    
    ggplot(data=df2, aes(x=as.factor(Row), y=as.factor(Column),
                         pattern = TreeCover, fill= Evaporation)) +
      geom_tile_pattern(pattern_color = NA,
                        pattern_fill = "black",
                        pattern_angle = 45,
                        pattern_density = 0.5,
                        pattern_spacing = 0.025,
                        pattern_key_scale_factor = 1) +
      scale_pattern_manual(values = c(Yes = "circle", No = "none")) +
      scale_fill_gradient(low="#0066CC", high="#FF8C00") +
      coord_equal() + 
      labs(x = "Row",y = "Column") + 
      guides(pattern = guide_legend(override.aes = list(fill = "white")))
    

    【讨论】:

    • 有趣的包,谢谢分享。我个人觉得条纹条形图并不是很有帮助,但瓷砖上的演示是一个非常令人信服的模式用例,IMO :) +1
    • 隐藏的宝石在这里:使用guides(fill=guide_legend(override.aes=list(pattern="none")))从填充指南中删除图案
    • 有些期刊需要黑白图形和图案,而不是在黑白之间填充渐变,如果您想继续使用 ggplot2 绘制图形,像 geom_bar_pattern 这样的东西绝对至关重要。
    猜你喜欢
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    相关资源
    最近更新 更多