ggplot (ggfan) 中的双 y 轴但次轴具有特定注释

Double y axis in ggplot (ggfan) but secundary axis have especific annotations

我想在绘图中使用双 y 轴并放置包含一些标签的特定注释。这是我所拥有的一个可重现的例子(问题在最后,在情节中):

#libraries
library(dplyr)
library(tidyr)
library(ggplot2)
library(ggfan)
library(gridExtra)
library(stringr)
library(scales)

#Create a dataframe 
month <- 1:120 
price_a <- 5000 
demand <- 10
data <- data.frame(month, price_a, demand)

#Create 100 simulations to project price_a and demand for the future
simulations <- 100
intervalo <- seq_len(120)
set.seed(96)
lista_meses <- lapply(setNames(intervalo, paste0("data", intervalo)), function(i) {
  cbind(
    data[rep(i, simulations),],
    growth_pricea = as.numeric(runif(simulations, min = -0.02, max = 0.05)),
    growth_demand = as.numeric(runif(simulations, min = -0.03, max = 0.03)),
    revenue = demand*price_a
  )
})

#Calculate the growth of each variable and revenue
for (i in 2:length(lista_meses)){
  lista_meses[[i]][["price_a"]] <- lista_meses[[i-1]][["price_a"]]*(1+lista_meses[[i]][["growth_pricea"]])
  lista_meses[[i]][["demand"]] <- lista_meses[[i-1]][["demand"]]*(1+lista_meses[[i]][["growth_demand"]])
  lista_meses[[i]][["revenue"]] <- lista_meses[[i]][["price_a"]]*lista_meses[[i]][["demand"]]
}

#Extract revenue columns from all dataframes in list
time <- 1:120 #10 years. 

extract_column <- lapply(lista_meses, function(x) x["revenue"]) 

fandataq <- do.call("cbind", extract_column) 
mandataq <- as.matrix.data.frame(fandataq)
pdataq <- data.frame(x=time, t(fandataq)) %>% gather(key=sim, value=y, -x)



#Extract quantile vlues
# OR, using ggfan::calc_quantiles:
label_table <- calc_quantiles(pdataq, intervals = c(0.95, 0), x_var = "x", y_var = "y") %>%
  ungroup() %>%
  filter(x == max(x)) %>%
  mutate(y_label = scales::comma(y),
         intervalo = ifelse(quantile == 0.500, 50,
                            ifelse(quantile == 0.025, 2.5, 97.5)))

label_table <- label_table[-nrow(label_table),]

#Graph: I WANT TO SHOW DOUBLE Y AXIS WITH THE LABELS ON IT
ggplot(pdataq, aes(x=x, y= y)) + 
  geom_fan(intervals =c(95)/100, show.legend = F) + 
  geom_interval(intervals = c(0), show.legend = F) +
  scale_fill_gradient(low="steelblue1", high="steelblue")+
  scale_y_continuous(labels = scales::comma)+
  geom_text(data = label_table,
            aes(x = 125, label = paste0(intervalo, "% (",y_label,")")),  nudge_x = 1.5, size = 4.5) +
  coord_cartesian(clip = "off") +
  scale_x_continuous(expand = expansion(add = c(5, 20))) + 
  theme_bw()

此代码生成以下图表: 我希望 y 轴位于绘图的末尾,geom_text 的标签是轴号。可能吗?它会是这样的:

提前致谢!

当然可以。您可以使用辅助轴或重复的 y 轴通过辅助轴技巧添加标签:

ggplot(pdataq, aes(x=x, y= y)) + 
  geom_fan(intervals =c(95)/100, show.legend = F) + 
  geom_interval(intervals = c(0), show.legend = F) +
  scale_fill_gradient(low="steelblue1", high="steelblue")+
  scale_y_continuous(labels = scales::comma, 
                     sec.axis = dup_axis(
                       breaks = label_table$y,
                       labels = paste0(label_table$intervalo, "% (",label_table$y_label,")")))+
  coord_cartesian(clip = "off") +
  scale_x_continuous(expand = expansion(add = c(5, 0))) + 
  theme_bw()