动态生成多个领域的 R 点阵图
我想知道以下是否可能。
我有一项调查,包含 100 多个问题,全部是分类问题,收集于 4 个地点。所有问题都标记为 q1、q2 等。为简单起见,假设有 100 个。
我可以通过以下方式直观地比较跨位置的特定问题的结果:
library (lattice);
histogram(~ q75 | location, data = survey, layout=c(1,4));
或者使用 ggplot2,
library (ggplot2);
qplot(q75, data=survey) + facet_grid(location ~ .);
这为一个问题提供了 4 个垂直对齐的直方图。
我想知道是否有一种编程方式来生成所有 100个问题的直方图,所以最左边我有q1的4个直方图堆栈,然后是右边q2 的 4 个直方图堆栈,依此类推。当然,这将是一条很长的线,但这仅用于目视检查以及发现初始区域以进行进一步调查。向右滚动对我来说没问题,我有一台宽屏显示器,所以我可以一次获得相当数量的直方图。
问题用“q”+计数器标记是很好的。我不知道
- 如何用点阵(或ggplot2?)制作这种图,它是一个二维点阵。
- 如何将此类以编程方式生成的字段名称输入到这些命令中。
欢迎提出建议。我是一名程序员,但不是 R 领域的新手。
I'm wondering if the following is possible.
I have a survey with 100+ questions, all categorical, collected in 4 locations. All questions are labeled q1, q2 and so on. Let's say there are 100 for simplicity.
I can visually compare the results of a particular question across locations with:
library (lattice);
histogram(~ q75 | location, data = survey, layout=c(1,4));
or, with ggplot2,
library (ggplot2);
qplot(q75, data=survey) + facet_grid(location ~ .);
This gives 4 histograms, aligned vertically, for one question.
I'm wondering if there is a programmatic way to generate the histograms for all 100 questions, so left most I have the stack of 4 histograms for q1, then to the right the stack of 4 histograms for q2 and so on. Granted, it's going to be a long line, but this is for visual inspection only and for spotting the initial areas to investigate further. Scrolling to the right is fine with me, I have a wide-screen monitor so I'll get a decent number of histograms fitting in at one time.
That the questions are labeled with 'q' + a counter is good. What I don't know is
- how to make this kind of plot with lattice (or ggplot2?) it's a bi-dimensional lattice.
- how to feed such programmatically-generated field names into these commands.
Suggestions are appreciated. I'm a programmer, but not in R where I'm a newbie.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
诀窍在于以正确的格式获取数据。您需要一个包含 3 列的数据框:问题、位置和分数。 (
reshape
包可以帮助您操作数据集。)之后,绘制直方图就很容易了。
格子:
ggplot2:
The trick is to get the data in the correct format. You want a data frame with 3 columns: the question, the location and the score. (The
reshape
package may assist you in manipulating your dataset.)After that, drawing the histograms are easy.
lattice:
ggplot2: