我正在尝试用三个类绘制一个参数(即RMSE) (第一类:土地覆盖;第二类:波段;第三类:名称)。类似的问题也发布在here上,但y轴不是dataframe中的变量。
我尝试了以下几种方法:
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)但结果却很奇怪。

首先,第二类“Band”应该填充条纹(不带颜色),第三类"Name“应该填充颜色(不带条纹)。这是我想要绘制的figure,它类似于this question。
发布于 2020-11-28 17:53:09
在@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"))https://stackoverflow.com/questions/64939768
复制相似问题