R语言-绘制饼图-点击链接加入群【农产品一体化解决方案】:https://jq.qq.com/?_wv=1027&k=49BAREK

本文介绍了如何使用R语言的pie()函数绘制饼图,包括基础饼图、带标题和颜色的饼图、显示切片百分比和图例的饼图,以及3D饼图。通过示例代码详细展示了绘制过程。

点击链接加入群【农产品一体化解决方案】:https://jq.qq.com/?_wv=1027&k=49BAREK

饼图是表示不同颜色的值的圆片,切片标记和对应于各切片的数量也被表示在图表中

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()

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

    Pie Chatr using 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()
    

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

    Pie-chart with title and colours

    切片百分比和图表图例

    我们可以通过创建额外的图表变量添加切片百分比和图表图例。

    # 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()
    

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

    pie-chart with percentage and labels

    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 pie-chart

### 如何生成多层 生成多层可以通过多种工具和编程语言实现,例如 Python 的 Matplotlib 库、JavaScript 的 D3.js 库,或者使用专业的数据可视化工具如 Tableau 或 Excel。以下是基于 Python 和 JavaScript 的实现方法[^4]。 #### 使用 Python 生成多层 Python 的 Matplotlib 提供了绘制的功能,并支持通过调整参数来创建多层效果。以下是一个简单的代码示例: ```python import matplotlib.pyplot as plt # 数据准备 sizes_outer = [30, 25, 20, 15, 10] sizes_inner = [15, 10, 5] # 颜色设置 colors_outer = ['#ff9999','#66b3ff','#99ff99','#ffcc99','#c2c2f0'] colors_inner = ['#c2c2f0','#ffb3e6','#ff6666'] # 绘制外层 fig, ax = plt.subplots() ax.pie(sizes_outer, radius=1, colors=colors_outer, labels=['A', 'B', 'C', 'D', 'E'], labeldistance=1.05, wedgeprops=dict(width=0.3, edgecolor='w')) # 绘制内层 ax.pie(sizes_inner, radius=1-0.3, colors=colors_inner, labels=['X', 'Y', 'Z'], labeldistance=0.7, wedgeprops=dict(width=0.3, edgecolor='w')) # 设置像属性 ax.set(aspect="equal", title='Multi-layer Pie Chart') plt.show() ``` 这段代码利用 `wedgeprops` 参数控制每层的宽度,并通过调整半径(`radius`)实现多层效果[^4]。 #### 使用 JavaScript (D3.js) 生成多层 D3.js 是一个强大的数据可视化库,适合生成复杂的表。以下是一个简单的多层生成示例: ```javascript const dataOuter = [30, 25, 20, 15, 10]; const dataInner = [15, 10, 5]; const width = 400; const height = 400; const outerRadius = Math.min(width, height) / 2; const innerRadius = outerRadius * 0.7; const svg = d3.select("body") .append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", `translate(${width / 2}, ${height / 2})`); // 外层 const pieOuter = d3.pie().value(d => d)(dataOuter); const arcOuter = d3.arc().outerRadius(outerRadius).innerRadius(innerRadius); svg.selectAll(".arc.outer") .data(pieOuter) .enter().append("path") .attr("class", "arc outer") .attr("d", arcOuter) .style("fill", (_, i) => d3.schemeCategory10[i]); // 内层 const pieInner = d3.pie().value(d => d)(dataInner); const arcInner = d3.arc().outerRadius(innerRadius - 20).innerRadius(0); svg.selectAll(".arc.inner") .data(pieInner) .enter().append("path") .attr("class", "arc inner") .attr("d", arcInner) .style("fill", (_, i) => d3.schemeCategory10[i]); ``` 这段代码通过 `d3.pie()` 方法生成角度数据,并使用 `d3.arc()` 方法绘制内外两层[^5]。 #### 布局与样式 在生成多层时,需要注意以下几点: - **颜色选择**:确保不同层的颜色区分明显,避免混淆。 - **标签位置**:合理安排标签的位置,避免重叠或遮挡。 - **交互性**:如果需要增强用户体验,可以添加鼠标悬停显示详细信息的功能[^6]。 ### 示例像占位符 由于无法直接生成像,请参考上述代码实现多层的效果。以下是可能的占位符描述: - 外层包含五个部分,颜色分别为浅红、浅蓝、浅绿、橙色和紫色。 - 内层包含三个部分,颜色分别为紫色、粉色和红色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值