R中有没有办法在不将数据从宽格式转换为长格式的情况下绘制分组的点图?

发布于 2025-02-12 09:23:14 字数 1182 浏览 2 评论 0原文

我正在使用一个看起来像:

df = data_frame(
A=c(sample(0:2,size=50,replace=TRUE)),
A1=c(sample(0:50,size=50,replace=TRUE)),
A2=c(sample(0:50,size=50,replace=TRUE)))

我正在尝试生成A1和A1的分组点图,例如

: “ alt =”分组图”>

为了产生此功能,我使用以下代码将数据从宽格式转换为长格式,然后

library(reshape2)
df2 <- melt(df, id.vars=c("A"))

我正在尝试工作出去如何绘制等效图而不转换格式。我当前的代码是:

library(ggplot2)
ggplot(df,aes(x=as.factor(A)))+
    xlab("A") + 
    ylab("Score") +
    geom_dotplot (aes(y=A1), binaxis="y",binwidth=1, stackdir = "center",fill="#5ec962",colour="#5ec962") + 
    stat_summary(aes(y=A1),fun = median, fun.min = median, fun.max = median, geom = "crossbar", width = 0.2,colour="#5ec962") +
    geom_dotplot (aes(y=A2), binaxis="y",binwidth=1, stackdir = "center",fill="#3b528b",colour="#3b528b") + 
    stat_summary(aes(y=A2),fun = median, fun.min = median, fun.max = median, geom = "crossbar", width = 0.2,colour="#3b528b") +
    theme_classic()

这两个组彼此之间的顶部是:

“

是否有一种产生的方法像第一组一样的图形,而没有转换格式?我打算查看多次比较,理想情况下将不继续进行转换。

谢谢。

I am using an r data frame which looks something like:

df = data_frame(
A=c(sample(0:2,size=50,replace=TRUE)),
A1=c(sample(0:50,size=50,replace=TRUE)),
A2=c(sample(0:50,size=50,replace=TRUE)))

I am trying to produce a grouped dot plot of A1 and A1, grouped by A, e.g:

Grouped Plot

To produce this I've converted the data from wide to long format using the below code and then utlised position_dodge

library(reshape2)
df2 <- melt(df, id.vars=c("A"))

I'm trying to work out how to draw the equivalent graph without converting the format. My current code is:

library(ggplot2)
ggplot(df,aes(x=as.factor(A)))+
    xlab("A") + 
    ylab("Score") +
    geom_dotplot (aes(y=A1), binaxis="y",binwidth=1, stackdir = "center",fill="#5ec962",colour="#5ec962") + 
    stat_summary(aes(y=A1),fun = median, fun.min = median, fun.max = median, geom = "crossbar", width = 0.2,colour="#5ec962") +
    geom_dotplot (aes(y=A2), binaxis="y",binwidth=1, stackdir = "center",fill="#3b528b",colour="#3b528b") + 
    stat_summary(aes(y=A2),fun = median, fun.min = median, fun.max = median, geom = "crossbar", width = 0.2,colour="#3b528b") +
    theme_classic()

This has the two groups on top of each other like this:

Ungrouped plot

Is there a way to produce a graph like the first one with the groups side by side without converting the format? I'm planning to look at multiple comparison and would ideally not keep doing conversions.

Thank you.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文