label mpchart 饼图

您所在的位置:网站首页 甜甜圈怎么作 label mpchart 饼图

label mpchart 饼图

2023-07-31 14:39| 来源: 网络整理| 查看: 265

饼图,听起来挺简单的,但是为什么R里面画饼图的怎么这么少?

下面是答案:

Pie charts are not recommended in the R documentation, and their features are somewhat limited. The authors recommend bar or dot plots over pie charts because people are able to judge length more accurately than volume. Pie charts are created with the function pie(x, labels=) where x is a non-negative numeric vector indicating the area of each slice and labels= notes a character vector of names for the slices.

谷歌翻译一下就是:

R文档中不建议使用饼图,并且饼图的功能受到一定限制。作者建议在饼图中使用条形图或点图,因为人们能够比长度更准确地判断长度。饼图是使用函数pie(x,labels =)创建的,其中x是指示每个切片区域的非负数字矢量,labels =表示切片名称的字符矢量。

所以说,有时候不要把R单纯的看成一个编程、统计软件。从软件的角度来看,R没有python灵活,没有c++快,感觉几乎都没有什么优点。但是将R看成一个艺术品,你就不会在乎这些。从刚开始“丑陋”的Rgui到后面一系列艺术的包、一系列具有艺术的代码思想(可能有些限制,但是真的美)。不同的角度看真的漂亮。如果单纯的从一些理性的角度看R。我感觉R根本就没有资格排在代码排行榜前8左右的位置。任何一个软件足以取代他。所以说,看看R的内在设计艺术(不是看源码)。

扯远了,接下来几个方法来介绍如何使用R画饼图!

简单版

# Simple Pie Chartslices 10, lbls "US", pie(slices, labels = lbls, main="Pie Chart of Countries")

d7a06b337bea71a1b57508add6509f5b.png

评论一下就是感觉真丑

如果加上每个数值的大小了,而且是使用百分比的格式显示

# Pie Chart with Percentagesslices 10, lbls "US", pct 100)lbls # add percents to labelslbls "%",sep=pie(slices,labels = lbls, col=rainbow(length(lbls)),    main="Pie Chart of Countries")

3cdf6e0c50bcc935e5617bb64f2452e4.png

这个图感觉还行

3d版的饼图?

# 3D Exploded Pie Chartlibrary(plotrix)slices c(lbls c(pie3D(slices,labels=lbls,explode=0.1, main="Pie Chart of Countries ")

4f666d00159f691eb452690e3bf998c3.png

还行,起码还算是能看

上面是基础R画图系统出来的。ggplot2版本可不能少

ggplot2没有内置可以直接画饼图的。但是ggplot2里面有个可以将x或者y轴卷起来的东西。刺不刺激,怕不是很多人从来就没见过吧

library("ggplot2") # Data visualizationlibrary("dplyr") # Data manipulationcount.data data.frame( class = c("1st", "2nd", "3rd", "Crew"), n = c(325, 285, 706, 885), prop = c(14.8, 12.9, 32.1, 40.2))count.data # Add label positioncount.data data %>% arrange(desc(class)) %>% mutate(lab.ypos = cumsum(prop) - 0.5*prop)count.data mycols "#0073C2FF", ggplot(count.data, aes(x = "", y = prop, fill = class)) + geom_bar(width = 1, stat = "identity", color = "white") + coord_polar("y", start = 0)+ geom_text(aes(y = lab.ypos, label = prop), color = "white")+ scale_fill_manual(values = mycols) + theme_void()

af3597293198cacb4636af7c172c3ade.png

ggplot(count.data, aes(x = 2, y = prop, fill = class)) + geom_bar(stat = "identity", color = "white") + coord_polar(theta = "y", start = 0)+ geom_text(aes(y = lab.ypos, label = prop), color = "white")+ scale_fill_manual(values = mycols) + theme_void()+ xlim(0.5, 2.5)

db8914175e39386ae8e9543ef7f320c6.png

ggplot2难点就是这个,坐标轴旋转上面这个从饼变成甜甜圈为什么可以?因为它让x的界限变长了,导致下面的空掉了。空的卷起来,那可就是甜甜圈了。当然也可也看我之前的文章:

R画polar图

1c568e4df8cfb281fd3d8ca7215c86d2.png

ggplot2能做的,基本上plotly都能做

library(plotly)library(dplyr)fig fig % add_pie(data = count(diamonds, cut), labels = ~cut, values = ~n, name = "Cut", domain = list(x = c(0, 0.4), y = c(0.4, 1)))fig % add_pie(data = count(diamonds, color), labels = ~color, values = ~n, name = "Color", domain = list(x = c(0.6, 1), y = c(0.4, 1)))fig % add_pie(data = count(diamonds, clarity), labels = ~clarity, values = ~n, name = "Clarity", domain = list(x = c(0.25, 0.75), y = c(0, 0.6)))fig % layout(title = "Pie Charts with Subplots", showlegend = F, xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE), yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))fig

859589d750c220491f623f1ea3d12205.png

难道只有这样的图?扩展一点,比如说,这个扇型可以展开,看到里面的东西。这种图叫sunburst图。看我以前的文章:

武汉疫情全国分布(sunburst图)

9120d2526280ebf2d82923f623a3981c.png

如果大圆包括小圆,怎么做?

# Circlepacker packagelibrary(circlepackeR) # devtools::install_github("jeromefroe/circlepackeR") # If needed# Let's use the 'flare dataset' (stored in the ggraph library)library(ggraph)data_edge $edgesdata_edge$from ".*\\.",data_edge$to ".*\\.",head(data_edge) # This is an edge list# We need to convert it to a nested data frame. the data.tree library is our best friend for that:library(data.tree)data_tree data_nested level1 = function(x) x$path[2], level2 = function(x) x$path[3], level3 = function(x) x$path[4], level4 = function(x) x$path[5])[-1,-1]data_nested # Now we can plot it as seen before!data_nested$pathString "roots", data_nesteddata_nested$value=1data_Node p "value")p# save the widget# library(htmlwidgets)# saveWidget(p, file=paste0( getwd(), "/HtmlWidget/circular_packing_circlepackeR1.html"))

下面的图真的是漂亮,当你在浏览器里面看到的时候(输出的是html不是静态的东西)

2cf958b691f124f681f94252e4d36ef5.png

本文参考链接:

https://www.statmethods.net/graphs/pie.html

https://plotly.com/r/pie-charts/

https://www.datanovia.com/en/blog/how-to-create-a-pie-chart-in-r-using-ggplot2/

https://www.r-graph-gallery.com/338-interactive-circle-packing-with-circlepacker

说句实在的,如果无聊,可以看看我之前写的很多文章

欢迎关注华东吴亦凡

8c469c488af46e88c8925649ff29d880.png



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3