r - 如何在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”应该用颜色填充(不带条纹)。这是我要绘制的图,类似于这个问题。
解决方案
根据@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"))
推荐阅读
- c++ - 对于基于 RB-tree 的 C++ std::set 的插入时间基准,为什么我得到一个常数而不是对数曲线?
- java - csvReader:用两个空格写入csv
- android - Xamarin.Android 布局问题
- sas - 没有第一行的 proc 摘要(总摘要)
- java - java.sql.SQLException:列计数与准备好的语句的第 1 行的值计数不匹配
- javascript - 如何让 VueJS 注意到新添加的 DOM 元素?
- php - 检查MySQL中是否没有数据
- php - 从第 5 个帖子开始,每 10 个帖子显示一次图像
- android - 在 Android 中,如何测试依赖于另一个活动结果的活动?
- linux - Yocto 图像配方和 SRC_URI