美文网首页r语言
多种动态图的绘制(R语言篇)

多种动态图的绘制(R语言篇)

作者: 枫璃 | 来源:发表于2020-05-08 12:59 被阅读0次

    上一章介绍过动态气泡图的绘制(https://www.jianshu.com/p/702d02d8333b ),这里继续介绍动态条形图、柱形图、点图、折线图的制作,原理和气泡图差不多,非常简单易上手。

    一、动态条形图

    1.1 加载包

    library(gapminder)

    library(ggdark)

    library(ggplot2)

    library(gganimate)

    1.2 加载数据集

    原始数据:使用gapminder这个数据集合。该数据一共有6列,依次为country(国家)、continent(洲)、year(年份)、lifeExp(生活指数)、pop(人口)、gdpPercap(国内生产总值)。

    head(gapminder)

    mydata <- gapminder[which(gapminder$country %in% (c('France','Italy','China','Japan','Austria','Brazil','Colombia','Cuba','','Germany','India'))),] #避免图形太冗杂,只筛选部分国家

    1.3 绘制动态条形图

    使用gganimate这个包绘制动态图:

    使用函数transition_time()添加动态,并指定动态依据哪个变量变化,这里动态变量是year:

    ps = ggplot(mydata, aes(x=reorder(country, lifeExp),y=lifeExp, fill=country,frame=year)) +

        geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +

        geom_text(aes(label=paste0(lifeExp)),col="black",hjust=-0.2)+

        theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +

        theme(panel.background=element_rect(fill='transparent'))+

      theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+

      theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+

      theme(panel.grid =element_blank()) +  ## 删去网格线

      theme(axis.text = element_blank()) +  ## 删去所有刻度标签

      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线

      coord_flip()+ #横纵坐标位置转换

      transition_time(year) + #设置动态

      labs(title = paste('Year:', '{frame_time}'),x = '', y ='各国生活指数')+

      ease_aes('linear')

    ps

    二、动态柱形图

    和条形图类似,取消横纵坐标位置转换(coord_flip()),稍微调整横纵坐标即可:

    ps = ggplot(mydata, aes(x=reorder(country, lifeExp),y=lifeExp, fill=country,frame=year)) +

        geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +

        geom_text(aes(label=paste0(lifeExp)),col="black",hjust=0)+

        theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +

        theme(panel.background=element_rect(fill='transparent'))+

      theme(axis.text.y=element_text(angle=0,colour="white",size=12,hjust=0))+

      theme(axis.text.x=element_text(angle=0,colour="black",size=9,hjust=0))+

      theme(panel.grid =element_blank()) +  ## 删去网格线

      theme(axis.text = element_blank()) +  ## 删去所有刻度标签

      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线

    # Here comes the gganimate specific bits

    transition_time(year) +

    labs(title = paste('Year:', '{frame_time}'),x = '', y ='各国生活指数')+

    ease_aes('linear')

    ps

    三、动态点图

    ps = ggplot(mydata, aes(x=year,y=lifeExp)) +

    geom_point(aes(color = country)) +

    #Here comes the gganimate specific bits

    transition_manual(year, cumulative = T) +

    labs(title = paste('Year:', '{current_frame}'),x = '', y ='各国生活指数')+ ease_aes('linear')

    四、动态折线图

    在点图的基础上加上对点的连线,即是动态折线图啦,只需要加个简单的语句:geom_line(aes(color = country))

    ps = ggplot(mydata, aes(x=year,y=lifeExp)) +

        geom_point(aes(color = country)) +

    geom_line(aes(color = country)) +

    # Here comes the gganimate specific bits

    transition_manual(year, cumulative = T) +

    labs(title = paste('Year:', '{current_frame}'),x = '', y ='各国生活指数')+

    ease_aes('linear')

    总结:

    用R绘制动态图的方法都是先用ggplot绘制出需要的图形种类,然后使用transition_time函数添加动态变量,这个变量可以是时间、也可以是其他变量,就能得到对应的动态图啦。

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    更多数据相关文章,欢迎关注公众号“大数据会”。


    相关文章

      网友评论

        本文标题:多种动态图的绘制(R语言篇)

        本文链接:https://www.haomeiwen.com/subject/fpbgghtx.html