R:选择合适的图,以创建具有3个亚组的线图,每个点的样本尺寸以及亚组之间的显着性差异

发布于 2025-02-09 04:13:13 字数 1714 浏览 0 评论 0 原文

我正在尝试创建这样的线图(在PowerPoint上制作):

显示体重对年龄(体重〜年龄)的依赖性,对于3个亚组(因此每个组都有自己的线路)。我还希望该图表显示:

  • 每个点的样本量,含义为每个年龄段的每个子组的个体数量。
  • 每个年龄段的亚组之间的显着性差异。 (tukeyhsd结果)

更重要的是:我必须重复几个参数(例如长度〜年龄和宽度〜年龄),并且可能必须进行几次,所以,例如 Geom_text

>我真的很想避免手动插入解决 例如:

我尝试了此代码:

plot_morphologic <- ggplot(data = weight_table, 
       mapping = aes(x = as.numeric(age), 
                     y = weight, color=POPULATION))+
  geom_line(se=TRUE)

但是,这为3个人群创建了一行...

我也尝试了以下方式:

plot_morphologic <- ggline(data=weight_table, x = "age", y = "weight", add = "mean_sd",
       color = "POPULATION")+
  stat_compare_means(aes(group = POPULATION), method = "anova", label = "p.signif", 
                     label.y = c(40),na.rm=F)+
  stat_n_text(group="POPULATION")

但是无法将样本尺寸拆分为每个子组子组。

我的数据的一个示例:

weight_table1
# A tibble: 246 × 4
   ID         POPULATION age weight
   <chr>      <chr>  <chr>       <dbl>
 1 Shere Khan A      0           13.4 
 2 Shere Khan A      1           14.2 
 3 Shere Khan A      2           17.4 
 4 Serafina   B      0            5.19
 5 Serafina   B      1           15.3 
 6 Serafina   B      2           NA   
 7 Kaa        A      0            7.68
 8 Kaa        A      1            6.92
 9 Kaa        A      2           19.4 
10 Shenzi     C      0            6.96

tnx!

I'm trying to create a line plot like this one (made on Powerpoint):
enter image description here

showing the dependency of weight on age (weight~age), for 3 subgroups (so each group has its own line). I also want the chart to show:

  • sample size for each point, meaning, the number of individuals for each subgroup at each age.
  • significance differences between the subgroups at each age. (TukeyHSD results)

one more important thing: I'm gonna have to repeat those graphs for several parameters (like length~age, and width~age), and also might have to do them several times, so I would really like to avoid manual inserting solutions, like geom_text if possible..

I've tried several options but keep getting "stuck" at some point. for example:

I have tried this code:

plot_morphologic <- ggplot(data = weight_table, 
       mapping = aes(x = as.numeric(age), 
                     y = weight, color=POPULATION))+
  geom_line(se=TRUE)

but that creates one line for the 3 populations...

I've also tried this:

plot_morphologic <- ggline(data=weight_table, x = "age", y = "weight", add = "mean_sd",
       color = "POPULATION")+
  stat_compare_means(aes(group = POPULATION), method = "anova", label = "p.signif", 
                     label.y = c(40),na.rm=F)+
  stat_n_text(group="POPULATION")

but couldn't split the sample size to each subgroup and couldn't add the significance of the differences between the subgroups.

an example of my data:

weight_table1
# A tibble: 246 × 4
   ID         POPULATION age weight
   <chr>      <chr>  <chr>       <dbl>
 1 Shere Khan A      0           13.4 
 2 Shere Khan A      1           14.2 
 3 Shere Khan A      2           17.4 
 4 Serafina   B      0            5.19
 5 Serafina   B      1           15.3 
 6 Serafina   B      2           NA   
 7 Kaa        A      0            7.68
 8 Kaa        A      1            6.92
 9 Kaa        A      2           19.4 
10 Shenzi     C      0            6.96

tnx!!

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

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

发布评论

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

评论(1

甩你一脸翔 2025-02-16 04:13:13

您可以使用以下基本r函数来实现所有这些:

  1. 绘制轴和 plot 带有参数 xlim ylim 设置为您要绘制的最大范围(通常是使用 1.1*range(YourData $ yourrow)左右计算的最大范围)。
  2. 行绘制另外两行
  3. 点点 arrows arrows arrows arrows 答案。请注意,关于“特别直觉参数 code = 3 >”的有趣咆哮;-)
  4. text 一起添加文本。

我不明白为什么最后一步是重复用例的问题,因为您可以使用 sprintf(“ n =%i”,nrow(yourdata))创建标签文本。

You can achieve all this with the following base R functions:

  1. Plot the axis and the first line with plot with parameters xlim and ylim set to the maximum range you want to plot (usually automatically computed with 1.1*range(yourdata$yourrow) or so).
  2. Plot the two other lines with lines.
  3. Plot the points and the error bars with points and arrows, as explained in this answer. Note the amusing rant about the "particularly intuitive parameter code=3" ;-)
  4. Add the text with text.

I do not see why the last step is a problem for repeated use cases, because you can programatically create the label text with sprintf("n=%i", nrow(yourdata)).

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