我在使用ggplot和ggpattern绘制一组带有底纹的柱状图时,希望在最后绘制图片的图例时,仅仅表示颜色的图例,而不显示底纹的图例;问题如下:
我在对单张图片进行绘制的时候,尝试在 scale_pattern_continuous() 中添加 guide = 'none' 这一参数,虽然成功了,但是当我使用theme对图中的其他信息进行修改之后,图例上又有底纹了
单张图片代码:
ggplot() +
geom_bar_pattern(data = plt_data, aes(x = period, y = slope, fill = events, pattern = pat),
position = 'dodge',
stat = 'identity',
color = 'white',
pattern_fill = 'grey',
pattern_color = 'NA')+
scale_pattern_continuous(choices = c( 'none','crosshatch'), guide = 'none')+
scale_fill_npg(name = '', labels = c('DFAA', 'Drought', 'Flood'))+
labs(y = 'Data', x = 'Period', subtitle = '(e)') +
theme(#legend.position = 'none',
panel.background = element_blank(),
panel.border = element_rect(size=2,fill = 'transparent'),
axis.text = element_text(color='black',size = 40),
axis.title = element_text(size = 45),
plot.subtitle = element_text(size = 50),
legend.text = element_text(size = 28))
之后我尝试使用分面的画法对组图进行绘制,但是出现了另一个问题,分面绘制的图例过于紧凑,不够美观,我在 theme() 中使用 legend.spacing.y() 进行调整,但是仍然失败,图例紧凑的情况没有改善
绘图代码如下:
pp_facet <- ggplot() +
geom_bar_pattern(data = plt_all_data, aes(x = period, y = slope, fill = events, pattern = pat),
position = 'dodge',
stat = 'identity',
color = 'white',
pattern_fill = 'grey',
pattern_color = 'NA')+
scale_pattern_continuous(choices = c( 'none','crosshatch'), guide = 'none')+
scale_x_discrete(labels = c('1961-2022', '1961-1991', '1992-2022'))+
labs(y = 'Event trend (day/year)', x = 'Period') +
facet_wrap(group~., nrow = 2) +
theme(#legend.position = 'none',
strip.background = element_blank(), #去除标题框背景
strip.text.x=element_text(size = 45),
panel.background = element_blank(),
panel.border = element_rect(size=2,fill = 'transparent'),
axis.text = element_text(color='black',size = 40),
axis.title = element_text(size = 45),
plot.subtitle = element_text(size = 50),
legend.text = element_text(size = 50),
legend.position = c(0.75,0.2),
axis.text = element_blank(),
axis.ticks = element_blank())
绘图所需要的数据如下分享:
链接:https://pan.baidu.com/s/1PzyjripKczHlz70wHdq0sw?pwd=o5fc
提取码:o5fc
向各位大佬求解!