[英]ggplot in R - historam & line plot with two y-axis
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
?可以在
ggplot
做到这ggplot
吗?
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.