R语言 y轴的自由刻度不适用于facet_nested(ggh4x)

kqhtkvqz  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(138)

我试图使y轴的刻度自由。但它看起来不起作用。我希望适应有自己的刻度,适应后也有自己的刻度。我试图使y轴的刻度自由。但它看起来不起作用。我希望适应有自己的刻度,适应后也有自己的刻度。

  1. tgc <- structure(list(Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
  2. 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
  3. 2L), .Label = c("Visible", "Remembered"), class = "factor"),
  4. Condition = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L,
  5. 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L
  6. ), .Label = c("CEN", "IPS", "CTL"), class = "factor"), test = structure(c(1L,
  7. 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L,
  8. 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("Pre-test", "Post-test"
  9. ), class = "factor"), Session = structure(c(1L, 2L, 1L, 2L,
  10. 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
  11. 2L, 1L, 2L, 1L, 2L), .Label = c("Adaptation", "Post-adaptation"
  12. ), class = "factor"), N = c(12, 12, 12, 12, 12, 12, 12, 12,
  13. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  14. 12), EE = c(2.134379625, 0.333942625, 1.742841125, 0.317361916666667,
  15. 2.84197270833333, 0.307057416666667, 2.403112375, 0.281202,
  16. 3.49590529166667, 0.305657666666667, 2.85211466666667, 0.3131155,
  17. 1.44857545833333, 0.269328166666667, 1.740270875, 0.243361833333333,
  18. 2.10702266666667, 0.286209125, 2.145855125, 0.305474083333333,
  19. 1.60016616666667, 0.281528625, 1.94182179166667, 0.294655916666667
  20. ), sd = c(0.727246182828044, 0.0816168443914292, 0.549168068103643,
  21. 0.0894916121701392, 1.14554677132408, 0.0958562360654162,
  22. 1.06827971273128, 0.0953131237162305, 1.18204258551111, 0.0896670491921828,
  23. 1.32864473484909, 0.109865886496798, 0.605344957514288, 0.0815454655757737,
  24. 0.833908172662699, 0.0798994165789182, 1.11582277105041,
  25. 0.0976064300150272, 0.667812406644538, 0.142929179817685,
  26. 0.686043669971901, 0.109794818975944, 1.39509308576833, 0.161854932615856
  27. ), se = c(0.209937889711449, 0.0235607535398997, 0.158531165974993,
  28. 0.0258340031883217, 0.330690868396632, 0.0276713118479362,
  29. 0.308385789857611, 0.0275145288174349, 0.341226302469221,
  30. 0.0258846474942731, 0.383546697661249, 0.0317155495718416,
  31. 0.174748037086728, 0.0235401482506832, 0.240728553983119,
  32. 0.0230649748349663, 0.322110288616933, 0.0281765493219072,
  33. 0.192780836372198, 0.0412601002213964, 0.198043748767058,
  34. 0.0316950341456936, 0.402728684306467, 0.0467234944577166
  35. ), ci = c(0.462070179795855, 0.0518568689018959, 0.348924743722983,
  36. 0.0568602576432562, 0.727845693918804, 0.0609041467375754,
  37. 0.678752547059741, 0.0605590696140879, 0.751034027967696,
  38. 0.0569717250090983, 0.844180589754564, 0.069805453951774,
  39. 0.384617836383033, 0.0518115169661108, 0.529839974927164,
  40. 0.0507656673296478, 0.708959965158704, 0.0620161669201078,
  41. 0.424307760005262, 0.0908128682911871, 0.435891352085212,
  42. 0.0697602998032695, 0.886399857701764, 0.102837717929058)), row.names = c(NA,
  43. -24L), class = "data.frame")
  1. library(ggh4x)
  2. p <- ggplot(tgc, aes(x = Condition, y = EE), fill = test) +
  3. geom_errorbar(aes(ymin=EE-se, ymax=EE+se, group = test), position = position_dodge(0.5), width=.1) +
  4. geom_bar(aes(fill = test), stat = "identity", width = 0.5, color = "black", position='dodge') + ylim(0,4) + theme_bw() + theme(
  5. axis.text.x = element_text(size = 12,face="bold"),#, angle = 10, hjust = .5, vjust = .5),
  6. axis.text.y = element_text(size = 12, face = "bold"),
  7. axis.title.y = element_text(vjust= 1.8, size = 16),
  8. axis.title.x = element_text(vjust= -0.5, size = 16),
  9. axis.title = element_text(face = "bold")) + xlab("Workspace") + ylab("EE (cm)") + theme(legend.position="top") +
  10. scale_fill_manual(values = c("grey80", "grey20")) + facet_nested(. ~ Session + Group, scales = "free_y") + theme(aspect.ratio = 6/4)
  11. p + guides(fill=guide_legend(title="Test:")) + theme(legend.text=element_text(size=14),legend.title=element_text(size=14) ) +
  12. theme(strip.text = element_text(face="bold", size=12))
jgzswidk

jgzswidk1#

facet_nested()函数基于facet_grid(),其中您可以在网格中的行之间设置不同的y刻度,但不能在一行内设置。
让我们做一个情节的基础。

  1. library(ggplot2)
  2. #> Warning: package 'ggplot2' was built under R version 4.1.1
  3. library(ggh4x)
  4. tgc <- structure(list(Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
  5. 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
  6. 2L), .Label = c("Visible", "Remembered"), class = "factor"),
  7. Condition = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L,
  8. 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L
  9. ), .Label = c("CEN", "IPS", "CTL"), class = "factor"), test = structure(c(1L,
  10. 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L,
  11. 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("Pre-test", "Post-test"
  12. ), class = "factor"), Session = structure(c(1L, 2L, 1L, 2L,
  13. 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
  14. 2L, 1L, 2L, 1L, 2L), .Label = c("Adaptation", "Post-adaptation"
  15. ), class = "factor"), N = c(12, 12, 12, 12, 12, 12, 12, 12,
  16. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  17. 12), EE = c(2.134379625, 0.333942625, 1.742841125, 0.317361916666667,
  18. 2.84197270833333, 0.307057416666667, 2.403112375, 0.281202,
  19. 3.49590529166667, 0.305657666666667, 2.85211466666667, 0.3131155,
  20. 1.44857545833333, 0.269328166666667, 1.740270875, 0.243361833333333,
  21. 2.10702266666667, 0.286209125, 2.145855125, 0.305474083333333,
  22. 1.60016616666667, 0.281528625, 1.94182179166667, 0.294655916666667
  23. ), sd = c(0.727246182828044, 0.0816168443914292, 0.549168068103643,
  24. 0.0894916121701392, 1.14554677132408, 0.0958562360654162,
  25. 1.06827971273128, 0.0953131237162305, 1.18204258551111, 0.0896670491921828,
  26. 1.32864473484909, 0.109865886496798, 0.605344957514288, 0.0815454655757737,
  27. 0.833908172662699, 0.0798994165789182, 1.11582277105041,
  28. 0.0976064300150272, 0.667812406644538, 0.142929179817685,
  29. 0.686043669971901, 0.109794818975944, 1.39509308576833, 0.161854932615856
  30. ), se = c(0.209937889711449, 0.0235607535398997, 0.158531165974993,
  31. 0.0258340031883217, 0.330690868396632, 0.0276713118479362,
  32. 0.308385789857611, 0.0275145288174349, 0.341226302469221,
  33. 0.0258846474942731, 0.383546697661249, 0.0317155495718416,
  34. 0.174748037086728, 0.0235401482506832, 0.240728553983119,
  35. 0.0230649748349663, 0.322110288616933, 0.0281765493219072,
  36. 0.192780836372198, 0.0412601002213964, 0.198043748767058,
  37. 0.0316950341456936, 0.402728684306467, 0.0467234944577166
  38. ), ci = c(0.462070179795855, 0.0518568689018959, 0.348924743722983,
  39. 0.0568602576432562, 0.727845693918804, 0.0609041467375754,
  40. 0.678752547059741, 0.0605590696140879, 0.751034027967696,
  41. 0.0569717250090983, 0.844180589754564, 0.069805453951774,
  42. 0.384617836383033, 0.0518115169661108, 0.529839974927164,
  43. 0.0507656673296478, 0.708959965158704, 0.0620161669201078,
  44. 0.424307760005262, 0.0908128682911871, 0.435891352085212,
  45. 0.0697602998032695, 0.886399857701764, 0.102837717929058)), row.names = c(NA,
  46. -24L), class = "data.frame")
  47. p <- ggplot(tgc, aes(x = Condition, y = EE), fill = test) +
  48. geom_errorbar(aes(ymin=EE-se, ymax=EE+se, group = test),
  49. position = position_dodge(0.5), width=.1) +
  50. geom_bar(aes(fill = test),
  51. stat = "identity", width = 0.5, color = "black", position='dodge') +
  52. theme_bw() + theme(
  53. axis.text.x = element_text(size = 12,face="bold"),
  54. axis.text.y = element_text(size = 12, face = "bold"),
  55. axis.title.y = element_text(vjust= 1.8, size = 16),
  56. axis.title.x = element_text(vjust= -0.5, size = 16),
  57. axis.title = element_text(face = "bold")) +
  58. xlab("Workspace") + ylab("EE (cm)") +
  59. theme(legend.position="top") +
  60. scale_fill_manual(values = c("grey80", "grey20")) +
  61. theme(aspect.ratio = 6/4) +
  62. guides(fill=guide_legend(title="Test:")) +
  63. theme(legend.text=element_text(size=14),legend.title=element_text(size=14) ) +
  64. theme(strip.text = element_text(face="bold", size=12))

字符串
如果你想有一个非网格布局,使用facet_nested_wrap()可能更容易。

  1. p + facet_nested_wrap(~Session + Group, scales = "free_y", nrow = 1)


x1c 0d1x的数据
或者,如果您必须保留网格布局并希望y轴在一行中独立,则可以使用independent = "y"

  1. p + facet_nested(~Session + Group, scales = "free_y", independent = "y")

创建于2021-11-09由reprex package(v2.0.1)

展开查看全部
hk8txs48

hk8txs482#

这并不是对这个问题的直接回答,但另一种选择是单独创建面板。你可以通过先创建一个列表来半编程地完成这一点。我可以看到这种方式的最大缺点是,拼凑仍然不能像对图例那样合并/合并x/y轴标题,所以你需要单独创建每个情节,这反过来又会挫败这种方法的意义。

  1. library(tidyverse)
  2. library(ggh4x)
  3. library(patchwork)
  4. ls_p <-
  5. tgc %>%
  6. split(., .$Session) %>%
  7. map(function(x){
  8. ## I've tried to de-clutter your code.
  9. ggplot(x, aes(x = Condition, y = EE, fill = test)) +
  10. geom_errorbar(aes(ymin = EE - se, ymax = EE + se, group = test), position = position_dodge(0.5), width = .1) +
  11. geom_col(width = 0.5, color = "black", position = "dodge") +
  12. labs(x= "Workspace", y = "EE (cm)") +
  13. scale_fill_manual("Test:", values = c("grey80", "grey20")) +
  14. facet_nested(. ~ Session + Group, scales = "free_y") +
  15. ## I am using cowplot::theme_minimal because I think it's a well designed theme
  16. cowplot::theme_minimal_hgrid()
  17. })
  18. wrap_plots(ls_p) + plot_layout(guides = "collect") &
  19. theme(legend.position = "top")

字符串
x1c 0d1x的数据

展开查看全部

相关问题