如何在条形图中扩展 geom_bspline?

How to extend geom_bspline in a bar chart?

我研究了 19 世纪 (=x) 几十年间的物种灭绝 (=y)。我正在绘制一条平滑的样条曲线,它显示了灭绝物种的累积值,横跨一组显示每十年绝对值的条形图。现在,当值出现在 X 位置的中心时,平滑样条曲线在最后一个柱的中心结束。由于这在视觉上令人不快,我想延长样条曲线,使其继续延伸到图的边界。

我尝试了 position 的说法,但闪避没有帮助。 geom_bspline 函数似乎也没有提供解决方案(例如 type="open" 没有帮助)。

所以我正在寻找一个允许从 ggforce 包(或类似的解决方案)扩展 geom_bspline 的解决方案。

下面我添加了情节和情节的代码示例。

library("tidyverse")
library("ggforce")


## DATASET
dftest <- tibble(
  x = seq(1,11,1),
  y1 = c(3,   0 ,  1  , 0 ,  2   ,0,  20, 126, 426, 400 , 37),
  y2 = c( 3 ,3   , 4 ,   4 ,   6 ,   6 ,  26,  152 , 578 , 978, 1015)
)


###### Figure ------
ggplot(dftest, aes(x=x)) + 
  
  ### BARS
  geom_bar(aes(y= y1), fill="#FC301A", stat="identity") +
  
  ### SPLINE
  geom_bspline(aes(x=x,y=y2),data=dftest)+
  
  ### SCALES
  scale_x_discrete(name ="",  drop = FALSE) + 
  scale_y_continuous(expand = c(0,0),
                     limits = c(0, 1200),
                     breaks = seq(0,1200,200),name = "Kumulativ",
                     sec.axis = sec_axis(~ ((. - a)/b)+2,
                                         name = "Absolut"),
                     labels=scales::comma_format(big.mark = ".")) +
  
  ### THEME
  theme_classic()+
  theme(
    panel.grid.major.y = element_line(colour = "grey70", size = 0.2),  # linien
    
    ###axis
    axis.ticks.length = unit(1, "mm"),
    
    axis.text.x = element_text(family = "Helvetica",  size=10,angle = 90,vjust = 0.5,
                               margin = unit(c(0.2,0,0,0),"cm")),
    axis.text.y = element_text(family = "Helvetica",  size=10),
    axis.text.y.right = element_text(color = "red",family = "Helvetica",  size=10),
    
    
    axis.title.y.right =  element_text(color = "red",family = "Helvetica",  size=10,
                                       margin = unit(c(0,0,0,0.25),"cm")),
    axis.title.y.left =  element_text(family = "Helvetica",  size=10, 
                                      margin = unit(c(0,0.25,0,0),"cm"))
    
  )

样条曲线必须在结点之间进行插值,除非您外推结束结点,否则无法外推,因此最简单的方法是创建数据框的副本并添加一个额外的行时间测量值,例如 x = 11.5 或 x = 11.7(无论您觉得“看起来正确”),但具有相同的 y2 值:

dftest2 <- rbind(dftest, tail(dftest, 1))
dftest2$x[nrow(dftest2)] <- 11.7

那么你可以这样做:

ggplot(dftest, aes(x = x)) + 
  geom_bar(aes(y= y1), fill="#FC301A", stat="identity") +
  geom_bspline(aes(x = x, y = y2), data = dftest2) +
  scale_x_discrete(name ="",  drop = FALSE) + 
  scale_y_continuous(expand = c(0,0),
                     limits = c(0, 1200),
                     breaks = seq(0,1200,200),name = "Kumulativ",
                     sec.axis = sec_axis(~ ((. - 0)/2.3)+2,
                                         name = "Absolut"),
                     labels=scales::comma_format(big.mark = ".")) +
  theme_classic()+
  theme(
    panel.grid.major.y = element_line(colour = "grey70", size = 0.2),
    axis.ticks.length = unit(1, "mm"),
    
    axis.text.x = element_text(family = "Helvetica",  
                               size=10,angle = 90, vjust = 0.5,
                               margin = unit(c(0.2,0,0,0),"cm")),
    axis.text.y = element_text(family = "Helvetica",size=10),
    axis.text.y.right = element_text(color = "red",
                                     family = "Helvetica",  size = 10),
    axis.title.y.right =  element_text(color = "red",family = "Helvetica",  
                                       size=10,
                                       margin = unit(c(0,0,0,0.25),"cm")),
    axis.title.y.left =  element_text(family = "Helvetica",  size=10, 
                                      margin = unit(c(0,0.25,0,0),"cm"))
  )

请注意,您的代码无法完全重现,因为您没有为次轴包含变量 ab,因此我不得不在此处近似您的次轴。