R 使用多个ggplot2 geom_纵横参数时重置颜色美学

R 使用多个ggplot2 geom_纵横参数时重置颜色美学,r,ggplot2,tidyverse,R,Ggplot2,Tidyverse,我试图控制对geom_crosbar的两个单独调用的颜色,第一个使用绿色,第二个使用蓝色。然而,我从第二个geom_纵横制呼叫中得到警告,“填充”的比例已经存在: 警告:为“填充”添加另一个刻度,将替换现有刻度 规模 下面是我的代码示例: my.data %>% ggplot(aes(site, npp_nofert)) + geom_crossbar(aes(ymin=npp_nofert-npp.sd_nofert,ymax=npp_nofert+npp.sd_no

我试图控制对geom_crosbar的两个单独调用的颜色,第一个使用绿色,第二个使用蓝色。然而,我从第二个geom_纵横制呼叫中得到警告,“填充”的比例已经存在:

警告:为“填充”添加另一个刻度,将替换现有刻度 规模

下面是我的代码示例:

   my.data %>%
   ggplot(aes(site, npp_nofert)) +
    geom_crossbar(aes(ymin=npp_nofert-npp.sd_nofert,ymax=npp_nofert+npp.sd_nofert, 
                      fatten=1.0,fill=period),position='dodge', alpha=0.5)   +
    scale_fill_brewer(palette="Greens") + 
    #labs(y=expression(paste("MMM %",Delta," (+/- 1",sigma,")")), x="", fill="", title="") + theme_bw() + 
    labs(y="",x="", fill="", title="") + theme_bw() + 
    theme(legend.key.size=unit(1.0,"cm"),legend.direction="horizontal",legend.position=c(0.3,0.05), 
          axis.text.x=element_blank(),axis.ticks.x=element_blank(), 
          plot.title=element_text(size=12,margin=margin(t=5,b=-20)), legend.spacing=unit(0,"cm"),
          text = element_text(size=15)) + 
   new_scale_fill() +
   geom_crossbar(aes(ymin=npp_fert-npp.sd_fert,ymax=npp_fert+npp.sd_fert, fatten=1.0,fill=period), 
                 position='dodge',alpha=0.5)   +
   scale_fill_brewer(palette="Blues")
和示例输出:

不幸的是,我无法输出数据,因为我没有权限这样做。 如何将第一个绘图设置为绿色,第二个绘图设置为蓝色?另外,刚刚注意到对alpha的调用在图例中。如何消除这种情况


注:1980年至1999年期间,只有一个地块,即没有处理,因此该期间不会有重叠地块。x轴代表研究地点,我可以稍后修复标签

实现这一点的一般方法是使用ggnewscale包,它允许您在绘图过程中的某个点“重置”美学

由于没有数据可供使用,我将编造一些虚拟数据,这些数据与您上面显示的内容有模糊的相似之处

图书馆GGPLOT2 图书馆GG新闻量表
df所以我决定我在情节中使用的方法看起来很糟糕。IMO,更好的解决方案是使用geom_交叉杆和geom_点范围

下面是一个使用teubrand提供的数据的示例:

library(ggplot2)
library(ggnewscale)

df <- data.frame(
  x = 1:5,
  blue_low = 1:5,
  blue_mid = 2:6,
  blue_high = 3:7,
  green_low = 0:4,
  green_mid = 2:6,
  green_high = 4:8
)

ggplot(df, aes(x = 1, group = x)) +
   geom_crossbar(aes(ymin = green_low, y = green_mid, ymax = green_high, 
                     fill = as.factor(x)),
                 position = "dodge", alpha = 0.8) +
   scale_fill_brewer(palette = "Greens") +
   new_scale_fill() + # Important to put this after you defined the first scale
   geom_pointrange(aes(ymin = blue_low, y = blue_mid, ymax = blue_high, 
                     fill = as.factor(x)), # paste to differentiate scale
                 position = position_dodge(width=0.9), color="gray30") +
   scale_fill_brewer(palette = "Blues")

伟大的这对我在ggplot和geom_crossbar中的参数进行了修改。