柱状图+折线图+双Y-axis [R ggplot2]

本文介绍了如何使用R语言的ggplot2包创建组合条形图和折线图,用于可视化分层数据。通过示例展示了如何使用geom_bar()绘制条形图,geom_line()绘制折线图,并通过调整Y轴区间和添加次轴来处理不同比例的数据。同时,还演示了如何添加误差棒(ErrorBar)以显示标准差,以及如何美化图表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

😑有时,在处理分层数据时,我们需要将两种或多种不同的图表类型组合成一个图,以便更好地进行可视化和分析。这些被称为“组合图”。本文中,将使用ggplot2【R】实现组合条形图和折线图。实现类似下图效果:(不要在意马赛克)

在这里插入图片描述

😑使用geom_bar() 实现柱状图,使用方法:

Syntax:geom_bar(stat, fill, color, width)
Parameters :

stat : Set the stat parameter to identify the mode.
fill : Represents color inside the bars.
color : Represents color of outlines of the bars.
width : Represents width of the bars.

😑使用geom_line() 实现折线图,使用方法:

Syntax:geom_line(mapping=NULL, data=NULL, stat=”identity”, position=”identity”,…)

准备数据

year <- c(2016, 2017, 2018, 2019, 2020, 2021,2022)
course <- c(35, 30, 40, 25, 30, 35, 65)
penroll <- c(0.3, 0.25, 0.3, 0.5, 0.4, 0.2, 0.6)

# 创建 Data Frame
```r
perf <- data.frame(year, course, penroll)
perf
###
  year   course penroll
1 2016   35     0.30
2 2017   30     0.25
3 2018   40     0.30
4 2019   25     0.50
5 2020   30     0.40
6 2021   35     0.20
7 2022   65     0.60

先试一下

library(ggplot2)

ggplot(perf) +
  geom_bar(aes(x=year, y=course),
           stat="identity", fill="black",
           colour="#006000") +
  geom_line(aes(x=year, y=penroll),
            stat="identity", color="white") +
  labs(title= "Courses vs Students Enrolled in GeeksforGeeks",
       x="Year", y="Number of Courses Sold")

😑下图,可以看到条形图的形状与预期相符,但直线图只是可见的。这是由于比例造成的,折线图表示百分比,以十进制表示,而当前Y-axis 的值非常大。因此,我们需要一个次轴(另一个Y-axis ),以便在同一图表区域中正确地展示折线。需要使用ggplot2中的scale_y_continuous() 调整Y轴区间;使用sec_axis()添加次轴(另一个Y-axis)。

在这里插入图片描述

调整

ggp <- ggplot(perf) + 
  geom_bar(aes(x=year, y=course),
           stat="identity",
           fill="black") +
  geom_line(aes(x=year, y=100*penroll),
            stat="identity",
            color="red",
            size=2) +
  labs(title= "Title",
       x="Year",
       y="Number") +
  scale_y_continuous(sec.axis=sec_axis(~.*0.01, name="Percentage", labels=scales::percent))

ggp + theme_classic()

在这里插入图片描述

Error Bar 怎么办 ?

😑这时需要Standard Deviation ( SD ),下面随便添加的,实际需要计算,使用geom_errorbar() 实现:

# 添加sd值
perf$sd <- c(2,0.8,5.3,4.2,3.1,1.7,1)

# 画图

ggp <- ggplot(perf) + 
  geom_bar(aes(x=year, y=course),
           stat="identity",
           fill="skyblue") +
  geom_errorbar(
    aes(x=year, ymin=course-sd, ymax=course+sd),
    width=0.4, 
    colour="orange", 
    alpha=0.9, 
    size=1.3) +
  geom_line(aes(x=year, y=100*penroll),
            stat="identity",
            color="black",
            size = 0.8) +
  geom_point(aes(x=year, y=100*penroll), size = 2) +
  labs(title= "Title",
       x="Year",
       y="Number") +
  scale_y_continuous(sec.axis=sec_axis(~.*0.01, name="Percentage", labels=scales::percent))

ggp + theme_classic()

在这里插入图片描述

😑其实还是差一些意思的,看起来也就那样吧……以后如果用得上,再美化吧……

欢迎关注:猪猪的乌托邦

在这里插入图片描述

JFreeChart是一个流行的开源Java库,用于创建各种图表,包括层叠柱状图(Stacked Bar Chart)和折线图(Line Chart)。在JFreeChart中,你可以轻松地将这两种类型的图表结合在一起展示数据。 层叠柱状图通常用于比较各部分占总体的比例,每个类别中的条形会累积起来,显示的是累计值。而折线图则更适用于表示趋势和连续变化的数据。为了在JFreeChart中组合它们,可以按照以下步骤操作: 1. **创建柱状图**:首先创建一个`DefaultCategoryDataset`或自定义数据集,然后使用`BarPlot`构造柱状图2. **创建折线图**:同样创建一个`NumberAxis`和`XYPlot`,以及一个`XYSeriesCollection`来存储系列数据。 3. **添到同一个图表容器**:使用`CombinedDomainXYPlot`,它允许你在同一个坐标系中叠多个`XYPlot`。首先创建一个主轴,然后添柱状图的`BarPlot`和折线图的`XYPlot`作为子图。 4. **绘制层叠效果**:设置`BarPlot`的`stackMode`属性为`XY.StackMode.STACKED`,使其堆叠显示。 5. **生成图表组件**:最后,创建一个`JFreeChart`实例并将其渲染成`JFrame`、`JPanel`或其他支持的输出形式。 示例代码可能会像这样: ```java import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.*; ... // 创建数据 DefaultCategoryDataset dataset = ...; // 创建柱状图 BarChart barChart = ChartFactory.createStackedBarChart( "标题", // X轴标签 "Y轴标签", // Y轴标签 dataset, // 数据集 new CategoryPlot( ..., // 其他柱状图配置项 new StackedBarRenderer() // 使用堆叠模式 ) ); // 创建折线图 XYLineSeries xySeries = ...; XYPlot linePlot = new XYPlot(xySeries, ..., ...); linePlot.setDataset(0, ...); // 设置数据集 // 组合图表 CombinedDomainXYPlot combinedPlot = new CombinedDomainXYPlot(barChart.getDomainAxis()); combinedPlot.add(linePlot, 1); // 最后创建JFreeChart JFreeChart combinedChart = new JFreeChart(combinedPlot); ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值