R语言ggplot2可视化经典例子实现

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值向量的显示。

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

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

position=dodge(0.9))

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

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

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

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

显示图的背景颜色修饰

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


数据点的误差效果可视化

#
names(iris)<-paste0('AA',1:5)
ggplot(iris, aes(x=AA1, y=AA3,shape=AA5,col=AA5)) +
geom_line() + geom_point(size=4) + 
geom_errorbar(aes(ymin=AA3-sd(iris$AA3)/9,
    ymax=AA3+sd(iris$AA3)/9), width=.1,col='red')
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章