在同一张图上绘制两个因素

发布于 2024-09-29 15:42:04 字数 535 浏览 2 评论 0原文

假设我有两个因素,我想将它们绘制在同一个图上,这两个因素具有相同的水平。

s1 <- c(rep("male",20), rep("female", 30))
s2 <- c(rep("male",10), rep("female", 40))
s1 <- factor(s1, levels=c("male", "female"))
s2 <- factor(s2, levels=c("male", "female"))

我本以为使用表函数会产生正确的绘图结果,但它弹出了。

table(s1, s2)
        s2 
s1       male female   
male     10     10   
female    0     30

所以实际上有两个问题,表函数是做什么来获得这个结果的,以及我可以使用什么其他函数来使用具有相同级别的函数创建具有 2 个系列的图形?

另外,如果这是一个因素,我会使用 gplots 包中的 barplot2 来绘制它。

Say I have two factors and I want to graph them on the same plot, both factors have the same levels.

s1 <- c(rep("male",20), rep("female", 30))
s2 <- c(rep("male",10), rep("female", 40))
s1 <- factor(s1, levels=c("male", "female"))
s2 <- factor(s2, levels=c("male", "female"))

I would have thought that using the table function would have produced the correct result for graphing but it pops out.

table(s1, s2)
        s2 
s1       male female   
male     10     10   
female    0     30

So really two questions, what is the table function doing to get this result and what other function can i use to create a graph with 2 series using functions with the same levels?

Also if it is a factor I'm using barplot2 in the gplots package to graph it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉南桥 2024-10-06 15:42:04

您可以使用lattice包获得稍微更详细的结果:

s1 <- factor(c(rep("male",20), rep("female", 30)))
s2 <- factor(c(rep("male",10), rep("female", 40)))
D <- data.frame(s1, s2)

library(lattice)
histogram(~s1+s2, D, col = c("pink", "lightblue"))

alt text

或者如果您想要男性/女性并排以便于比较:

t1 <- table(s1)
t2 <- table(s2)
barchart(cbind(t1, t2), stack = F, horizontal = F)

替代文字

You can achieve slightly more detailed results with lattice package:

s1 <- factor(c(rep("male",20), rep("female", 30)))
s2 <- factor(c(rep("male",10), rep("female", 40)))
D <- data.frame(s1, s2)

library(lattice)
histogram(~s1+s2, D, col = c("pink", "lightblue"))

alt text

Or if you want males/females side by side for easier comparison:

t1 <- table(s1)
t2 <- table(s2)
barchart(cbind(t1, t2), stack = F, horizontal = F)

alt text

画离情绘悲伤 2024-10-06 15:42:04

来自 ?table

“table”使用交叉分类因素来构建意外事件
每个因子水平组合的计数表。

当您执行 table(s1,s2) 时,函数会将 s1s2 视为配对结果。实际上,它告诉您,如果您采用cbind(s1,s2),那么将会有 10 行男性-男性,10 行男性-女性,依此类推。

要理解这一点,请考虑一个非常简单的例子:

a <- c("M","M","F","F")
b <- c("F","F","M","M")
table(a,b)

  b
a   F M
  F 0 2
  M 2 0

您应该做的是:

t1 <- table(s1)
t2 <- table(s2)
barplot(cbind(t1,t2), beside=TRUE, col=c("lightblue", "salmon"))

From ?table:

‘table’ uses the cross-classifying factors to build a contingency
table of the counts at each combination of factor levels.

When you do table(s1,s2) what happens is that the function considers s1 and s2 as paired results. Effectively it tells you that if you were to take cbind(s1,s2) then there would be 10 rows of male-male, 10 of male-female and so on.

To understand this consider a very trivial example:

a <- c("M","M","F","F")
b <- c("F","F","M","M")
table(a,b)

  b
a   F M
  F 0 2
  M 2 0

What you should do is:

t1 <- table(s1)
t2 <- table(s2)
barplot(cbind(t1,t2), beside=TRUE, col=c("lightblue", "salmon"))
记忆で 2024-10-06 15:42:04

产生稍微不同形式的绘图的两个选项是

plot(s1, s2)

plot(table(s1,s2))

前者是 spinplot,马赛克图的一种特殊情况,由 tableplot 方法生成(第二个示例)。有关更多详细信息,请参阅 ?spineplot?mosaicplot,如果您愿意,您可以直接使用这些函数,而不是通用的 plot()

另请查看 Meyer 等人在 CRAN 上的 vcd 包中的 mosaic() 函数 (CRAN 上的 vcd 链接)

table() 正在生成 这两个因素的列联表

Two options producing slightly different forms of plots are

plot(s1, s2)

and

plot(table(s1,s2))

The former is a spineplot a special case of the mosaic plot, which the plot method for table produces (the second example). See ?spineplot and ?mosaicplot for more details and you can use these functions directly, rather than the generic plot() if you wish.

Also take a look at the mosaic() function in the vcd package on CRAN by Meyer et al (Link to vcd on CRAN)

table() is producing the contingency table for the two factors.

凹づ凸ル 2024-10-06 15:42:04

嗯..我不认为卡梅伦想要创建列联表。如果我理解正确的话,我认为他想创建一个包含两个变量的数据框,其中 s1 和 s2 似乎是相同大小的向量。 (长度(s1)==长度(s2))。

在这种情况下,他只需要使用以下方法创建一个“表”(我认为他指的是 data.frame):

df = data.frame(s1=s1, s2=s2);

然后在同一个图中绘制 2 个系列。

至于绘制这些东西的第二个问题,我会使用 matplot。例如:

matplot(1:10, data.frame(a=rnorm(10), b=rnorm(10)), type="l", lty=1, lwd=1, col=c("blue","red"))

鉴于他将 2 个向量的数据组织在名为“df”的单个 data.frame 中,他可以执行以下操作:

matplot(df, type="l", lty=1, lwd=1, col=c("blue","red"))

希望这会有所帮助。

Hmm.. I don't think creating a contingency table is what Cameron was looking for. If I understood him correctly, I think he wanted to create a data frame with two variables in it, where s1 and s2 seems to be vectors of the same size. (length(s1)==length(s2)).

In this case, he would simply need to create a "table" (I think he meant data.frame) using:

df = data.frame(s1=s1, s2=s2);

And then plot the 2 series in the same plot.

So as for the second question of plotting these things, I'd use matplot. For example:

matplot(1:10, data.frame(a=rnorm(10), b=rnorm(10)), type="l", lty=1, lwd=1, col=c("blue","red"))

Given that he has his data of 2 vectors organized in a single data.frame named "df", he can just do something like:

matplot(df, type="l", lty=1, lwd=1, col=c("blue","red"))

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文