R ggplot2双坐标轴作图
作者:互联网
R ggplot2双坐标轴作图
1
library(ggplot2)
library(hrbrthemes)
a<-data.frame('time'=c("2018.6","2018.12","2019.6","2020.3",
"2020.6","2020.12","2021.6"),
'num'=c(56892,61011,63882,71027,74939,78241,81206),
'perc'=as.numeric(c(71,73.6,74.8,78.6,79.7,79.1,80.3)))
a$perc<-as.numeric(a$perc)
ggplot(data=a,aes(x=time)) +
geom_bar(aes(y=num),stat ='identity',fill='#69b3a2') +
geom_text(aes(y = num + 3000, label = num)) +
geom_line(aes(y=perc*1300,group=1),color='#E69F00',size=1.1) +
geom_point(aes(y=perc*1300,group=1),color='#E69F00',size=3.2)+
scale_y_continuous(
name = "用户规模(万人)",
sec.axis = sec_axis(trans=~./1300, name="使用率")
)+
geom_text(aes(y = perc*1300 + 5000, label = paste0(perc,'%')))+
xlab('时间')+
theme_test()+
theme(
axis.title.y = element_text(color = 'black', size=13),
axis.title.y.right = element_text(color = 'black', size=13),
plot.margin = unit(c(8,0.5,8,0.5), "lines")
)
2.
ggplot(data=temp,aes(x=time)) +
geom_area(aes(y=gdp1),fill="#69b3a2", alpha=0.5) +
geom_line(aes(y=speed*200000),color='black',lty='dashed') +
scale_y_continuous(
name = "人均GDP(元)",
sec.axis = sec_axis(trans=~.*0.000005, name="GDP增速")
)+
xlab('年份')+
ggtitle('中国人均GDP情况')+
scale_x_continuous(limits=c(2000,2018), breaks=seq(2000,2018,5))+
theme_ipsum()+
theme(
axis.title.y = element_text(color = '#69b3a2', size=13),
axis.title.y.right = element_text(color = 'black', size=13)
)
标签:GDP,aes,作图,color,坐标轴,ggplot2,size,axis 来源: https://blog.csdn.net/weixin_43251277/article/details/120802110