×

R 教程

R语言 概述R语言 环境设置R语言 基本语法R语言 数据类型R语言 变量R语言 运算符R语言 决策R语言 包R语言 循环R语言 数据重塑R语言 函数R语言 字符串R语言 向量R语言 列表R语言 矩阵R语言 数组R语言 因子R语言 数据帧

R语言 图表

R语言 条形图R语言 箱线图R语言 直方图R语言 折线图R语言 散点图R语言 饼状图

R语言 数据接口

R语言 CSV文件R语言 Excel文件R语言 二进制文件R语言 XML文件R语言 JSON文件R语言 Web数据R语言 数据库

R语言 统计示例

R语言 平均值,中位数和模式R语言 线性回归R语言 多重回归R语言 逻辑回归R语言 标准分布R语言 二项分布R语言 泊松回归R语言 协方差分析R语言 时间序列分析R语言 非线性最小二乘R语言 决策树R语言 随机森林算法R语言 生存分析R语言 卡方检验

R语言 相关资源

R语言 外部资源R语言 相关讨论R语言 面试题

R语言 饼状图


R编程语言有许多库来创建图表和图表。 饼图是将值表示为具有不同颜色的圆的切片。 切片被标记,并且对应于每个片的数字也在图表中表示。
在R语言中,饼图是使用pie()函数创建的,它使用正数作为向量输入。 附加参数用于控制标签,颜色,标题等。

语法

使用R语言创建饼图的基本语法是 -

pie(x, labels, radius, main, col, clockwise)

以下是所使用的参数的描述 - 

  • x是包含饼图中使用的数值的向量。

  • labels用于给出切片的描述。

  • radius表示饼图圆的半径(值-1和+1之间)。

  • main表示图表的标题。

  • col表示调色板。

  • clockwise是指示片段是顺时针还是逆时针绘制的逻辑值。

使用输入向量和标签创建一个非常简单的饼图。 以下脚本将创建并保存当前R语言工作目录中的饼图。

# Create data for the graph.
x <- c(21, 62, 10, 53)
labels <- c("London", "New York", "Singapore", "Mumbai")

# Give the chart file a name.
png(file = "city.jpg")

# Plot the chart.
pie(x,labels)

# Save the file.
dev.off()

当我们执行上面的代码,它产生以下结果 -

馅饼的chatr,使用R

饼图标题和颜色

我们可以通过向函数中添加更多参数来扩展图表的功能。 我们将使用参数main向图表添加标题,另一个参数是col,它将在绘制图表时使用彩虹色板。 托盘的长度应与图表中的值的数量相同。 因此,我们使用length(x)

以下脚本将创建并保存当前R语言工作目录中的饼图。

# Create data for the graph.
x <- c(21, 62, 10, 53)
labels <- c("London", "New York", "Singapore", "Mumbai")

# Give the chart file a name.
png(file = "city_title_colours.jpg")

# Plot the chart with title and rainbow color pallet.
pie(x, labels, main = "City pie chart", col = rainbow(length(x)))

# Save the file.
dev.off()

当我们执行上面的代码,它产生以下结果 -

饼图以标题和颜色

切片百分比和图表图例

我们可以通过创建其他图表变量来添加切片百分比和图表图例。

# Create data for the graph.
x <-  c(21, 62, 10,53)
labels <-  c("London","New York","Singapore","Mumbai")

piepercent<- round(100*x/sum(x), 1)

# Give the chart file a name.
png(file = "city_percentage_legends.jpg")

# Plot the chart.
pie(x, labels = piepercent, main = "City pie chart",col = rainbow(length(x)))
legend("topright", c("London","New York","Singapore","Mumbai"), cex = 0.8,
   fill = rainbow(length(x)))

# Save the file.
dev.off()

当我们执行上面的代码,它产生以下结果 -

饼图用百分比和标签

3D饼图

可以使用其他软件包绘制具有3个维度的饼图。 软件包plotrix有一个名为pie3D()的函数,用于此。

# Get the library.
library(plotrix)

# Create data for the graph.
x <-  c(21, 62, 10,53)
lbl <-  c("London","New York","Singapore","Mumbai")

# Give the chart file a name.
png(file = "3d_pie_chart.jpg")

# Plot the chart.
pie3D(x,labels = lbl,explode = 0.1, main = "Pie Chart of Countries ")

# Save the file.
dev.off()

当我们执行上面的代码,它产生以下结果 -

3D饼图

分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)