博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用rstudio画散点图_R中的散点图
阅读量:2530 次
发布时间:2019-05-11

本文共 5580 字,大约阅读时间需要 18 分钟。

用rstudio画散点图

Scatterplots in R are the simplest form of graphs that plot two vector variables against each other. These are useful to know the distribution and density of a variable relative to another.

R中的散点图是最简单的图形形式,它们将两个向量变量相互绘制。 这些对于了解变量相对于另一个变量的分布和密度很有用。

Suppose you wish to plot the heights of children against their age and see how tall most children are for the given age, the best plot to use is a scatter plot.

假设您希望根据孩子的年龄绘制其身高,并查看给定年龄段的大多数孩子的身高,则最好使用散点图。

These can also be applied to situations like lung capacity vs hours of exercise, time of the day vs employee logins, week of the month vs daily sales, etc.

这些也可以应用于肺活量与运动时间,一天中的时间与员工登录,每月中的一周与每日销售额等情况。

We will begin by plotting some scatterplots using the R’s basic package and then move onto visualizing similar graphs in 3D using R.

我们将首先使用R的基本软件包绘制一些散点图,然后再使用R可视化3D中的相似图形。

在R中创建散点图 (Creating Scatterplots in R)

The simplest scatterplot can be created using a plot(x,y) command, where x and y are . Let us look at an example using some in-built R datasets.

可以使用plot(x,y)命令创建最简单的散点图,其中x和y是 。 让我们来看一个使用一些内置R数据集的示例。

The iris dataset in R is a collection of 150 observations across 5 variables concerning the iris flower. These variables indicate the dimensions of flowers such as sepal length/width and petal length/width. Let us try plotting the petal width of the flowers against the petal length observed.

R中的鸢尾花数据集是关于鸢尾花的5个变量的150个观测值的集合。 这些变量指示花朵的尺寸,例如萼片长度/宽度和花瓣长度/宽度。 让我们尝试根据观察到的花瓣长度绘制花朵的花瓣宽度。

> plot(iris$Petal.Length,iris$Petal.Width)
Iris Scatterplot
Iris Scatterplot
虹膜散点图

This gives us our first scatterplot. The graph is easy to read and tells us that some of the petals have a length of 1 to 2 cm, with a petal width of less than a centimeter – around 0.5 cm to be more precise.

这是我们的第一个散点图。 该图易于阅读,并告诉我们一些花瓣的长度为1到2厘米,花瓣的宽度小于一厘米-更精确地说约为0.5厘米。

Additionally, most of the petals are longer (3-7 cm) and tend to have wider petals that are mostly clustered around 1 to 2 cm. We can also clearly say that there is a weakly positive, slightly linear relationship between the petal length and width.

此外,大多数花瓣较长(3-7厘米),并且倾向于具有较宽的花瓣,大部分簇集在1-2厘米左右。 我们还可以清楚地说,花瓣的长度和宽度之间存在微弱的线性关系。

How do we make these graphs more informative? Observe that the iris dataset has 3 species of flowers. We can have a scatterplot that plots the 3 species with 3 different colors for the same variables we have chosen above.

我们如何使这些图更具参考价值? 观察虹膜数据集有3种花。 我们可以有一个散点图,用上面选择的相同变量绘制3种具有3种不同颜色的物种。

All you need to do is use an unclass() function against the col argument when plotting the graph. The unclass function takes the iris$species as an argument and the colors of the dots in the plot get assigned according to this. We also add the label, title and legend to make the graph look more appealing. Let us look at the code snippet.

您要做的就是在绘制图形时对col参数使用unclass()函数。 unclass函数将iris $ species作为参数,并根据此点分配图形中点的颜色。 我们还添加了标签,标题和图例,以使图形看起来更具吸引力。 让我们看一下代码片段。

plot(iris$Petal.Length, iris$Petal.Width, pch=20, col=c("red","green","blue")[unclass(iris$Species)], xlab="Petal Length", ylab="Petal Width" ,main="Iris length vs width by species")legend("topleft",c("Setosa","Virginica","Versicolor"), pch=20, col=c('red','green','blue'), title="Species")

The resulting graph is shown below.

结果图如下所示。

Iris Scatterplot With Colors
Iris Scatterplot With Colors
虹膜散点图与颜色

This graph carries far more information than the simple one we created above such as – Setosa is the smallest species in the lot and Versicolor is the largest. Also, there are some Virginica irises that are large enough to look like Versicolor ones. All these interpretations are possible with a legend we have drawn above the graph.

该图所包含的信息远远超过我们上面创建的简单图,例如– Setosa是批次中最小的物种,而Versicolor是最大的物种。 此外,还有一些维珍妮卡虹膜,其大小足以看起来像Versicolor虹膜。 通过在图上方绘制的图例,所有这些解释都是可能的。

创建3D散点图 (Creating 3D Scatterplots)

Sometimes, we might also be interested in creating scatterplots that involve 3 variables instead of 2.

有时,我们可能还想创建包含3个变量而不是2个变量的散点图。

While R’s basic graphics have support for only 2D scatterplots, there is another package named scatterplot3d that accomplishes this purpose. We will begin by installing this package.

尽管R的基本图形仅支持2D散点图,但是还有另一个名为scatterplot3d的软件包可以实现此目的。 我们将从安装此软件包开始。

install.packages("scatterplot3d")package ‘scatterplot3d’ successfully unpacked and MD5 sums checkedThe downloaded binary packages are inC:\Users\JournalDev\AppData\Local\Temp\Rtmpc7b8JR\downloaded_packages

Now make sure that you load the installed package to your environment.

现在确保将已安装的程序包加载到您的环境中。

library("scatterplot3d")

So let us create a 3D scatterplot for three variables from the iris dataset.

因此,让我们为虹膜数据集中的三个变量创建3D散点图。

> scatterplot3d(iris$Petal.Length,iris$Petal.Width,iris$Sepal.Length)
3d Iris Scatterplot
3d Iris Scatterplot
3d虹膜散点图

Now, let us try plotting a different color for each class as above. However, the unclass function will not work in this case.

现在,让我们尝试为上述每个类绘制不同的颜色。 但是,在这种情况下,unclass函数将不起作用。

We need to convert the species variable into a factor using the as.factor() function. Let us also add x-label, y-label, z-label and title to the graph like above.

我们需要使用as.factor()函数将物种变量转换为一个因子。 让我们还将x-label,y-label,z-label和标题添加到上面的图形中。

> spec <- as.factor(iris$Species)> scatterplot3d(iris$Petal.Length,iris$Petal.Width,iris$Sepal.Length, pch=20, xlab='Petal Length',ylab='Petal Width',zlab='Sepal Length',main="3D plot", color=c('red','green','blue')[spec])

The resulting 3D plot is as follows:

生成的3D图如下:

3d Iris Plot
3d Iris Plot
3d虹膜图

Scatterplots are simple yet important tools in data analysis. They are best used to identify outliers and get a general observation of the data characteristics.

散点图是数据分析中简单但重要的工具。 它们最适合用来识别异常值并获得对数据特征的一般观察。

翻译自:

用rstudio画散点图

转载地址:http://dgozd.baihongyu.com/

你可能感兴趣的文章
“==”运算符与equals()
查看>>
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>