在同一张图上绘制两个因素
假设我有两个因素,我想将它们绘制在同一个图上,这两个因素具有相同的水平。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用lattice包获得稍微更详细的结果:
或者如果您想要男性/女性并排以便于比较:
You can achieve slightly more detailed results with lattice package:
Or if you want males/females side by side for easier comparison:
来自
?table
:当您执行
table(s1,s2)
时,函数会将s1
和s2
视为配对结果。实际上,它告诉您,如果您采用cbind(s1,s2)
,那么将会有 10 行男性-男性,10 行男性-女性,依此类推。要理解这一点,请考虑一个非常简单的例子:
您应该做的是:
From
?table
:When you do
table(s1,s2)
what happens is that the function considerss1
ands2
as paired results. Effectively it tells you that if you were to takecbind(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:
What you should do is:
产生稍微不同形式的绘图的两个选项是
和
前者是 spinplot,马赛克图的一种特殊情况,由
table
的plot
方法生成(第二个示例)。有关更多详细信息,请参阅?spineplot
和?mosaicplot
,如果您愿意,您可以直接使用这些函数,而不是通用的plot()
。另请查看 Meyer 等人在 CRAN 上的
vcd
包中的mosaic()
函数 (CRAN 上的 vcd 链接)table()
正在生成 这两个因素的列联表。Two options producing slightly different forms of plots are
and
The former is a spineplot a special case of the mosaic plot, which the
plot
method fortable
produces (the second example). See?spineplot
and?mosaicplot
for more details and you can use these functions directly, rather than the genericplot()
if you wish.Also take a look at the
mosaic()
function in thevcd
package on CRAN by Meyer et al (Link to vcd on CRAN)table()
is producing the contingency table for the two factors.嗯..我不认为卡梅伦想要创建列联表。如果我理解正确的话,我认为他想创建一个包含两个变量的数据框,其中 s1 和 s2 似乎是相同大小的向量。 (长度(s1)==长度(s2))。
在这种情况下,他只需要使用以下方法创建一个“表”(我认为他指的是 data.frame):
然后在同一个图中绘制 2 个系列。
至于绘制这些东西的第二个问题,我会使用 matplot。例如:
鉴于他将 2 个向量的数据组织在名为“df”的单个 data.frame 中,他可以执行以下操作:
希望这会有所帮助。
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:
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:
Given that he has his data of 2 vectors organized in a single data.frame named "df", he can just do something like:
Hope this helps.