R语言可视化——极坐标变换与衍生图表类型

浏览: 2305

今天跟大家简单介绍ggplot作图系统中的极坐标图表类型。


ggplot没有像条形图那样的直接作图函数,需要借助柱形图和极坐标变换来模拟各种具有极坐标性质的图表类型——饼图、圆环图、圆段图、玫瑰图等。

在常见的三种坐标形式中,极坐标转换可以非常轻松的将常见的柱形图(条形图)、堆积柱形图只需追加一句代码,就可轻松的的转化为饼图、玫瑰图、圆环图等。

饼图需要借助柱形图结合极坐标进行转化:


ggplot(diamonds,aes(x=factor(1),fill=cut))+

geom_bar()



当使用一个因子变量作为横轴,使用分类变量进行填充时,该柱形图变成了单条堆积柱形图。

接下来我们通过以上函数添加极坐标(注意极坐标的设定是如何影响最终的图表形式的)

ggplot(diamonds,aes(x=factor(1),fill=cut))+

geom_bar()+

coord_polar(theta = "y")


ggplot(diamonds,aes(x=factor(1),fill=cut))+

geom_bar()+

coord_polar(theta = "x")

ggplot(diamonds,aes(x=factor(1),fill=cut))+

geom_bar()+

coord_polar()


从做出的图表以及极坐标函数内的参数我们可以看出来,当参数指定为x时,最终柱形图的x轴会被指定为极坐标的x轴(圆周),而柱形图的y轴则会成为极坐标的y轴(半径)。当指定为y轴时,柱形图的y轴为成为极坐标的x轴(圆周),柱形图的x轴会成为极坐标的y轴(半径)。

同时极坐标状态下,饼图的半径是由柱形图柱形图宽度决定的,以上图表中圆心有个空白,如果我们将柱形图柱形图宽度定义为1,则会成为正圆。

ggplot(diamonds,aes(x=factor(1),fill=cut))+

geom_bar(width=1)+

coord_polar(theta = "y")


图中其他的元素控制方式与往常的柱形图做法一样:

去掉极坐标轴的轴刻度标签,去掉两个轴标题、更换配色:

ggplot(diamonds,aes(x=factor(1),fill=cut))+

geom_bar(width=1)+

coord_polar(theta = "y",start=0)+

scale_fill_brewer(palette="Blues")+

guides(fill=guide_legend(reverse=TRUE,title=NULL))+

theme(

     panel.grid = element_blank(),

    panel.background = element_blank(),

    axis.text = element_blank(),

    axis.ticks = element_blank(),

    axis.title = element_blank()

    )


当有多个序列时:

ggplot(diamonds,aes(cut))+

geom_bar(width=1)


通过极坐标转换可以实现圆环图、圆条图效果:

ggplot(diamonds,aes(cut))+

geom_bar(width=1,fill="steelblue",colour="white")+

coord_polar(theta = "y",start=0)+

theme(

     panel.grid = element_blank(),

     panel.background = element_blank(),

     axis.text.x = element_blank(),

     axis.title = element_blank()

      )


通过将条形图序列颜色进行分类,可以实现圆段条形图的颜色区隔效果:

ggplot(diamonds,aes(cut,fill=cut))+

geom_bar(width=1,colour="white")+

coord_polar(theta = "y",start=0)+

scale_fill_brewer(palette="Greens")+

guides(fill = guide_legend(reverse = TRUE))+

theme(

     panel.grid = element_blank(),

     panel.background = element_blank(),

     axis.text.x = element_blank(),

     axis.title = element_blank()

     )


改变极坐标轴参数设置,可以模拟南丁格尔玫瑰图效果:

ggplot(diamonds,aes(cut))+

geom_bar(width=0.95,fill="#3182BD")+

ylim(c(-3000,22500))+

coord_polar(theta = "x",start=0)+

theme(

     panel.grid = element_blank(),

     panel.background = element_blank(),

     axis.text.y = element_blank(),

     axis.ticks= element_blank(),

     axis.title = element_blank()

     )


同样的方式,对其进行颜色分类映射操作:

ggplot(diamonds,aes(cut,fill=cut))+

geom_bar(width=0.95)+

coord_polar(theta = "x",start=0)+

scale_fill_brewer(palette="Greens")+

guides(fill = guide_legend(reverse = TRUE))+

ylim(c(-3000,22500))+

theme(

    panel.grid = element_blank(),

    panel.background = element_blank(),

    axis.text.y = element_blank(),

   axis.ticks= element_blank(),

   axis.title = element_blank()

   )


当然如果你使用的原始柱形图数据中添加了分类序列(也就是堆积柱形图),按照上面的方式可以制作更加复杂的圆环图、堆叠玫瑰图

ggplot(diamonds,aes(x=color,fill=cut))+

geom_bar(width=0.95,colour="white")+

coord_polar(theta = "y",start=0)+

scale_fill_brewer(palette="Blues")+

guides(fill=guide_legend(reverse=TRUE,title=NULL))+

theme(

      panel.grid = element_blank(),

      panel.background = element_blank(),

      axis.text.x = element_blank(),

      axis.title = element_blank()

        )




ggplot(diamonds,aes(x=color,fill=cut))+

geom_bar(width=0.95,colour="white")+

coord_polar(theta = "x",start=0)+

scale_fill_brewer(palette="Blues")+

guides(fill=guide_legend(reverse=TRUE,title=NULL))+

ylim(c(-2000,12000))+

theme_bw()+

theme(

    axis.text.y = element_blank(),

    axis.title = element_blank()

    )


尝试着用分面来解决多序列问题:

玫瑰图分面:

ggplot(diamonds,aes(x=color,fill=cut))+

geom_bar(width=0.95,colour="white")+

coord_polar(theta = "x",start=0)+

scale_fill_brewer(palette="Blues")+

guides(fill=guide_legend(reverse=TRUE,title=NULL))+

ylim(c(-2000,6000))+

theme_bw()+

facet_grid(.~cut)+

theme(

        axis.text.y = element_blank(),

        axis.title = element_blank()

        )


饼图分面:

ggplot(diamonds,aes(x=factor(1),fill=cut,order=cut))+

geom_bar(position="fill", width = 1)+

facet_grid(.~color)+coord_polar(theta = "y")+

scale_fill_brewer(palette="Blues")+

guides(fill=guide_legend(reverse=TRUE,title=NULL))+

theme(

      panel.grid = element_blank(),

      panel.background = element_blank(),

      axis.text = element_blank(),

      axis.ticks = element_blank(),

      axis.title = element_blank()

      )




推荐 0
本文由 EasyCharts 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册