首页 > 解决方案 > 如何在ggplot中添加带有“三类变量”的条纹和图案?

问题描述

我正在尝试绘制具有三个类别的参数(即 RMSE)(第一:土地覆盖;第二:波段,第三:名称)。此处发布了一个类似的问题,但 y 轴不是数据框中的变量。
我试过如下:

library(ggplot2)
library(ggpattern)

df_data <- data.frame(type = c('Forest','Forest','Forest','Forest','Forest','Forest',
                          'Shrub','Shrub','Shrub','Shrub','Shrub','Shrub'),
                 Band = c('A','B','C','A','B','C','A','B','C','A','B','C'),
                 Name = c('N1','N2','N1','N2','N1','N2','N1','N2','N1','N2','N1','N2'),
                 RMSE = c(54.8,58.3,58.7,46.5,42.9,44.7,34.6,39.9,45.1,31.9,33.9,38.3))

df_data$type = factor(df_data$type, levels = c("Forest", "Shrub"))
df_data$Band = factor(df_data$Band, levels = c("A","B","C"))
df_data$Name = factor(df_data$Name, levels = c("N1","N2"))

theme_set(theme_bw())

p1 <- ggplot(df_data, aes(x = type, y = RMSE)) + 
  geom_col_pattern(position = position_dodge(width = 0.95), width = 0.8,
                   aes(pattern_fill = Band, fill = Name, pattern_angle = Band),
                   pattern_density = 0.1, pattern_size = 0.15) + 
  scale_fill_manual(values = c("#80B1D3", "#9CDC82", "#BEBADA", "#FDB462", "#FB8072", "#8DD3C7"))+
  labs(x="", y="RMSE") + ylim(0, 60)

但结果很奇怪。

绘图输出

首先,第二类“Bands”应该用条纹填充(不带颜色),第三类“Name”应该用颜色填充(不带条纹)。这是我要绘制的,类似于这个问题

标签: rggplot2

解决方案


根据@Parfait 的建议,我成功完成了这项工作。

library(ggplot2)
library(ggpattern)
# remotes::install_github("coolbutuseless/ggpattern")

df_data <- data.frame(type = c('Forest','Forest','Forest','Forest','Forest','Forest',
                               'Shrub','Shrub','Shrub','Shrub','Shrub','Shrub'),
                      Band = c('A','B','C','A','B','C','A','B','C','A','B','C'),
                      Name = c('N1','N2','N1','N2','N1','N2','N1','N2','N1','N2','N1','N2'),
                      RMSE = c(54.8,58.3,58.7,46.5,42.9,44.7,34.6,39.9,45.1,31.9,33.9,38.3))

df_data$type = factor(df_data$type, levels = c("Forest", "Shrub"))
df_data$Band = factor(df_data$Band, levels = c("A","B","C"))
df_data$Name = factor(df_data$Name, levels = c("N1","N2"))

theme_set(theme_bw())

ggplot(df_data, aes(x = type, y = RMSE, fill = Name, pattern = Band, pattern_angle = Band)) + 
    geom_col_pattern(position = position_dodge(preserve = "single"),
                   color = "black", pattern_fill = "black",
                   pattern_density = 0.001, pattern_spacing = 0.025, pattern_key_scale_factor = 0.6) + 
    scale_fill_manual(values = c("#80B1D3", "#9CDC82"))+
    scale_pattern_manual(values = c("none", "stripe", "stripe")) +
    scale_pattern_angle_manual(values = c(0, 45, 0)) +
    labs(x="", y="RMSE") + ylim(0, 60) + 
    guides(pattern = guide_legend(override.aes = list(fill = "white")),
           fill = guide_legend(override.aes = list(pattern = "none"))) +
    theme(axis.text.y = element_text(size = 30, hjust = 0.5, color = "black"), 
          axis.text.x = element_text(size = 30, color = "black"),
          axis.title.y = element_text(size = 30, face = "bold", vjust = 1.5),
          legend.title = element_blank(), legend.text = element_text(size = 30, face = "bold"))

情节


推荐阅读