如何在ggplot的条形图中添加阴影线,条纹或其他图案或纹理? [英] How can I add hatches, stripes or another pattern or texture to a barplot in ggplot?

查看:1050
本文介绍了如何在ggplot的条形图中添加阴影线,条纹或其他图案或纹理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我同时具有序数变量和分类变量的数据:

Suppose I have data with both an ordinal variable and a categorical variable:

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)美学映射一起使用,以可视化这些变量之间的关系.

One could use ggplot and geom_bar with x, fill and alpha (or color) aesthetic mappings to visualize the relationship between these variables.

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并不理想.更好的替代方法可能是应用诸如条纹或交叉影线的图案.

However, alpha and color are not ideal. A better alternative might be to apply a pattern such as stripes or a crosshatch.

该问题的可接受答案 10年前说要使用颜色,最受好评的答案(虽然很聪明)使用了100行以上的代码.

The accepted answer to this question from over 10 years ago says to use colors, and the most upvoted answer (while clever) uses over 100 lines of code.

此问题得到了一些好评,但没有新的答案.

This question received some upvotes but no new answers.

除了在这里可以看到的模式之外,还有其他更好的选择吗?

Is there any better alternative to adding a pattern such as can be seen here?

推荐答案

一种方法是使用

One approach is to use the ggpattern package written by Mike FC (no affiliation):

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组合连续变量和分类变量的示例:

The package appears to support a number of common geometries. Here is an example of using geom_tile to combine a continuous variable with a categorical variable:

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")))

这篇关于如何在ggplot的条形图中添加阴影线,条纹或其他图案或纹理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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