在 R 中使用多因素生成单独的点图

发布于 2024-10-31 23:48:16 字数 440 浏览 2 评论 0原文

我正在尝试在 R 中生成具有多个因素的单个点图,该图与 minitab 生成的点图类似。

我已经能够在 R 中生成单独的点图,但我不知道如何向 x 轴添加分组。例如,我有一个颜色和一个类型,我想在 x 轴上显示,每个类型都有两种颜色,我希望将其放在同一个图表上,但无法弄清楚点图。

结果应该与此分组类似,但是是一个点图,并且只有 1 个图表,我只是想说明分组。

http://ajpendo.physicalology.org/content/280/5 /E804/F6.small.gif

抱歉,无法发布图像,太菜鸟了:)

我已经搜索过网络,但找不到太多帮助。

有什么想法吗?

I'm trying to generate an individual dot plot with multiple factors in R that's similar to the one produced by minitab.

I've been able to produce the individual dot plot in R but I can't figure out how to add grouping to the x-axis. For example I have a Color and a Type that I'd like to display on the x-axis, each Type has two colors and I'd like this put on the same graph but can't figure it out for a dot plot.

The result should look similar to this grouping but a dot plot instead and only have 1 graph, I'm just trying to illustrate the grouping.

http://ajpendo.physiology.org/content/280/5/E804/F6.small.gif

sorry can't post the image, too much of a noob to stack :)

I've scoured the web but can't find much help.

Any ideas?

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

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

发布评论

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

评论(2

谁的年少不轻狂 2024-11-07 23:48:16

您可以在 R 中通过多种方式执行此操作。

第一种是使用 lattice 包:

library(lattice)
xyplot(lat ~ long | cut(depth, 3),
        data = quakes, 
        aspect = "iso", 
        pch = ".", 
        cex = 2, 
        type = c("p", "g"), 
        xlab = "Longitude", 
        ylab = "Latitude", 
        strip = strip.custom(strip.names = TRUE, var.name = "Depth"))

在此处输入图像描述

第二个在 ggplot 中:

library(ggplot2)
df <- quakes
df$cut_depth <- cut(quakes$depth, 3)
ggplot(quakes, aes(x=long, y=lat)) + geom_point() + facet_grid(.~cut_depth)

在此处输入图像描述

You can do this in a number of ways in R.

The first is to use the lattice package:

library(lattice)
xyplot(lat ~ long | cut(depth, 3),
        data = quakes, 
        aspect = "iso", 
        pch = ".", 
        cex = 2, 
        type = c("p", "g"), 
        xlab = "Longitude", 
        ylab = "Latitude", 
        strip = strip.custom(strip.names = TRUE, var.name = "Depth"))

enter image description here

And the second is in ggplot:

library(ggplot2)
df <- quakes
df$cut_depth <- cut(quakes$depth, 3)
ggplot(quakes, aes(x=long, y=lat)) + geom_point() + facet_grid(.~cut_depth)

enter image description here

谁的新欢旧爱 2024-11-07 23:48:16

您可以使用 ggplot 最直接地执行此操作。假设您有一个包含以下列的 data.frame:

dta := x, y, color, condition

其中 x,y 是数字,color, condition 是因子。然后你可以执行以下操作:

require(ggplot2)
ggplot(dta, aes(x = x, y = y, color = color)) + geom_point() + facet_wrap(~ condition)

或者,在常规 R 中你可以执行以下操作:

plot(dta$x, dta$y, col = dta$color, pch = dta$condition)

但是,这可能太忙了。

吉姆

You can use ggplot to do this most directly. Let's say you have a data.frame with the following columns:

dta := x, y, color, condition

where x,y are numeric and color, condition are factors. Then you can do the following:

require(ggplot2)
ggplot(dta, aes(x = x, y = y, color = color)) + geom_point() + facet_wrap(~ condition)

alternatively, in regular R you could do:

plot(dta$x, dta$y, col = dta$color, pch = dta$condition)

however, this might be way too busy.

jim

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