网络知识 娱乐 R语言ggplot2实现带y值显示条柱、函数式注释、数据点带误差显示

R语言ggplot2实现带y值显示条柱、函数式注释、数据点带误差显示

geom_bar典型应用

#默认stat='count', stat_count() can only have an x or y aesthetic.不能有x,y同时出现;默认模式即是直方图;

ggplot(cc1[sample(1:12748,120),],aes(x=year,fill=year)) + geom_bar()

通过annotate参数设置实现y值向量的显示。

#ncc139<-cc1%>% as_tibble()%>%filter(maxtempc>=39);cc139nggplot(cc139,aes(x=year,fill=year)) + geom_bar() + #,label=countn labs(x='年份',y='超过39℃的天数',title='') +n annotate('text',x=1:7,y=cc139[,2]%>%table()%>%unname(),n label=cc139[,2]%>%table()%>%unname(),size=9,color=2)n

ggplot(data,aes(x=专业,y=比例)) + geom_bar(stat='identity',fill=语言,

position=dodge(0.9))

通过改变geom_bar函数中的position参数实现的,使柱子并排放置。

labs()及annotate()显示复杂数学式子

#nggplot(iris,aes(x=Species,y=Petal.Width,col=Species)) +geom_bar(stat='identity') +n labs(y=quote(f(x)==x^2),title='')nn

#np <- ggplot(data.frame(x=c(-3,3)), aes(x=x)) + stat_function(fun = dnorm,col='darkblue',lwd=3)np + annotate("text", x=0, y=0.3, parse=TRUE,label="frac(1,sqrt(2*pi))*e^{-x^2/2}",col='red',size=6)nn

显示图的背景颜色修饰

#nggplot(iris,aes(y=Petal.Width,x=Species))+ n ttgeom_col(colour =2,position='dodge2') +n theme(panel.background=element_rect(fill = "gold", colour = "blue")) n


数据点的误差效果可视化

#nnames(iris)<-paste0('AA',1:5)nggplot(iris, aes(x=AA1, y=AA3,shape=AA5,col=AA5)) +ngeom_line() + geom_point(size=4) + ngeom_errorbar(aes(ymin=AA3-sd(iris$AA3)/9,n ymax=AA3+sd(iris$AA3)/9), width=.1,col='red')n