r语言中进行数据可视化
R programming was developed in 1993 for making graphs and producing statistical results. There are many libraries in R language that can be used for making graphs and producing statistical data.
R编程于1993年开发,用于制作图形和产生统计结果。 R语言中有许多库可用于制作图形和生成统计数据。
There are many steps that have to be taken into consideration for doing data analysis through this language. These steps are:
通过这种语言进行数据分析时,必须考虑许多步骤。 这些步骤是:
- Programming 程式设计
- Transforming 转型
- Discovering 发现
- Modeling 造型
- Communicating 沟通交流
R programming is being used in many industries like academics, healthcare, government, insurance, retail, media, manufacturing, etc.
R编程已在许多行业中使用,例如学术界,医疗保健,政府,保险,零售,媒体,制造业等。
This data analysis can be done through programming in R language which comes with a number of packages having many inbuilt functions and this is the reason that developers do not have to program much. They just need to use those functions and carry out the analysis.
可以通过R语言编程来完成此数据分析,R语言带有许多具有许多内置功能的程序包,这是开发人员不必进行太多编程的原因。 他们只需要使用这些功能并进行分析即可。
为什么要使用R编程? (Why R Programming?)
R Programming can be used to create statistics and graphs. The language has become very popular and people who want to make their career in this language can undergo R certification online through various institute who provide in-depth knowledge of R.
R编程可用于创建统计数据和图形。 该语言已变得非常流行,并且想要使用该语言进行职业的人们可以通过提供R深入知识的各种机构在线获得R认证 。
R certification when seen today can prove to be very useful for students and they can plan a good career after the certification which certainly gives an individual an upper edge over others.
今天看到的R认证可能对学生非常有用,并且他们可以在认证之后计划一个良好的职业,这肯定会使个人在其他方面拥有上风。
R中的数据可视化 (Data Visualization in R)
Many types of data visualizations can be created through the language and these are:
通过该语言可以创建多种类型的数据可视化,它们是:
- Histogram 直方图
- Bar / Line Chart 条形图/折线图
- Box plot 箱形图
- Scatter plot 散点图
- Heat Map 热图
- Mosaic Map 镶嵌图
- Map Visualization 地图可视化
- 3D Graphs 3D图形
- Correlogram 相关图
These will be discussed one by one.
这些将一一讨论。
直方图 (Histogram)
A histogram can be created by using histdata package that has many small data sets to create the histogram. A histogram can be used to break data into bins and show their frequency. Here is the code in which histogram is created. Here is the code of creating a simple histogram.
直方图可以通过使用具有许多小的数据集来创建直方图的histdata包来创建。 直方图可用于将数据分解为bin,并显示其频率。 这是创建直方图的代码。 这是创建简单直方图的代码。
hist(Temperature,
main="Maximum daily temperature ",
xlab="Temperature in degrees Fahrenheit",
xlim=c(50,100),
col="darkmagenta",
freq=FALSE
)
While creating a histogram a developer can see that number of colors specified is more than the number of breaks. The colors are repeated if the number of colors is more.
在创建直方图时,开发人员可以看到指定的颜色数量大于中断的数量。 如果颜色数量更多,则重复颜色。
折线图 (Line Chart)
A line chart shows an increase in the data for a given time period. Line charts are created to compare the changes between two organizations or between weather of two places or other comparisons. Line charts are also created to analyze the trends over a particular period.
折线图显示给定时间段内数据的增加。 创建折线图是为了比较两个组织之间或两个地方的天气之间的变化或进行其他比较。 还创建折线图以分析特定时期内的趋势。
x <- c(1:5); y <- x
par(pch=22, col="red")
par(mfrow=c(2,4))
opts = c("p","l","o","b","c","s","S","h")
for(i in 1:length(opts)){
heading = paste("type=",opts[i])
plot(x, y, type="n", main=heading)
lines(x, y, type=opts[i])
}
条形图 (Bar Chart)
Bar chart is also created for comparing profits or weather report or other comparisons. The chart is displayed in the form of bars. Here is the code of creating a vertical bar chart.
还创建了条形图,用于比较利润或天气预报或其他比较。 图表以条形形式显示。 这是创建垂直条形图的代码。
counts <- table(mtbikes$gear)
barplot(counts, main="Bike Distribution",
xlab="Number of Gears")
Here is an example of creating a horizontal bar chart.
这是创建水平条形图的示例。
counts <- table(mtbikes$gear)
barplot(counts, main="Bike Distribution", horiz=TRUE,
names.arg=c("3 Gears", "4 Gears", "5 Gears"))
箱形图 (Box Plot)
It is created either for a single variable or a group of variables. The syntax of creating a box plot is as follows
它是为单个变量或一组变量创建的。 创建箱形图的语法如下
boxplot(x,data=)
Here x is the formula and data= is the frame which provides the data. Here is an example of creating a box plot.
这里x是公式,data =是提供数据的框架。 这是创建箱形图的示例。
boxplot(mpg~cyl,data=mtbikes, Bike Milage Data",
xlab="Number of Cylinders", ylab="Miles Per Gallon")
散点图 (Scatter Plot)
Scatter plot can be created in many ways. The basic formula for creating the scatter plot is
散点图可以通过多种方式创建。 创建散点图的基本公式是
plot(x, y)
Here is an example of creating a scatter plot.
这是创建散点图的示例。
attach(mtbikes)
plot(wt, mpg, main="Scatterplot Example",
xlab="Bike Weight ", ylab="Miles Per Gallon ", pch=19)
热图 (Heat Map)
A heat map is displayed in the form of a table in which colors are displayed in place of numbers. All the columns can have either same or different colors. The dark color denotes highs while the light colors denote lows. Here is an example of creating a heat map.
热图以表格的形式显示,其中以颜色代替数字来显示颜色。 所有列可以具有相同或不同的颜色。 深色表示高,而浅色表示低。 这是创建热图的示例。
> heatmap(as.matrix(mtcars))
镶嵌图 (Mosaic Map)
Mosaic Map can be created by using VCD library which has an ample amount of functions to create the map. The syntax for creating a mosaic map is as follows:
可以使用VCD库创建马赛克地图,该库具有创建地图的大量功能。 创建镶嵌图的语法如下:
mosaic(x, condvar=, data=)
Here x is the formula and codevar is an optional variable in which conditions can be defined. The example below shows the making of a mosaic map
这里x是公式,而codevar是可以在其中定义条件的可选变量。 以下示例显示了镶嵌图的制作
library(vcd)
mosaic(HairEyeColor, shade=TRUE, legend=TRUE)
3D图形 (3D Graphs)
R programming can be used to create 3D graphs which are very impressive. The R commander package is used to create these graphs. In order to create the 3D graph R commander package has to be installed and then 3D plot option should be used in the graph.
R编程可用于创建令人印象深刻的3D图形。 R命令程序包用于创建这些图。 为了创建3D图形,必须安装R命令程序包,然后在图形中使用3D绘图选项。
Here is the code for creating the graph:
这是用于创建图形的代码:
>data(iris, package="datasets")
>scatter3d(Petal.Width~Petal.Length+Sepal.Length|Species, data=iris, fit="linear"
>residuals=TRUE, parallel=FALSE, bg="black", axis.scales=TRUE, grid=TRUE, ellipsoid=FALSE)
Lattice package can also be used to create 3D graphs.
莱迪思软件包还可用于创建3D图形。
Here is an example
这是一个例子
>attach(iris)
>cloud(Sepal.Length~Sepal.Width*Petal.Length|Species, main="3D Scatterplot by Species")
>xyplot(Sepal.Width ~ Sepal.Length, iris, groups = iris$Species, pch= 20)
相关图 (Correlogram)
Correlogram helps the users to view the data in the form of matrices. The syntax for creating a correlogram is given below.
关联图可帮助用户以矩阵形式查看数据。 下面给出了创建相关图的语法。
corrgram(x, order = , panel=, lower.panel=, upper.panel=, text.panel=, diag.panel=)
Here
这里
Order=TRUE will set the variables in proper order in relation to the correlation matrix.
Order = TRUE将按照相关矩阵的正确顺序设置变量。
Panel= refers to diagonal panels in which developers can use lower= and upper=. These options can be chosen below and above the diagonal. Text.panel and diag.panel are the references to main diagonal.
Panel =是指对角线面板,开发人员可以在其中使用lower =和upper =。 可以在对角线的下方和上方选择这些选项。 Text.panel和diag.panel是对角线的引用。
Here is an example of creating a correlogram.
这是创建相关图的示例。
library(corrgram)
corrgram(mtbikes, order=TRUE, lower.panel=panel.shade,
upper.panel=panel.pie, text.panel=panel.txt,
main="Bike Milage Data in PC2/PC1 Order")
地图可视化 (Map Visualization)
This is the latest thing that has been put into the R programming. R can provide the map visualization through JavaScript libraries. The leaflet is open source through which JavaScript libraries can be used for creating interactive maps. In order to use the library, it should be installed.
这是R编程中最新的东西。 R可以通过JavaScript库提供地图可视化。 该传单是开放源代码,通过它可以将JavaScript库用于创建交互式地图。 为了使用该库,应该安装它。
library(magrittr)
library(leaflet)
m <- leaflet() %>%
addTiles() %>%
addMarkers(lng=77.2310, lat=28.6560, popup="The delicious food of India")
m
己宾 (Hexbin)
Hexbin is a package, which helps to create multiple points This package can be used to create a bivariate histogram. Here is the code for the same.
Hexbin是一个程序包,可帮助创建多个点。此程序包可用于创建双变量直方图。 这是相同的代码。
>library(hexbin)
>a=hexbin(diamonds$price,diamonds$carat,xbins=40)
>library(RColorBrewer)
>plot(a)
最终裁决 (Final Verdict)
It can be said that various kinds of graphs can be made from R language by writing few lines of code and embedding packages and functions, which have already been coded.
可以说,通过编写很少的代码行并嵌入已经编码的包和函数,可以用R语言制成各种图形。
These charts can be used on the web as well as desktop applications and can help you for data visualization in R in a specific way.
这些图表可以在Web以及桌面应用程序上使用,并且可以帮助您以特定方式在R中进行数据可视化。
翻译自: https://www.thecrazyprogrammer.com/2018/12/data-visualization-in-r.html
r语言中进行数据可视化
R编程语言在数据可视化方面非常强大,提供了多种图形类型,如直方图、折线图、条形图、箱形图、散点图、热图、镶嵌图、3D图形、相关图和地图可视化等。通过简单的代码和丰富的库,开发者可以轻松创建统计图表,适用于各种行业,包括学术、医疗、政府等。此外,R语言还支持创建交互式地图,如使用Leaflet库。无论是初学者还是专业人士,R都是数据可视化的一个优秀选择。

4万+

被折叠的 条评论
为什么被折叠?



