ggplot2:从2个数据集复制图失败(ggplot2: fail replicating plot from 2 datasets)

编程入门 行业动态 更新时间:2024-10-18 10:19:15
ggplot2:从2个数据集复制图失败(ggplot2: fail replicating plot from 2 datasets)

我试图复制这个例子中的最后一个情节: https : //www.r-bloggers.com/plotting-individual-observations-and-group-means-with-ggplot2/

当我使用示例中的相同代码和数据时,我成功了。 但是,如果我自己尝试它就行不通。

我的原始数据是长格式:

> head(summpas) id session paradigm N mean sd se min firstq median thirdq max 1 1 s1 baseline 20 831.00 692.7155 154.8959 95 326.50 585.5 1327.50 2433 2 1 s1 post1 20 1344.65 1261.5589 282.0931 107 315.25 1008.5 2105.00 4621 3 1 s1 post2 20 1058.05 856.6661 191.5564 105 144.50 1064.0 1915.25 2427 4 1 s1 post3 20 1318.00 1016.1804 227.2248 95 381.75 1289.5 1741.50 3688 6 1 s2 baseline 20 1058.20 1118.8923 250.1919 10 131.00 314.5 1984.25 3042 7 1 s2 post1 20 1909.65 1478.1206 330.5178 59 760.50 1465.0 2808.00 4602

总结如示例中所述并不适用于我:

> meansummpas <- summpas %>% group_by(session, paradigm) %>% summarise(mean = mean(mean)) > meansummpas mean 1 949.5366

所以我使用:

library(plyr) meansummpas <- ddply(summpas, c("session", "paradigm"), summarise, mean=mean(mean))

现在我尝试一下情节:

library(ggplot2) ggplot(summpas, aes(x=paradigm, y=mean, group=id, colour=session)) + geom_line(aes(group=session), alpha=.3) + geom_line(data=meansummpas, alpha=.8, size=3)

但我得到错误:

Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous. Error: Aesthetics must be either length 1 or the same as the data (8): x, y, group, colour

我注意到的是,示例中的数据和我的数据并不是完全相同的类(这也是未编号的数据):

class(gd) [1] "grouped_df" "tbl_df" "tbl" "data.frame" class(meansummpas) [1] "data.frame"

为什么我会得到这个错误? 我究竟做错了什么? :) 非常感谢!!

I'm trying to replicate the last plot from this example: https://www.r-bloggers.com/plotting-individual-observations-and-group-means-with-ggplot2/

I succeed when I use the same code and data from the example. However, if I'm trying it on my own it won't work.

My original data is a longformat:

> head(summpas) id session paradigm N mean sd se min firstq median thirdq max 1 1 s1 baseline 20 831.00 692.7155 154.8959 95 326.50 585.5 1327.50 2433 2 1 s1 post1 20 1344.65 1261.5589 282.0931 107 315.25 1008.5 2105.00 4621 3 1 s1 post2 20 1058.05 856.6661 191.5564 105 144.50 1064.0 1915.25 2427 4 1 s1 post3 20 1318.00 1016.1804 227.2248 95 381.75 1289.5 1741.50 3688 6 1 s2 baseline 20 1058.20 1118.8923 250.1919 10 131.00 314.5 1984.25 3042 7 1 s2 post1 20 1909.65 1478.1206 330.5178 59 760.50 1465.0 2808.00 4602

Summarizing like stated in the example doesn't work for me:

> meansummpas <- summpas %>% group_by(session, paradigm) %>% summarise(mean = mean(mean)) > meansummpas mean 1 949.5366

So therefore I use:

library(plyr) meansummpas <- ddply(summpas, c("session", "paradigm"), summarise, mean=mean(mean))

Now I try the plot:

library(ggplot2) ggplot(summpas, aes(x=paradigm, y=mean, group=id, colour=session)) + geom_line(aes(group=session), alpha=.3) + geom_line(data=meansummpas, alpha=.8, size=3)

But I get the error:

Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous. Error: Aesthetics must be either length 1 or the same as the data (8): x, y, group, colour

What I have noticed is that the data from the example and my data are not exactly the same class (this also accounts for the unsummarized data):

class(gd) [1] "grouped_df" "tbl_df" "tbl" "data.frame" class(meansummpas) [1] "data.frame"

Why do I get this error? What am I doing wrong? :) Many thanks!!

最满意答案

目前还不清楚你在绘制什么,因为你似乎已经总结了每个范例/会话。

似乎出现了“每组一次观察”的问题,因为你的x变量是分类的; 这显然意味着分组。

但我确实设法获得了平均值的所有方法,但我不得不添加第8行和第9行。

summpas <- read.table(text='id session paradigm N mean sd se min firstq median thirdq max 1 1 s1 baseline 20 831.00 692.7155 154.8959 95 326.50 585.5 1327.50 2433 2 1 s1 post1 20 1344.65 1261.5589 282.0931 107 315.25 1008.5 2105.00 4621 3 1 s1 post2 20 1058.05 856.6661 191.5564 105 144.50 1064.0 1915.25 2427 4 1 s1 post3 20 1318.00 1016.1804 227.2248 95 381.75 1289.5 1741.50 3688 6 1 s2 baseline 20 1058.20 1118.8923 250.1919 10 131.00 314.5 1984.25 3042 7 1 s2 post1 20 1909.65 1478.1206 330.5178 59 760.50 1465.0 2808.00 4602 8 1 s2 post2 20 1060.20 1118.8923 250.1919 10 131.00 314.5 1984.25 3042 9 1 s2 post3 20 1980.20 1118.8923 250.1919 10 131.00 314.5 1984.25 3042 ', header=TRUE, as.is=TRUE) ggplot(summpas, aes(x=paradigm, y=mean)) + geom_path(aes(colour=session, group=session)) + stat_summary(fun.y=mean, geom='line', aes(group=NA))

It's unclear what you are plotting, as you already seem to have summarised each paradigm/session.

It appears the issue with 'one observation per group' appears because your x-variable is categorial; this apparently implies a grouping.

But I did manage to get a plot that averages across all means, but I had to add row 8 and 9.

summpas <- read.table(text='id session paradigm N mean sd se min firstq median thirdq max 1 1 s1 baseline 20 831.00 692.7155 154.8959 95 326.50 585.5 1327.50 2433 2 1 s1 post1 20 1344.65 1261.5589 282.0931 107 315.25 1008.5 2105.00 4621 3 1 s1 post2 20 1058.05 856.6661 191.5564 105 144.50 1064.0 1915.25 2427 4 1 s1 post3 20 1318.00 1016.1804 227.2248 95 381.75 1289.5 1741.50 3688 6 1 s2 baseline 20 1058.20 1118.8923 250.1919 10 131.00 314.5 1984.25 3042 7 1 s2 post1 20 1909.65 1478.1206 330.5178 59 760.50 1465.0 2808.00 4602 8 1 s2 post2 20 1060.20 1118.8923 250.1919 10 131.00 314.5 1984.25 3042 9 1 s2 post3 20 1980.20 1118.8923 250.1919 10 131.00 314.5 1984.25 3042 ', header=TRUE, as.is=TRUE) ggplot(summpas, aes(x=paradigm, y=mean)) + geom_path(aes(colour=session, group=session)) + stat_summary(fun.y=mean, geom='line', aes(group=NA))

更多推荐

本文发布于:2023-07-22 09:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219090.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   fail   datasets   plot   replicating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!