Reputation: 1860
I am trying to create something like this:
Insired by this question i tried the following, but i does not work:
claims_freq <- c(0.1,0.3,0.2,0.7,0.1)
claims_sev <- c(10000, 12000, 14000, 16000, 1600)
year <- c(2015, 2016, 2017, 2018, 2019)
data <- cbind(claims_freq, claims_sev, year)
data <- as.data.table(data)
twoord.stackplot(lx=data$year, rx=data$year,
ldata=cbind(data$claims_freq),
rdata=cbind(data$claims_sev),
lcol=c("black"),
rcol=c("black"),
ltype=c("l"),
rtype=c("bar"),
lylab="freq", rylab="sev",
xlab="year",
main="Freq/Sev 2015-2020",
border="grey80")
is it possible to do this in ggplot
?
Upvotes: 0
Views: 89
Reputation: 627
Try:
#It has to be in data.frame format
data <- data.frame(year, claims_sev, claims_freq)
#column plot + line plot
ggplot(data, aes(year)) +
geom_col(aes(y = claims_sev), fill = "#D17F7D") +
#adding line plot - x times the value (e.g. 30 000)
geom_line(aes(y = claims_freq*30000), color = "#227BC4") +
#adding second axis + dividing main axis by x
scale_y_continuous(sec.axis = sec_axis(~./30000))
#you can use `labels()` and `theme()` for further adjustments
Upvotes: 1