动态生成多个领域的 R 点阵图

发布于 2024-09-14 03:41:45 字数 728 浏览 9 评论 0原文

我想知道以下是否可能。

我有一项调查,包含 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 技术交流群。

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

发布评论

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

评论(1

回忆凄美了谁 2024-09-21 03:41:45

诀窍在于以正确的格式获取数据。您需要一个包含 3 列的数据框:问题、位置和分数。 (reshape 包可以帮助您操作数据集。)

n_questions <- 100
n_responses <- c(North = 89, East = 37, South = 57, West = 71)
n_locations <- length(n_responses)
total_responses <- sum(n_responses)

survey <- data.frame(
  question = unlist(lapply(n_responses, function(x) rep(seq_len(n_questions), each = x))),      
  location = rep(c("North", "East", "South", "West"), times = n_questions * n_responses),
  score = sample(n_questions, n_questions * total_responses, replace = TRUE)
)

之后,绘制直方图就很容易了。

格子:

library(lattice)
library(latticeExtra)
useOuterStrips(histogram(~ score | question * location, data = survey))

ggplot2:

library(ggplot2)
ggplot(survey, aes(score)) + geom_histogram() + facet_grid(location ~ question)

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.)

n_questions <- 100
n_responses <- c(North = 89, East = 37, South = 57, West = 71)
n_locations <- length(n_responses)
total_responses <- sum(n_responses)

survey <- data.frame(
  question = unlist(lapply(n_responses, function(x) rep(seq_len(n_questions), each = x))),      
  location = rep(c("North", "East", "South", "West"), times = n_questions * n_responses),
  score = sample(n_questions, n_questions * total_responses, replace = TRUE)
)

After that, drawing the histograms are easy.

lattice:

library(lattice)
library(latticeExtra)
useOuterStrips(histogram(~ score | question * location, data = survey))

ggplot2:

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