有人能帮我添加次轴的百分比吗?我尝试了不同的方法,但没有得到任何结果。
我将提供以下数据:
year <- c("2010","2011","2012","2013","2014","2015","2016","2017","2018","2019","2020","2010","2011","2012","2013","2014","2015","2016","2017","2018","2019","2020")
sex <- c("male","male","male","male","male","male","male","male","male","male","male","female","female","female","female","female","female","female","female","female","female","female")
number <- c(12,5,15,5,4,6,5,5,8,4,0,54,64,70,50,54,35,40,37,42,36,24)
perFailed <- c(0.090909091,0.057971014,0.117647059,0.163636364,0.068965517,0.048780488,0.044444444,0.071428571, 0.1,0.1,0)
data <- data.frame(year,sex,number,perFailed)
data$year <- as.factor(data$year)
library(ggplot2)
ggplot(data=data, aes(x=year, y=number, fill=sex)) +
geom_bar(stat="identity", position=position_dodge()) +
theme_bw()
我不知道是怎么做到的,但是R复制了我的上一列(perFailed
),这个数据需要在辅助轴上绘制为geom_line()
。
谢谢大家。
1条答案
按热度按时间qhhrdooz1#
试试这个:
注:
1.我假设,既然数字在
"male"
和"female"
之间是不变的,那么将数据减少到其中一个是可以的,如果不减少,这行就会被重复打印,虽然这很少有影响,甚至不会被注意到,但似乎是不必要的。1.使用
match(year, sort(unique(year)))
有点像黑客。叠加轴可能会有点问题,这是一种有效的方法。