箱线图,每个箱子显示多个均值,展示个体变异。

huangapple go评论172阅读模式
英文:

Boxplot with means multiple means per box showing individual variation

问题

I am trying to make a box plot that shows not only the overall mean of the data per box (red dot) but also the means of the 9 individuals included in the data set for each behaviour I am looking at. So rather than plotting all the raw data as shown in below I want it to plot the average for each individual at each behaviour

This is the graph currently and I want approx 9 means per box + the overall mean shown in red.

箱线图,每个箱子显示多个均值,展示个体变异。

  1. library(ggplot2)
  2. ggplot(Seen2, aes(x=Behaviour, y=Roll_Avg, fill=Behaviour)) +
  3. geom_boxplot(outlier.shape= NA) +
  4. geom_point(aes(fill=Behaviour), size=2, position=position_jitter(width=0.2, height=0.1)) +
  5. stat_summary(fun=mean, geom="point", shape=20, size=5, color="red", fill="red") +
  6. theme_classic() + my_scale +
  7. theme(axis.text.y=element_text(size=16, angle=0))+
  8. ylim(-30, 30)
英文:

I am trying to make a box plot that shows not only the overall mean of the data per box (red dot) but also the means of the 9 individuals included in the data set for each behaviour I am looking at. So rather than plotting all the raw data as shown in below I want it to plot the average for each individual at each behaviour

This is the graph currently and I want approx 9 means per box + the overall mean shown in red.

箱线图,每个箱子显示多个均值,展示个体变异。

  1. library(ggplot2)
  2. ggplot(Seen2, aes(x=Behaviour, y=Roll_Avg, fill=Behaviour)) +
  3. geom_boxplot(outlier.shape= NA) +
  4. geom_point(aes(fill=Behaviour), size=2, position=position_jitter(width=0.2, height=0.1)) +
  5. stat_summary(fun=mean, geom="point", shape=20, size=5, color="red", fill="red") +
  6. theme_classic() + my_scale +
  7. theme(axis.text.y=element_text(size=16, angle=0))+
  8. ylim(-30, 30)

EDIT

I am now needing to put these in order of behaviours with specific colours. The code worked fine before adding the jitter however now it wont order them. See full code with @Mark fix.

箱线图,每个箱子显示多个均值,展示个体变异。

  1. my_colors <- c("#CCFFFF", "#000000", "#7F7F7F", "#336699", "#008080", "#00CCFF", "#264AE2")
  2. names(my_colors) <- levels(factor(c((Seen2$Behaviour), levels(Seen2$Behaviour))))
  3. my_scale <- scale_fill_manual(name="Behaviour", values=my_colors,)
  4. behavssec$Behaviour <- factor(Seen2$Behaviour,
  5. levels=c("Burst", "High energy swimming",
  6. "Medium energy swimming",
  7. "Low energy swimming", "Travel",
  8. "Ascending", "Descending"))
  9. ggplot(Seen2, aes(x=Behaviour, y=Roll_Avg, fill=Behaviour)) +
  10. geom_boxplot(outlier.shape= NA) +
  11. geom_point(data=means, size=2, position=position_jitter(width=0.2, height=0.1)) +
  12. stat_summary(fun=mean, geom="point", shape=20, size=5, color="red", fill="red") +
  13. theme_classic() + my_scale +
  14. theme( axis.text.y= element_text( size=16, angle =0)) +
  15. ylim(-30, 30)

data

  1. Seen2 <- structure(list(SharkID = c(9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
  2. 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
  3. 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
  4. 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L,
  5. 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
  6. 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 6L, 6L,
  7. 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
  8. 7L, 7L, 7L, 7L, 7L, 7L), Behaviour = c("Low.energy.swimming",
  9. "Low.energy.swimming", "Low.energy.swimming", "Low.energy.swimming",
  10. "Low.energy.swimming", "High.energy.swimming", "High.energy.swimming",
  11. "High.energy.swimming", "High.energy.swimming", "High.energy.swimming",
  12. "High.energy.swimming", "Travel", "Travel", "Travel", "Travel",
  13. "Travel", "Burst", "Burst", "Burst", "Burst", "Burst", "Ascending",
  14. "Ascending", "Ascending", "Ascending", "Ascending", "Ascending",
  15. "Ascending", "Descending", "Descending", "Descending", "Descending",
  16. "Descending", "Descending", "High.energy.swimming", "High.energy.swimming",
  17. "High.energy.swimming", "High.energy.swimming", "High.energy.swimming",
  18. "High.energy.swimming", "High.energy.swimming", "Medium.energy.swimming",
  19. "Medium.energy.swimming", "Medium.energy.swimming", "Medium.energy.swimming",
  20. "Medium.energy.swimming", "High.energy.swimming", "High.energy.swimming",
  21. "High.energy.swimming", "High.energy.swimming", "Medium.energy.swimming",
  22. "Medium.energy.swimming", "Medium.energy.swimming", "Medium.energy.swimming",
  23. "Medium.energy.swimming", "Medium.energy.swimming", "Medium.energy.swimming",
  24. "Medium.energy.swimming", "Medium.energy.swimming", "High.energy.swimming",
  25. "High.energy.swimming", "High.energy.swimming", "High.energy.swimming",
  26. "High.energy.swimming", "Burst", "Burst", "Burst", "Burst", "Burst",
  27. "Burst", "High.energy.swimming", "High.energy.swimming", "High.energy.swimming",
  28. "High.energy.swimming", "High.energy.swimming", "High.energy.swimming",
  29. "High.energy.swimming", "Medium.energy.swimming", "Medium.energy.swimming",
  30. "Medium.energy.swimming", "Medium.energy.swimming", "Medium.energy.swimming",
  31. "Low.energy.swimming", "Low.energy.swimming", "Low.energy.swimming",
  32. "Low.energy.swimming", "Low.energy.swimming", "Low.energy.swimming",
  33. "Low.energy.swimming", "Low.energy.swimming", "Low.energy.swimming",
  34. "Low.energy.swimming", "Low.energy.swimming", "High.energy.swimming",
  35. "High.energy.swimming", "High.energy.swimming", "High.energy.swimming",
  36. "High.energy.swimming", "Burst", "Burst", "Burst", "Burst", "Burst",
  37. "Burst", "Burst", "Medium.energy.swimming", "Medium.energy.swimming",
  38. "Medium.energy.swimming", "Medium.energy.swimming", "Medium.energy.swimming",
  39. "Medium.energy.swimming"), Roll_Avg = c(3.97084, 3.90604, 3.90738,
  40. 3.80425, 3.4154, -0.993225, -0.940408, -0.55992, -0.791121, -1.83573,
  41. -3.41667, -14.0837, -14.9381, -16.4732, -16.6994, -15.5318, -18.2402,
  42. -19.4427, -22.8129, -27.009, -27.3907, 17.3778, 13.4861, 7.82564,
  43. 4.63057, 6.94956, 14.3372, 22.0873, -11.5397, -11.7741, -11.4795,
  44. -10.7844, -10.5135, -11.0162, -90, -90, 11.0157, 6.13595, 2.2689,
  45. -0.710414, -5.56132, -12.0987, -9.70231, -7.13388, -5.41693,
  46. -4.23157, 2.11092, 2.19057, 1.5597, 0.637742, 1.17135, 3.41601,
  47. 4.71664, 4.61525, -0.813111, -4.45238, -7.43746, -9.11626, -9.94338,
  48. -11.0361, -11.8852, -10.472, -5.12697, 2.61247, 9.80993, 17.307,
  49. 10.5466, -4.01104, -7.40708, -2.72602, -5.43834, -5.22419, -4.8472,
  50. -4.43957, -1.67914, 2.39693, 7.84736, -9.7158, -8.70349, -8.22463,
  51. -8.22878, -9.43265, -0.527293, -0.283262, -0.614311, -0.380123,
  52. -0.344986, 7.73204, 7.47037, 7.00224, 7.01661, 7.38737, 7.83069,
  53. -1.83138, -1.7847, -1.68084, -1.61196, -1.49905, -1.61391, -1.46356,
  54. -0.986477, -0.806394, -0.883015, -0.840026, -0.727501, -1.15641,
  55. -1.28692, -1.38961, -1.43838, -1.42089, -1.27225)), class = "data.frame", row.names = c(NA,
  56. -111L))

答案1

得分: 0

你可以做的是,为你想绘制的样本创建一个单独的数据框,并在geom_point()调用中引用它:

  1. library(tidyverse)
  2. set.seed(123)
  3. number_of_samples <- 9
  4. Seen2 <- Seen2 %>%
  5. mutate(Behaviour = factor(Behaviour, levels = c("Burst","High.energy.swimming", "Medium.energy.swimming", "Low.energy.swimming", "Travel", "Ascending", "Descending")))
  6. # 创建样本数据框
  7. means <- Seen2 %>%
  8. group_by(Behaviour, SharkID) %>%
  9. summarise(Roll_Avg = mean(Roll_Avg)) %>%
  10. group_by(Behaviour) %>%
  11. sample_n(ifelse(n() < number_of_samples, n(), number_of_samples)) # 一些行为组少于9行,因此我们取9和每个组的行数的最大值
  12. ggplot(Seen2, aes(x=Behaviour, y=Roll_Avg, fill=Behaviour))+
  13. geom_boxplot(outlier.shape= NA)+
  14. geom_point(data = means, size = 2, position=position_jitter(width=0.2, height = 0.1))+
  15. stat_summary(fun=mean, geom="point", shape=20, size=5, color="red", fill="red") +
  16. theme_classic () +
  17. theme( axis.text.y= element_text( size = 16, angle =0))+
  18. ylim(-30,30)

箱线图,每个箱子显示多个均值,展示个体变异。

英文:

What you can do is, create a separate dataframe for just the samples you want to plot, and then reference that in the geom_point() call:

  1. library(tidyverse)
  2. set.seed(123)
  3. number_of_samples &lt;- 9
  4. Seen2 &lt;- Seen2 %&gt;%
  5. mutate(Behaviour = factor(Behaviour, levels = c(&quot;Burst&quot;,&quot;High.energy.swimming&quot; , &quot;Medium.energy.swimming&quot;, &quot;Low.energy.swimming&quot;, &quot;Travel&quot;, &quot;Ascending&quot;, &quot;Descending&quot;)))
  6. # create sample dataframe
  7. means &lt;- Seen2 %&gt;%
  8. group_by(Behaviour, SharkID) %&gt;%
  9. summarise(Roll_Avg = mean(Roll_Avg)) %&gt;% # get the mean of each group
  10. group_by(Behaviour) %&gt;%
  11. sample_n(ifelse(n() &lt; number_of_samples, n(), number_of_samples)) # some behaviours have less than 9 rows, so we take the max of 9 and the number of rows for each group
  12. ggplot(Seen2, aes(x=Behaviour, y=Roll_Avg, fill=Behaviour))+
  13. geom_boxplot(outlier.shape= NA)+
  14. geom_point(data = means, size = 2, position=position_jitter(width=0.2, height = 0.1))+ # you don&#39;t need to repeat the fill=Behaviour or y=Roll_Avg, since that is in ggplot() already
  15. stat_summary(fun=mean, geom=&quot;point&quot;, shape=20, size=5, color=&quot;red&quot;, fill=&quot;red&quot;) +
  16. theme_classic () +
  17. theme( axis.text.y= element_text( size = 16, angle =0))+
  18. ylim(-30,30)

箱线图,每个箱子显示多个均值,展示个体变异。

答案2

得分: 0

以下是您要翻译的内容:

首先,将标签缩短一点。

  1. Seen3$Behaviour &lt;- gsub(&#39;.energy.swimming&#39;, &#39;*&#39;, Seen3$Behaviour)

接下来,进行一些预处理,使用 tapply 计算每种行为和鲨鱼的平均值(请注意,箱线图中的中心线实际上显示的是中位数!)。

  1. m &lt;- with(Seen3, tapply(Roll_Avg, list(Behaviour, SharkID), median, na.rm=TRUE))

接下来,使用 boxplot

  1. boxplot(Roll_Avg ~ Behaviour, Seen3, col=0, border=&#39;grey40&#39;, ylab=&#39;Moving average&#39;)

然后,在为鲨鱼定义六种颜色之后,

  1. clr &lt;- rainbow(ncol(m), v=.75)

添加具有“jitter”的均值(或中位数)。

  1. set.seed(666) ## 用于固定抖动
  2. points(jitter(as.vector(row(m))), m, col=clr, pch=20)

最后,添加一个漂亮的 legend

  1. legend(&#39;bottomleft&#39;, legend=colnames(m), col=clr, pch=20, ncol=2, title=&#39;Shark&#39;)
  2. text(ncol(m), min(m, na.rm=TRUE)*1.75, &#39;*swimming&#39;, bty=&#39;n&#39;, xpd=TRUE, adj=0, cex=.9)

注意: 如果您(真的?)想要从显示中删除异常值,请在 boxplot() 调用中包括 pch=NA

英文:

First, shorten the labels a bit.

  1. Seen3$Behaviour &lt;- gsub(&#39;.energy.swimming&#39;, &#39;*&#39;, Seen3$Behaviour)

Next, doing some preprocessing, using tapply to calculate the mean per behavior and shark (note, that the central line in a boxplot actually shows the median!).

  1. m &lt;- with(Seen3, tapply(Roll_Avg, list(Behaviour, SharkID), median, na.rm=TRUE))

Next, use boxplot,

  1. boxplot(Roll_Avg ~ Behaviour, Seen3, col=0, border=&#39;grey40&#39;, ylab=&#39;Moving average&#39;)

and after defining six colors for the sharks,

  1. clr &lt;- rainbow(ncol(m), v=.75)

add the jittered means (or medians respectively).

  1. set.seed(666) ## to fix the jitter
  2. points(jitter(as.vector(row(m))), m, col=clr, pch=20)

Finally, add a nice legend.

  1. legend(&#39;bottomleft&#39;, legend=colnames(m), col=clr, pch=20, ncol=2, title=&#39;Shark&#39;)
  2. text(ncol(m), min(m, na.rm=TRUE)*1.75, &#39;*swimming&#39;, bty=&#39;n&#39;, xpd=TRUE, adj=0, cex=.9)

箱线图,每个箱子显示多个均值,展示个体变异。

Note: If you (really?) want to remove the outliers from display, include a pch=NA in the boxplot() call.


Data:

  1. ## Seen2 with outliers removed according to the Tukey criterion
  2. Seen3 &lt;- structure(list(SharkID = c(9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
  3. 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
  4. 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
  5. 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L,
  6. 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
  7. 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 6L, 6L,
  8. 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
  9. 7L, 7L, 7L, 7L, 7L, 7L), Behaviour = c(&quot;Low.energy.swimming&quot;,
  10. &quot;Low.energy.swimming&quot;, &quot;Low.energy.swimming&quot;, &quot;Low.energy.swimming&quot;,
  11. &quot;Low.energy.swimming&quot;, &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;,
  12. &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;,
  13. &quot;High.energy.swimming&quot;, &quot;Travel&quot;, &quot;Travel&quot;, &quot;Travel&quot;, &quot;Travel&quot;,
  14. &quot;Travel&quot;, &quot;Burst&quot;, &quot;Burst&quot;, &quot;Burst&quot;, &quot;Burst&quot;, &quot;Burst&quot;, &quot;Ascending&quot;,
  15. &quot;Ascending&quot;, &quot;Ascending&quot;, &quot;Ascending&quot;, &quot;Ascending&quot;, &quot;Ascending&quot;,
  16. &quot;Ascending&quot;, &quot;Descending&quot;, &quot;Descending&quot;, &quot;Descending&quot;, &quot;Descending&quot;,
  17. &quot;Descending&quot;, &quot;Descending&quot;, &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;,
  18. &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;,
  19. &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;, &quot;Medium.energy.swimming&quot;,
  20. &quot;Medium.energy.swimming&quot;, &quot;Medium.energy.swimming&quot;, &quot;Medium.energy.swimming&quot;,
  21. &quot;Medium.energy.swimming&quot;, &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;,
  22. &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;, &quot;Medium.energy.swimming&quot;,
  23. &quot;Medium.energy.swimming&quot;, &quot;Medium.energy.swimming&quot;, &quot;Medium.energy.swimming&quot;,
  24. &quot;Medium.energy.swimming&quot;, &quot;Medium.energy.swimming&quot;, &quot;Medium.energy.swimming&quot;,
  25. &quot;Medium.energy.swimming&quot;, &quot;Medium.energy.swimming&quot;, &quot;High.energy.swimming&quot;,
  26. &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;,
  27. &quot;High.energy.swimming&quot;, &quot;Burst&quot;, &quot;Burst&quot;, &quot;Burst&quot;, &quot;Burst&quot;, &quot;Burst&quot;,
  28. &quot;Burst&quot;, &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;,
  29. &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;,
  30. &quot;High.energy.swimming&quot;, &quot;Medium.energy.swimming&quot;, &quot;Medium.energy.swimming&quot;,
  31. &quot;Medium.energy.swimming&quot;, &quot;Medium.energy.swimming&quot;, &quot;Medium.energy.swimming&quot;,
  32. &quot;Low.energy.swimming&quot;, &quot;Low.energy.swimming&quot;, &quot;Low.energy.swimming&quot;,
  33. &quot;Low.energy.swimming&quot;, &quot;Low.energy.swimming&quot;, &quot;Low.energy.swimming&quot;,
  34. &quot;Low.energy.swimming&quot;, &quot;Low.energy.swimming&quot;, &quot;Low.energy.swimming&quot;,
  35. &quot;Low.energy.swimming&quot;, &quot;Low.energy.swimming&quot;, &quot;High.energy.swimming&quot;,
  36. &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;, &quot;High.energy.swimming&quot;,
  37. &quot;High.energy.swimming&quot;, &quot;Burst&quot;, &quot;Burst&quot;, &quot;Burst&quot;, &quot;Burst&quot;, &quot;Burst&quot;,
  38. &quot;Burst&quot;, &quot;Burst&quot;, &quot;Medium.energy.swimming&quot;, &quot;Medium.energy.swimming&quot;,
  39. &quot;Medium.energy.swimming&quot;, &quot;Medium.energy.swimming&quot;, &quot;Medium.energy.swimming&quot;,
  40. &quot;Medium.energy.swimming&quot;), Roll_Avg = c(3.97084, 3.90604, 3.90738,
  41. 3.80425, 3.4154, -0.993225, -0.940408, -0.55992, -0.791121, -1.83573,
  42. -3.41667, -14.0837, -14.9381, -16.4732, -16.6994, -15.5318, -18.2402,
  43. -19.4427, -22.8129, NA, NA, 17.3778, 13.4861, 7.82564, 4.63057,
  44. 6.94956, 14.3372, NA, -11.5397, -11.7741, -11.4795, -10.7844,
  45. -10.5135, -11.0162, NA, NA, 11.0157, 6.13595, 2.2689, -0.710414,
  46. -5.56132, -12.0987, -9.70231, -7.13388, -5.41693, -4.23157, 2.11092,
  47. 2.19057, 1.5597, 0.637742, 1.17135, 3.41601, 4.71664, 4.61525,
  48. -0.813111, -4.45238, -7.43746, -9.11626, -9.94338, -11.0361,
  49. -11.8852, -10.472, -5.12697, 2.61247, 9.80993, 17.307, 10.5466,
  50. -4.01104, -7.40708, -2.72602, -5.43834, -5.22419, -4.8472, -4.43957,
  51. -1.67914, 2.39693, 7.84736, -9.7158, -8.70349, -8.22463, -8.22878,
  52. -9.43265, -0.527293, -0.283262, -0.614311, -0.380123, -0.344986,
  53. 7.73204, 7.47037, 7.00224, 7.01661, 7.38737, 7.83069, -1.83138,
  54. -1.7847, -1.68084, -1.61196, -1.49905, -1.61391, -1.46356, -0.986477,
  55. -0.806394, -0.883015, -0.840026, -0.727501, -1.15641, -1.28692,
  56. -1.38961, -1.43838, -1.42089, -1.27225)), row.names = c(NA, -111L
  57. ), class = &quot;data.frame&quot;)

huangapple
  • 本文由 发表于 2023年6月29日 08:07:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577379.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定