如何创建多个条形图

Bar plots in R are the most frequently used plots in elementary statistics. These consist of horizontal or vertical bars representing a certain quantity associated with each entity in the dataset. For example, attendance days or students, no. of cars sold per model, no. of votes per party and so on.

R中的条形图是基本统计中最常用的图。 这些由水平或垂直条组成,这些水平或垂直条代表与数据集中的每个实体关联的特定数量。 例如,出勤天数或学生人数,没有。 每个型号售出的汽车数量 每个政党的票数等等。

R has some very useful functions to generate and customize bar plots. To illustrate these let us use the R’s in-built mtcars dataset, which is a collection of observations for 32 different cars across 11 different vehicular properties.

R具有一些非常有用的函数来生成和自定义条形图。 为了说明这些,让我们使用R的内置mtcars数据集,该数据集是针对11种不同车辆属性的32种不同汽车的观察值的集合。

This dataset is in the form of a data frame. More information about the dataset can be viewed in its R documentation page here. The documentation can also be accessed through your R console using ?mtcars.

该数据集采用数据框的形式。 有关数据集的更多信息,请参见此处的 R文档页面。 也可以使用?mtcars通过R控制台访问该文档。

R中的基本条形图 (Basic Bar Plot in R)

Let us take a look at the first 6 entries in the mtcars set.

让我们看一下mtcars集中的前6个条目。


> head(mtcars)mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

The gear column indicates how many gears are available in the car. Let us observe the column individually.

gear列指示汽车中可用的齿轮数。 让我们分别观察该列。


> mtcars$gear[1] 4 4 4 3 3 3 3 4 4 4 4 3 3 3 3 3 3 4 4 4 3 3 3 3 3 4 5 5 5 5 5 4

We can plot the frequency of the gears (how many cars are there with 3, 4 or 5 gears) in a bar plot. This is done by means of the barplot() function in R.

我们可以在条形图中绘制齿轮的频率(有3、4或5个齿轮的汽车数量)。 这是通过R中的barplot()函数完成的。


barplot(mtcars$gear)
Basic Bar Plot
基本条形图

But as you can see, this bar plot doesn’t reveal any information about the data in question. it simply plotted a bar for each one of the entries and left it to the user to understand the plot.

但是,正如您所看到的,此条形图不会显示有关数据的任何信息。 它只是为每个条目绘制一个条形图,然后将其留给用户以了解该图。

使用数据频率的条形图 (Bar Plots Using Data Frequency)

A barplot is the most meaningful when we can represent the data in frequency classes, i.e., how many data entries fall under each class. It would really help us to know how many cars have 3 gears or 5 gears and get a comprehensive comparison of these numbers.

当我们可以表示频率类别中的数据(即,每个类别下有多少个数据条目)时,条形图最有意义。 这确实有助于我们了解有多少辆汽车有3挡或5挡,并对这些数字进行了全面比较。

For this purpose, we need to plot the frequency of the mtcars$gear rather than the whole column. We use the table() function for this purpose.

为此,我们需要绘制mtcars$gear的频率,而不是整个列的频率。 为此,我们使用table()函数。


> table(mtcars$gear)3  4  5
15 12  5

Now we can plot the table into a barplot easily.

现在,我们可以轻松地将表格绘制到小图中。


> barplot(table(mtcars$gear))
Barplot With Frequency
频率条形图

This looks far more useful than the previous plot. It shows us that the maximum number of cars in the dataset has 3 gears and very few have 5 gears. Let us now annotate these graphs for making them even more useful.

这看起来比以前的情节要有用得多。 它向我们展示了数据集中的最大汽车数量为3个齿轮,很少有5个齿轮。 现在让我们注释这些图,以使其更加有用。

Let us try plotting the frequency of the cyl attribute which indicates how many cylinders a specific car has.

让我们尝试绘制cyl属性的频率,该频率指示特定汽车有多少个气缸。


> barplot(table(mtcars$cyl))
Barplot Cyl
Barplot Cyl

R中使用多个变量的条形图 (Bar Plots in R Using More Than One Variable)

It is also possible to split up these bar plots into sub-bars based upon any other categorical variable in the dataset. Two such variables in the outset are cyl – referring to the cylinders of a car, and am – referring to the transmission. Let us try splitting the bar plots using these attributes – making a table for both cyl and am together. This is commonly known as a stacked bar plot.

也可以根据数据集中的任何其他类别变量将这些条形图拆分为子条形。 首先,两个这样的变量是cyl(指汽车的汽缸)和am(指变速器)。 让我们尝试使用这些属性拆分条形图–为cyl和am共同创建一个表。 这通常称为堆积条形图。


> barplot(table(mtcars$cyl,mtcars$am))
Bar Plot With Categories
条形图与类别

美化并添加更多信息 (Beautifying and Adding More Info)

However, to make this graph more informative and more aesthetic, we need to change a few attributes of the barplot() function.

但是,为了使该图更具信息性和美观性,我们需要更改barplot()函数的一些属性。

  1. To have a separate bar for each am value (0 and 1), set the beside attribute to TRUE.要为每个am值(0和1)使用单独的条形,请将next属性设置为TRUE。
  2. You can change the barplot to horizontally oriented from the default vertical orientation by setting horiz attribute to TRUE.您可以更改barplot通过HORIZ属性设置为TRUE,水平地从默认的垂直方向导向。
  3. The title and axis labels are added in a similar manner to the plot() function.标题和轴标签以类似的方式添加到plot()函数。
  4. The names of each class can be specified using names.arg as a vector.可以使用names.arg作为向量来指定每个类的名称。
  5. Color is added using the col attribute.使用col属性添加颜色。

cars <- table(mtcars$am, mtcars$cyl)
barplot(cars,beside=TRUE,main="Car counts by cylinder - auto vs manual",ylab="No.of cars", names.arg=c("4","6","8"),col=c("red","blue"))
Barplot With Color
带颜色的条形图

在R中的条形图中添加图例 (Adding a Legend to Bar Plots in R)

Let us take this a step further and add a legend to the above code. Just paste the line after the code above.

让我们更进一步,并在上述代码中添加图例。 只需将代码行粘贴到上面的代码之后即可。


legend('top',c("Auto","Manual"),pch=15, col=c("red","blue"),title="No. of cars")

The pch 15 value refers to getting a filled square in the legend. We are displaying this legend on the top of our already plotted graph.

pch 15值是指在图例中获得实心的正方形。 我们将在已绘制图形的顶部显示此图例。

Barplot With Legend
带图例的地块

Thus we have created bar plots using the R graphics. Much more functionality can be added using the ggplot package which we will discuss in our further tutorials.

因此,我们使用R图形创建了条形图。 可以使用ggplot软件包添加更多功能,我们将在进一步的教程中进行讨论。

翻译自: https://www.journaldev.com/36249/bar-plots-in-r

如何创建多个条形图

如何创建多个条形图_在R中创建条形图相关推荐

  1. java 创建动态int数组_在Scala中创建动态增长数组的最佳方法是什么?

    如果要使用不可变结构,可以使用以下方法: scala> val orgList = List(1,2,3) orgList: List[Int] = List(1, 2, 3) scala> ...

  2. 创建队列 c语言_在C中创建队列

    创建队列 c语言 A queue in C is basically a linear data structure to store and manipulate the data elements ...

  3. matlab中find函数_在R中使用Matlab函数

    R, Matlab MATLAB是一款商业数学软件, R是一个拥有庞大工具库的数据统计.建模.可视化分析软件.R 不仅支持C/C++, python代码的运行和工程移植, 也支持在R中使用MATLAB ...

  4. 在R中创建晶须和盒图

    Box plots in R are a good way to measure and visualize how closely your data is distributed. These a ...

  5. 如何在 R 中创建人口金字塔

    人口金字塔是显示给定人口的年龄和性别分布的图表.这是一张有用的图表,可以轻松了解人口构成以及当前人口增长趋势. 如果人口金字塔呈矩形,则表明人口增长速度较慢:老一代正在被大小大致相同的新一代所取代. ...

  6. R 中创建绘制列联表

    R 中创建绘制列联表 可以使用该table函数在 R 中创建列联表.例如,考虑以下带有随机数据的示例, set.seed(20)X <- sample(c("Yes", &q ...

  7. 在idea中创建mybatis-config.xml模板(在idea中创建mybatis核心配置文件模板)

    在idea中创建mybatis-config.xml模板(在idea中创建mybatis核心配置文件模板) 1.写配置文件 2.设置 3.查看 1.写配置文件 先创建一个mybatis-config. ...

  8. a.创建动物类Animal,在该类中创建一个成员方法cry(), 输出“动物会发出叫声”,以及一个eat()方法,输出“动物需要食物”; b.创建一个Animal子类Dog类,在该类中重写父类的成员

    创建Zoo类作为主类,在main方法中分别创建各个类对象 ,并调用各自类的cry()方法, 创建Dog类的对象赋值给Animal类的对象,然后调用cry()和eat()方法. ** a.创建动物类An ...

  9. 创建视图SQL:在SQL Server中创建视图

    介绍 (Introduction) In this article, we are going to see how to use the CREATE VIEW SQL statement to c ...

最新文章

  1. CentOS中Mysql常用操作
  2. 使用bottle进行web开发:get的参数传递,form数据传递等
  3. python创建脚本文件_python创建文件备份的脚本
  4. 一个电脑同时运行 64bit 和 32bit 的eclipse 如何匹配 jdk环境
  5. js 正则中冒号代表什么_javascript中正则表达式语法详解
  6. 最大 / 小的K个数
  7. java二维数组省略_Java基础教程——二维数组
  8. tensorflow随机梯度下降算法使用滑动平均模型
  9. 6 款好用到爆的 JSON 处理工具,极大提高效率!
  10. VMWare16下载安装Centos7镜像教程,详细记录
  11. 万能五笔输入法弹窗_万能五笔输入法广告如何彻底关闭
  12. 2203-python跳转,窗口图标,提示框,消息盒子
  13. 计算机绘画小房子教案,小班美术教案小房子
  14. 计算机等级考试准考证打河南,2019年河南牧院计算机等级考试准考证打印时间...
  15. 方便实用的串口调试工具/串口调试助手/串口精灵 (工具一)
  16. C语言二级必须编译吗,c语言是计算机二级吗
  17. Vue 中实现Video视频不可快进
  18. 微博是这样炼成的:山寨QQ项目源码下载大全~
  19. 从阿里巴巴发行价看A股新股投资机会
  20. 四旋翼无人机学习第6节--SPL06气压传感器和MPU9250九轴传感器电路分析

热门文章

  1. 数据降维之多维缩放MDS(Multiple Dimensional Scaling)
  2. kettle的hello world
  3. sqlserver日期函数 dateadd,datediff ,datepart ,datename,convert
  4. oracle下查询的sql已经超出IIS响应时间
  5. LeetCode 712. Minimum ASCII Delete Sum for Two Strings
  6. 测试用例--因果图、判定表法
  7. Android 自定义RecyclerView 实现真正的Gallery效果
  8. 1002: Prime Path
  9. XE中rectangle实现渐变
  10. C/C++语言课程设计任务书