I would like to have a double y axis in a plot and put specific annotations that includes some labels. Here is a reproducible example of what I have (the issue is at the end, in the plot):
#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()
This code generates the following graph: I would like axis y to be at the end of the plot and the labels of geom_text to be the axis numbers. Is it possible? It would be something like this:
Thanks in advance!
CodePudding user response:
Sure is that possible. You could add your labels via the secondary axis trick by making use of a secondary or duplicated y axis:
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()