在R中按组别分层的图谱需要哪些组成部分[封闭式]

问题描述 投票:-4回答:1

我想在图形图中显示由第三个变量(分组变量)分隔的两个变量之间的客户关系。

有哪些选项可以用来显示在第三个变量上分组的x和y变量之间的数据?

r plot graph data-visualization information-extraction
1个回答
2
投票

如果你给绘图函数1个参数,它将做出一个一维的条形图。

如果你给绘图函数2的值(必须是数值),它将给你一个标准的代数图,水平轴与垂直轴。

如果你给绘图函数3个值(2个数值,一个因子,那么你就可以绘制点,但要给它们涂上颜色标签),然后你要让标签对用户很明显。

使用R的内置数据集 "Orange",你可以做一个这样的图。

> View(Orange)
> summary(Orange)

plot(Orange$age, Orange$circumference, col = rainbow(5)[Orange$Tree], pch = 16, main = "Correlating Tree Age by Circumference", grid(nx = 25, ny = 25)) legend("topleft", title = "Orange Trees", fill = rainbow(5), levels(Orange$Tree))

注:彩虹(5)?为什么是5?因为列树有1-5为因子。既然你有3个不同的化妆品品牌,你应该做彩虹(3)。

这就是你如何得到线性回归线,如果它工作。你必须使用线性模型(lm)函数。

> model <- lm(Orange$circumference ~ Orange$age)
> summary(model)
> abline(model) 

Plot Function with grid and regression line

你也可以用网格库中的xyplot来做

> library(xyplot)
> xyplot(circumference ~ age| Tree, data = Orange, type = c("p", "g", "r"), main = "Plots of Orange Age vs Circumference for 5 Orange Trees")

xyplot 3 variable plot

我没有给我的点涂上颜色,但我不需要。虽然我喜欢这个图,但我认为用绘图函数涂色更适合做统计判断,因为它把所有因素都放在同一个图中。

疑问。这些函数是如何工作的,等等?

>?plot
>?xyplot
>?Orange 

scatterplot3d函数也非常酷。你可以用它做一个三维图,但是如何判断相关性,会受到你设置的视图的 "角度 "的影响。

而且你还可以用xyplot函数做出一个更酷的图。一个每个因子都有多条回归线的图。

>xyplot(circumference ~ age, data = Orange, groups = Tree, type = c("p", "g", "r"), main = "Plots of Orange Age vs Circumference for 5 Orange Trees", pch = 16, auto.key = TRUE)

enter image description here

我使用auto.key命令的图例非常糟糕。我相信,它可以被改进!

如果你想绘制两个变量:一个数值变量和一个因子变量,你可以这样做。你可以使用tapply函数 在这里,我使用tapply函数来计算每个Tree的所有周长。然后使用barplot函数。这可能就是你所想的。

> sum_table <- tapply(Orange$circumference, Orange$Tree, FUN = sum)
> sum_table <- sort.default(sum_table, decreasing = TRUE, na.last = NA)
> barplot(sum_table, xlab = "Trees", ylab = "Circumference", main = "Sum of Circumferences for all 5 Orange Trees", col = "dodgerblue1"))

barplot_of_sums

好了,不要紧,当一个数值变量与另一个因子变量列在一起时,绘图函数默认为制作boxplots。

> plot(Orange$Tree, Orange$circumference, main = "Boxplots of Orange Circumference vs Orange Trees", xlab = "Orange Trees", ylab = "Circumference")

Wow! Rplot makes barplots

© www.soinside.com 2019 - 2024. All rights reserved.