Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ggplot2 to draw pie chart pie

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use ggplot2 to draw pie chart pie, the article is very detailed, has a certain reference value, interested friends must read it!

In the process of drawing the pie chart, using the geom_bar of ggplot2 combined with coord_polar, it is necessary to understand that the arrangement of the pie chart is determined according to the order of aes (fill) factors. For example, the data are as follows

Dat type Num1 A 902 B 343 C 564 D 995 E 15

You must first determine the factor order of aes (fill) in mapping based on the data, for example, it will be populated according to dat$type, and this non-ordered factor will default to its filling order based on alphabetical order.

In order to determine the order of data filling, and to facilitate filling in the corresponding data size in different regions, the order factor will be created first, so that the natural order of the data column dat$Num and the order of the factors are consistent to a certain extent (consistent corresponding or reverse corresponding). For example, make the direction consistent as follows:

Dat$type=factor (dat$type,levels = dat$type,order=T) dat$type

The results of the ordered factors are as follows, which is consistent with the order of dat$Num, and there is no corresponding disorder problem.

[1] A B C D ELevels: a < B < C < D < E

Draw a picture:

P_pie=ggplot (dat,aes (x = ", y=dat [, 2], fill=dat [, 1])) + geom_bar (stat=" identity ", width=1) + coord_polar (theta=" y ", direction=1) + scale_fill_brewer (palette =" Set3 ", direction=1) + labs (x =", y =", fill= "Type") + ggtitle (label = "test", subtitle=NULL) p_pie

Combined with the results of the following figure, you can see that the direction of the axis is clockwise, while the color setting scale_fill_brewer (palette = "Set3", direction = 1) sets the first color fill to the "A" corresponding to the first factor, which reflects that the data and factors are inversely corresponding in the actual distribution of the picture. Although the settings in the dat data box are corresponding in the same order and direction, they will change in the picture distribution.

Combined with the reverse corresponding relationship in the picture, fill the A block with the corresponding text Num:90, its coordinate should be sum (dat$Num)-90 + 90 dat$Num 2, and if it is B block, the coordinate should be sum (dat$Num)-90-34 + 34 long 2.

Summed up as sum (dat$Num)-cumsum (dat$Num) + dat$Num/2

Sum (dat$Num)-cumsum (dat$Num) + dat$Num/2 [1] 249.0 187.0 142.0 64.5 7.5

Combined with the location setting of geom_text (aes (xQuery)), it ensures that the middle text will not make mistakes.

Pendant pieziezo + geom_text (aes (xresume 1.2)-cumsum (dat$Num) + dat$Num/2, label=as.character (dat [, 2])), size=3) p_pie

# # what if the direction of the initial construction of the order factor corresponds inversely to the direction of the actual data?

Dat$type=factor (dat$type,levels = rev (dat$type), order=T) dat$typep_pie=ggplot (dat,aes (x = ", y=dat [, 2], fill=dat [, 1]) + geom_bar (stat=" identity ", width=1) + coord_polar (theta=" y ", direction=1) + scale_fill_brewer (palette =" Set3 ", direction=1) + labs (x =", y =", fill= "Type") + ggtitle (label = "test", subtitle=NULL) p_pie

Combined with the picture, we can see that the first factor "E" corresponds to the first color, but you can see that in the picture display coordinates, "A" is in the front, and the data corresponding to "A" in dat$Num is also in the top 90, so the calculation position will be changed. 90-90amp 2, "B" will correspond to 90cm 34-34max 2.

Summed up as cumsum (dat$Num)-dat$Num/2

Cumsum (dat$Num)-dat$Num/2 [1] 45.0 107.0 152.0 229.5 286.5

And the legend is also in reverse, you need to combine guides (fill=guide_legend (reverse=T)) and want the first color to correspond to the last factor "A", scale_fill_brewer (palette = "Set3", direction =-1)

Dat$type=factor (dat$type,levels = rev (dat$type), order=T) dat$typep_pie=ggplot (dat,aes (x = ", y=dat [, 2], fill=dat [, 1]) + geom_bar (stat=" identity ", width=1) + coord_polar (theta=" y ", direction=1) + scale_fill_brewer (palette =" Set3 ", direction=-1) + labs (x =", y =", fill= "Type") + ggtitle (label = "test", subtitle=NULL) + guides (fill=guide_legend (reverse = T)) + geom_text (aes (reverse = T)) Y=cumsum (dat$Num)-dat$Num/2, label=as.character (dat [, 2]), size=3) p_pie are all the contents of the article "how to draw a pie chart pie using ggplot2" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report