【问题标题】:How can I add stripes and pattern with "three class variable" in ggplot?如何在ggplot中添加带有“三类变量”的条纹和图案?
【发布时间】:2020-11-21 04:15:42
【问题描述】:

我正在尝试绘制具有三个类别(第一:土地覆盖;第二:波段,第三:名称)的参数(即 RMSE)。 here 发布了一个类似的问题,但 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”应该用颜色填充(不带条纹)。这是我要绘制的figure,类似于this question

【问题讨论】:

  • 请定义奇怪RMSE 的长度和内容是什么?每个r 标签(悬停或单击查看):请提供最小和reproducible example(s) 以及所需的输出。对数据使用dput(),并使用library() 调用指定所有非基础包。并且请避免为被公司代理拒绝 GDrive 路径的读者或具有死链接的未来读者提供数据链接。
  • 很难将您的数据输入我的 R。请查看 stackoverflow.com/help/minimal-reproducible-example。您可以对数据进行子集化(也许 5 行就足够了?)并使用dput(dt_data) 添加它。将dput 结果粘贴到您的问题中。祝你好运!
  • @Parfait 谢谢你,Parfait 和 Eric Krantz。感谢您的善意提醒。我已经更新了我的问题并使其清楚易懂。
  • 情节中的二等三等在哪里?他们是传奇系列,A-C和N1-N6吗?您是说传说应该与后者没有条纹相反吗?也许使用图像软件(在 R 之外)来显示理想的情节或圈出问题区域。另外,RMSE 是数据框中的一列,这怎么可能是真的:但是 y 轴不是数据框中的变量
  • @Parfait 很抱歉这个困惑的问题。是的。 第二和第三类是传奇系列,A-C和N1-N6。图例 A-C 只能用条形填充。图例 N1-N6 不应该有条纹。在这种情况下,我想使用 RMSE 作为 y 轴。但在我发布link 的类似问题中,它的 y 轴不是数据框中的列。

标签: r ggplot2


【解决方案1】:

我在@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"))

Plots

【讨论】:

    猜你喜欢
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 2022-06-22
    • 2021-07-31
    • 1970-01-01
    • 2021-07-13
    • 2016-03-06
    相关资源
    最近更新 更多