【问题标题】:How to restrict the ggplot legend rows (with guide_legend) when using ggpattern?使用 ggpattern 时如何限制 ggplot 图例行(使用 guide_legend)?
【发布时间】:2023-02-22 08:29:09
【问题描述】:

我正在尝试将 ggplot(带有 ggpattern)的图例限制为 2 行。当你只有一种美学可以玩时,它或多或少是直截了当的here。此外,this 是一个很好的例子,说明如何使用不止一种美学(例如,形状和颜色)来操纵图例。然而,ggpattern 将这些融合成看似一种美学,并且对由此产生的图例的操纵并不那么简单。

要在底部重现的数据和会话信息。

library(ggplot2)
library(ggpattern)

p0<-ggplot(Dat)+
  geom_col(
    aes(y=Prop,x=MHW,fill=Consumer),
    color="black",alpha=0.8
  )+
  facet_wrap(~Donor,nrow=2,scales="free")+
  theme_classic()+
  theme(
    axis.text.x = element_text(angle=90),
    legend.position = "bottom"
  )+
  guides(
    fill = guide_legend(nrow = 2)
  )

png("Test0.png",width=12,height=9,units = "in",res=300,type = "cairo")
p0
dev.off()

正如预期的那样,图例分为两行:

现在让我们更改为 ggpattern(已更改的行用 ## 指示)

p1<-ggplot(Dat)+
  geom_col_pattern(                                  ##
    aes(y=Prop,x=MHW,fill=Consumer,
        pattern=Consumer,pattern_fill=Consumer),     ##
    color="black",alpha=0.8,
    pattern_key_scale_factor=.3                      ##
  )+
  facet_wrap(~Donor,nrow=2,scales="free")+
  theme_classic()+
  theme(
    axis.text.x = element_text(angle=90),
    legend.position = "bottom"
  )+
  guides(
    # fill = guide_legend(nrow = 2),
    pattern_fill = guide_legend(nrow = 2)            ##
  )+
  scale_pattern_manual(                              ##
    values=c(rep(c('circle','stripe', 'crosshatch'),5)) ##
  ) 

png("Test1.png",width=12,height=9,units = "in",res=300,type = "cairo")
p1
dev.off()

然而,现在图例不限于两行。如果您只尝试 fill 美学或同时尝试两者:

  guides(
    fill = guide_legend(nrow = 2),
    pattern_fill = guide_legend(nrow = 2) 
  )+

  guides(
    fill = guide_legend(nrow = 2) #,
    # pattern_fill = guide_legend(nrow = 2) 
  )+

两者都没有帮助将图例限制为两行。如何限制图例?

数据

Dat<-structure(list(Detritus = c(195.378055295869, 195.378055295869, 
195.378055295869, 195.378055295869, 195.378055295869, 195.378055295869, 
195.378055295869, 195.378055295869, 195.378055295869, 195.378055295869, 
66.9513777221605, 66.9513777221605, 66.9513777221605, 66.9513777221605, 
66.9513777221605, 66.9513777221605, 66.9513777221605, 66.9513777221605, 
66.9513777221605, 66.9513777221605, 202.062496802138, 202.062496802138, 
202.062496802138, 202.062496802138, 202.062496802138, 202.062496802138, 
202.062496802138, 202.062496802138, 202.062496802138, 202.062496802138, 
1.10145527938025, 1.10145527938025, 1.10145527938025, 1.10145527938025, 
1.10145527938025, 1.10145527938025, 1.10145527938025, 1.10145527938025, 
1.10145527938025, 1.10145527938025, 0.095218385013956, 0.095218385013956, 
0.095218385013956, 0.095218385013956, 0.095218385013956, 0.095218385013956, 
0.095218385013956, 0.095218385013956, 0.095218385013956, 0.095218385013956, 
0.696299572709905, 0.696299572709905, 0.696299572709905, 0.696299572709905, 
0.696299572709905, 0.696299572709905, 0.696299572709905, 0.696299572709905, 
0.696299572709905, 0.696299572709905, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0.00476602121672308, 0.00476602121672308, 0.00476602121672308, 
0.00476602121672308, 0.00476602121672308, 0.00476602121672308, 
0.00476602121672308, 0.00476602121672308, 0.00476602121672308, 
0.00476602121672308, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110.223171189265, 
110.223171189265, 110.223171189265, 110.223171189265, 110.223171189265, 
110.223171189265, 110.223171189265, 110.223171189265, 110.223171189265, 
110.223171189265, 0.054931677571981, 0.054931677571981, 0.054931677571981, 
0.054931677571981, 0.054931677571981, 0.054931677571981, 0.054931677571981, 
0.054931677571981, 0.054931677571981, 0.054931677571981, 0.000772862445925245, 
0.000772862445925245, 0.000772862445925245, 0.000772862445925245, 
0.000772862445925245, 0.000772862445925245, 0.000772862445925245, 
0.000772862445925245, 0.000772862445925245, 0.000772862445925245, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 329.312477609154, 329.312477609154, 
329.312477609154, 329.312477609154, 329.312477609154, 329.312477609154, 
329.312477609154, 329.312477609154, 329.312477609154, 329.312477609154, 
3.06883722794725, 3.06883722794725, 3.06883722794725, 3.06883722794725, 
3.06883722794725, 3.06883722794725, 3.06883722794725, 3.06883722794725, 
3.06883722794725, 3.06883722794725, 77.6283061274114, 77.6283061274114, 
77.6283061274114, 77.6283061274114, 77.6283061274114, 77.6283061274114, 
77.6283061274114, 77.6283061274114, 77.6283061274114, 77.6283061274114, 
0.276194323464863, 0.276194323464863, 0.276194323464863, 0.276194323464863, 
0.276194323464863, 0.276194323464863, 0.276194323464863, 0.276194323464863, 
0.276194323464863, 0.276194323464863, 0.647536119803539, 0.647536119803539, 
0.647536119803539, 0.647536119803539, 0.647536119803539, 0.647536119803539, 
0.647536119803539, 0.647536119803539, 0.647536119803539, 0.647536119803539, 
1.11133139200563, 1.11133139200563, 1.11133139200563, 1.11133139200563, 
1.11133139200563, 1.11133139200563, 1.11133139200563, 1.11133139200563, 
1.11133139200563, 1.11133139200563, 0.0268967426691688, 0.0268967426691688, 
0.0268967426691688, 0.0268967426691688, 0.0268967426691688, 0.0268967426691688, 
0.0268967426691688, 0.0268967426691688, 0.0268967426691688, 0.0268967426691688, 
0.622290423005387, 0.622290423005387, 0.622290423005387, 0.622290423005387, 
0.622290423005387, 0.622290423005387, 0.622290423005387, 0.622290423005387, 
0.622290423005387, 0.622290423005387, 0.359791910433618, 0.359791910433618, 
0.359791910433618, 0.359791910433618, 0.359791910433618, 0.359791910433618, 
0.359791910433618, 0.359791910433618, 0.359791910433618, 0.359791910433618, 
184.29970071925, 184.29970071925, 184.29970071925, 184.29970071925, 
184.29970071925, 184.29970071925, 184.29970071925, 184.29970071925, 
184.29970071925, 184.29970071925, 0.0146353418211845, 0.0146353418211845, 
0.0146353418211845, 0.0146353418211845, 0.0146353418211845, 0.0146353418211845, 
0.0146353418211845, 0.0146353418211845, 0.0146353418211845, 0.0146353418211845, 
0.000715875766578085, 0.000715875766578085, 0.000715875766578085, 
0.000715875766578085, 0.000715875766578085, 0.000715875766578085, 
0.000715875766578085, 0.000715875766578085, 0.000715875766578085, 
0.000715875766578085, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Fishery = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0), Consumer = c("Zooplankton", "Zooplankton", 
"Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", 
"Zooplankton", "Zooplankton", "Zooplankton", "Gelatinous", "Gelatinous", 
"Gelatinous", "Gelatinous", "Gelatinous", "Gelatinous", "Gelatinous", 
"Gelatinous", "Gelatinous", "Gelatinous", "Krill", "Krill", "Krill", 
"Krill", "Krill", "Krill", "Krill", "Krill", "Krill", "Krill", 
"Forage Fish", "Forage Fish", "Forage Fish", "Forage Fish", "Forage Fish", 
"Forage Fish", "Forage Fish", "Forage Fish", "Forage Fish", "Forage Fish", 
"Misc. Fish", "Misc. Fish", "Misc. Fish", "Misc. Fish", "Misc. Fish", 
"Misc. Fish", "Misc. Fish", "Misc. Fish", "Misc. Fish", "Misc. Fish", 
"Juvenile Fish", "Juvenile Fish", "Juvenile Fish", "Juvenile Fish", 
"Juvenile Fish", "Juvenile Fish", "Juvenile Fish", "Juvenile Fish", 
"Juvenile Fish", "Juvenile Fish", "Rockfish", "Rockfish", "Rockfish", 
"Rockfish", "Rockfish", "Rockfish", "Rockfish", "Rockfish", "Rockfish", 
"Rockfish", "Jack Mackerel", "Jack Mackerel", "Jack Mackerel", 
"Jack Mackerel", "Jack Mackerel", "Jack Mackerel", "Jack Mackerel", 
"Jack Mackerel", "Jack Mackerel", "Jack Mackerel", "Hake", "Hake", 
"Hake", "Hake", "Hake", "Hake", "Hake", "Hake", "Hake", "Hake", 
"Benthic", "Benthic", "Benthic", "Benthic", "Benthic", "Benthic", 
"Benthic", "Benthic", "Benthic", "Benthic", "Seabirds", "Seabirds", 
"Seabirds", "Seabirds", "Seabirds", "Seabirds", "Seabirds", "Seabirds", 
"Seabirds", "Seabirds", "Mammals", "Mammals", "Mammals", "Mammals", 
"Mammals", "Mammals", "Mammals", "Mammals", "Mammals", "Mammals", 
"Fishery", "Fishery", "Fishery", "Fishery", "Fishery", "Fishery", 
"Fishery", "Fishery", "Fishery", "Fishery", "Zooplankton", "Zooplankton", 
"Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", 
"Zooplankton", "Zooplankton", "Zooplankton", "Gelatinous", "Gelatinous", 
"Gelatinous", "Gelatinous", "Gelatinous", "Gelatinous", "Gelatinous", 
"Gelatinous", "Gelatinous", "Gelatinous", "Krill", "Krill", "Krill", 
"Krill", "Krill", "Krill", "Krill", "Krill", "Krill", "Krill", 
"Forage Fish", "Forage Fish", "Forage Fish", "Forage Fish", "Forage Fish", 
"Forage Fish", "Forage Fish", "Forage Fish", "Forage Fish", "Forage Fish", 
"Misc. Fish", "Misc. Fish", "Misc. Fish", "Misc. Fish", "Misc. Fish", 
"Misc. Fish", "Misc. Fish", "Misc. Fish", "Misc. Fish", "Misc. Fish", 
"Juvenile Fish", "Juvenile Fish", "Juvenile Fish", "Juvenile Fish", 
"Juvenile Fish", "Juvenile Fish", "Juvenile Fish", "Juvenile Fish", 
"Juvenile Fish", "Juvenile Fish", "Rockfish", "Rockfish", "Rockfish", 
"Rockfish", "Rockfish", "Rockfish", "Rockfish", "Rockfish", "Rockfish", 
"Rockfish", "Jack Mackerel", "Jack Mackerel", "Jack Mackerel", 
"Jack Mackerel", "Jack Mackerel", "Jack Mackerel", "Jack Mackerel", 
"Jack Mackerel", "Jack Mackerel", "Jack Mackerel", "Hake", "Hake", 
"Hake", "Hake", "Hake", "Hake", "Hake", "Hake", "Hake", "Hake", 
"Benthic", "Benthic", "Benthic", "Benthic", "Benthic", "Benthic", 
"Benthic", "Benthic", "Benthic", "Benthic", "Seabirds", "Seabirds", 
"Seabirds", "Seabirds", "Seabirds", "Seabirds", "Seabirds", "Seabirds", 
"Seabirds", "Seabirds", "Mammals", "Mammals", "Mammals", "Mammals", 
"Mammals", "Mammals", "Mammals", "Mammals", "Mammals", "Mammals", 
"Fishery", "Fishery", "Fishery", "Fishery", "Fishery", "Fishery", 
"Fishery", "Fishery", "Fishery", "Fishery"), Donor = c("Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic", "Zooplankton", 
"Gelatinous", "Krill", "Forage Fish", "Misc. Fish", "Juvenile Salmon", 
"Rockfish", "Hake", "Juvenile Fish", "Benthic"), Prop = c(1417.16886531397, 
21.8852589743467, 29.5205335617856, 0, 0, 0, 0, 0, 0, 1.27731039677243, 
327.50830866045, 51.1400615337213, 5.29647698895142, 0, 0, 0, 
0, 0, 0, 0, 432.093078201989, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51.0494310878386, 
1.85177307192677, 53.6791923903298, 1.22426881699568, 0.447678212520347, 
0.0108188062979273, 0, 0, 0.712122477894116, 0.129836991126026, 
3.01381479489865, 0.429375380502875, 9.55847366160016, 6.55116783323041, 
0.840840438605795, 0.046249710322369, 0.322652592269059, 1.40659738778568, 
2.35895229863508, 5.69632701706482, 40.305644317743, 0.514963426130939, 
2.91947031365883, 0.121615077889678, 0.00907555165297567, 0.00073539519140489, 
0, 0, 1.22469472688476, 0.545024905072436, 2.04957719585246, 
0.0118268711741824, 3.20784444214808, 2.10466339517761, 0.485237400053026, 
0.0144050814181559, 0.153615511374632, 0.159524411882556, 0.414147440805842, 
0.519689856899177, 0.430867611693594, 0.00636979594256325, 1.60749793559994, 
0.537926031684436, 0, 0.00421928392766898, 0, 0, 0.135549144211162, 
0.0016863834068481, 2.90620227916775, 2.53134901312395, 35.6129076971812, 
12.0952219885951, 0.422734254350647, 0.0843215249443493, 0, 0, 
4.10550720195767, 1.91790098672496, 37.1197406894923, 0, 25.3967690693007, 
0.18826756476134, 0.0263827559709179, 0.00156361299544017, 0.00284374656952413, 
0, 0.732307567289515, 25.2546400500606, 0.368626704299541, 0.000420231357235188, 
0.120199285044128, 4.87345318075009, 0.124373759218538, 0.0313985827066985, 
0.0206980939802894, 0.0230888380542323, 0, 0.00109874331718929, 
0.478519185765927, 0.339160466140748, 1.49993377656359, 3.27450722365355, 
1.10728850131732, 0.0224745992469266, 0.17760410033594, 0.382116804399672, 
0.10544737536346, 0.34064263571257, 0, 0, 0, 0.561734361421642, 
0.262123906500932, 0, 0.0520197154380621, 3.63666569668098, 0, 
0.17085404555258, 1480.98880868256, 0.383980696580429, 36.6319663034093, 
0, 0, 0, 0, 0, 0, 2.11127300809657, 208.319040675764, 0.273992987508079, 
0.0891092907137129, 0, 0, 0, 0, 0, 0, 0, 215.954490957687, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 54.9286009257789, 0.469161558508563, 
49.7201823328684, 0.776011196128181, 0.460993201935953, 0.0138672177550104, 
0, 0, 1.68378528625113, 1.4659268303144, 4.09811728002028, 0.680189846947001, 
11.6133184499861, 7.92139441496285, 1.66589001872885, 0.0863688062908657, 
0.563271711372325, 1.7249508532637, 2.2469144494425, 7.49849908468619, 
55.3998578378037, 0.138117020761687, 3.76784428776134, 0.157589368848881, 
0.00192934846852898, 0.00103455463884516, 0, 0, 2.37768234206405, 
0.810825459331864, 3.45834639799013, 0.0846596910083975, 4.30189127867907, 
1.18438711744062, 0.758357275742268, 0.028135073209926, 0.270450419403965, 
0.190235699660435, 0.818734910162449, 1.00450088431345, 10.1271816346645, 
0.00965720383832612, 18.3577682768183, 3.36083127325828, 0.210530386674747, 
0.0969152790969377, 0.0176366626588672, 0.00344950826715526, 
0.702115148690833, 1.94932045096121, 6.41358861842098, 0.0325668049210667, 
24.1805699214855, 8.19422969521942, 0.339716093070585, 0.300324597784372, 
0, 0, 2.71099913452328, 3.14003215421284, 84.3060340111592, 0, 
46.9831368242163, 0.00351634488635138, 0.00995667063934028, 2.96446503412003e-08, 
0.0101229566324351, 0, 0.916063930481584, 58.6764716462903, 0.166829601836717, 
5.58184727333846e-05, 0.0755490452347125, 1.68692379472434, 0.0189872627761935, 
0.0288880616347642, 0.000946039170233896, 0.00767217984651186, 
0.076294623199603, 0.00057506209452, 0.5227132252172, 0.0619482744787457, 
1.84886149950565, 2.35514009373859, 0.884300572657665, 0.0419638868682333, 
0.128465917860897, 0.295031688941958, 0.12000517842897, 0.470381017621623, 
0, 0, 0, 0.0207034634950007, 0.214221943642372, 0, 0.0515155760949915, 
2.31093873862259, 0, 0.144893998318959), MHW = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), levels = c("pre", "post"), class = "factor")), row.names = c(NA, 
-260L), class = c("tbl_df", "tbl", "data.frame"))

会话信息

> sessionInfo()
R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_1.0.9     ggpattern_1.0.0 ggplot2_3.4.0   tidyr_1.2.0    

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8.3       pillar_1.7.0       compiler_4.2.2     class_7.3-20      
 [5] tools_4.2.2        memoise_2.0.1      lifecycle_1.0.3    tibble_3.1.7      
 [9] gtable_0.3.0       pkgconfig_2.0.3    rlang_1.0.6        DBI_1.1.2         
[13] cli_3.6.0          rstudioapi_0.13    gridpattern_1.0.1  fastmap_1.1.0     
[17] e1071_1.7-11       withr_2.5.0        generics_0.1.2     vctrs_0.5.1       
[21] classInt_0.4-7     grid_4.2.2         tidyselect_1.1.2   glue_1.6.2        
[25] sf_1.0-7           R6_2.5.1           fansi_1.0.3        purrr_0.3.4       
[29] farver_2.1.0       magrittr_2.0.3     units_0.8-0        scales_1.2.0      
[33] ellipsis_0.3.2     assertthat_0.2.1   colorspace_2.0-3   labeling_0.4.2    
[37] KernSmooth_2.23-20 utf8_1.2.2         proxy_0.4-27       munsell_0.5.0     
[41] cachem_1.0.6       crayon_1.5.1    

【问题讨论】:

    标签: ggplot2 plot ggpattern


    【解决方案1】:

    虽然您已经指定了fillpattern_fill,但您唯一缺少的和您实际需要指定的唯一东西是pattern(尽管您不会因包含所有三个而收到错误):

    guides(pattern = guide_legend(nrow = 2))+

    p2<-ggplot(Dat)+
      geom_col_pattern(
        aes(y=Prop,x=MHW,fill=Consumer,
            pattern=Consumer,pattern_fill=Consumer),
        color="black",alpha=0.8,
        pattern_key_scale_factor=.3
      )+
      facet_wrap(~Donor,nrow=2,scales="free")+
      theme_classic()+
      theme(
        axis.text.x = element_text(angle=90),
        legend.position = "bottom"
      )+
      guides(
        pattern = guide_legend(nrow = 2)
      )+
      scale_pattern_manual(
        values=c(rep(c('circle','stripe', 'crosshatch'),5))
      ) 
    
    png("Test2.png",width=12,height=9,units = "in",res=300,type = "cairo")
    p2
    dev.off()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 2021-10-10
      相关资源
      最近更新 更多