首页 > 解决方案 > 我的函数纵横比对我的 ggplot 没有任何影响

问题描述

我的主题(aspect.ratio)对我的图表没有任何影响,我想知道为什么。当我把它取下来时,它会过度拉伸图表。我想要控制,所以我调整我的图表。目前我的宽度很宽,但无法操纵图形的高度

tgc <- structure(list(Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L), .Label = c("Visible", "Remembered"), class = "factor"), 
    Condition = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
    3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L
    ), .Label = c("CEN", "IPS", "CTL"), class = "factor"), test = structure(c(1L, 
    1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 
    1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("Pre-test", "Post-test"
    ), class = "factor"), Session = structure(c(1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L, 1L, 2L, 1L, 2L), .Label = c("Adaptation", "Post-adaptation"
    ), class = "factor"), N = c(12, 12, 12, 12, 12, 12, 12, 12, 
    12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
    12), Abs_IDE = c(23.7992344166667, 13.0386095591667, 15.7028633063333, 
    19.84749675725, 18.1572712615, 11.68093831675, 16.1626619356667, 
    22.9018392689167, 25.980293756, 13.45901734025, 24.4302041906667, 
    12.0791439846667, 22.4191223275, 8.96559583341667, 16.7862310038333, 
    18.9797044311667, 20.1929425604167, 11.7699855065833, 13.56023988325, 
    19.73171153625, 33.5879796116667, 14.6798949093333, 26.1433479941667, 
    8.74572327816667), sd = c(8.04062173855832, 9.0782958352921, 
    6.90557155294788, 10.7770226212508, 9.90142743265505, 6.13623590788893, 
    9.72344405555326, 11.4928324154261, 14.7569847333998, 8.68503337334045, 
    14.1914679614007, 8.09149043273921, 8.16863922288962, 7.04528824438605, 
    5.65528757276849, 10.2612900552688, 8.59034167901021, 9.73347852641845, 
    6.85089019152845, 10.7480537432634, 14.6034318371352, 7.50792731288404, 
    10.5564213216697, 4.75186261300342), se = c(2.3211275626043, 
    2.62067827214448, 1.99346679750134, 3.11105845572093, 2.85829589680247, 
    1.77137872661536, 2.80691652146197, 3.31769494439875, 4.25997455412779, 
    2.5071531780095, 4.09672392385531, 2.33581208974363, 2.35808302712413, 
    2.0337995322074, 1.63254090124132, 2.96217928782113, 2.4798180404037, 
    2.80981322368957, 1.97768164813376, 3.10269586096884, 4.21564765113117, 
    2.16735192757487, 3.04737634587255, 1.37174457938482), ci = c(5.10876731997174, 
    5.76807398636459, 4.38759083843585, 6.84739349321937, 6.29106685201305, 
    3.89877829018243, 6.17798160935384, 7.3021973383327, 9.37614077600327, 
    5.51820693887549, 9.01682856139793, 5.14108774628837, 5.19010574896418, 
    4.4763625889898, 3.59319829687477, 6.51971265402513, 5.45804270665608, 
    6.18435720796999, 4.35284795892798, 6.82898754627016, 9.27857792031489, 
    4.7703094292883, 6.70723011447976, 3.01918946266214)), row.names = c(NA, 
-24L), class = "data.frame")
 library(ggh4x)

 p <- ggplot(tgc, aes(x = Condition, y = Abs_IDE), fill = test) +
   geom_errorbar(aes(ymin=Abs_IDE-se, ymax=Abs_IDE+se, group = test), position = position_dodge(0.5), width=.1) +
   geom_bar(aes(fill = test), stat = "identity", width = 0.5, color = "black", position='dodge') + ylim(0,38.5) + theme_bw() + theme(
     axis.text.x = element_text(size = 12,face="bold"),#, angle = 10, hjust = .5, vjust = .5),
     axis.text.y = element_text(size = 12, face = "bold"),
     axis.title.y = element_text(vjust= 1.8, size = 20),
     axis.title.x = element_text(vjust= -0.5, size = 20),
     axis.title = element_text(face = "bold")) + xlab("space") + ylab("Plot title") + theme(legend.position="top") +
   scale_fill_manual(values = c("grey80", "grey20")) +
   facet_nested(. ~ Session + Group )
 
 
 p + guides(fill=guide_legend(title="Test:")) + theme(legend.text=element_text(size=16),legend.title=element_text(size=16) ) +
   theme(strip.text = element_text(face="bold", size=12))  + theme(aspect.ratio = 1)

标签: rggplot2bar-chartaspect-ratio

解决方案


我想我修复了这个错误,至少以下内容现在应该适用于当前的 github 分支:

library(ggplot2)
library(ggh4x) # devtools::install_github("teunbrand/ggh4x")

# tgc <- structure(...) # omitted for brevity

p <- ggplot(tgc, aes(x = Condition, y = Abs_IDE), fill = test) +
  geom_errorbar(aes(ymin=Abs_IDE-se, ymax=Abs_IDE+se, group = test), position = position_dodge(0.5), width=.1) +
  geom_bar(aes(fill = test), stat = "identity", width = 0.5, color = "black", position='dodge') + ylim(0,38.5) + theme_bw() + theme(
    axis.text.x = element_text(size = 12,face="bold"),#, angle = 10, hjust = .5, vjust = .5),
    axis.text.y = element_text(size = 12, face = "bold"),
    axis.title.y = element_text(vjust= 1.8, size = 20),
    axis.title.x = element_text(vjust= -0.5, size = 20),
    axis.title = element_text(face = "bold")) + xlab("space") + ylab("Plot title") + theme(legend.position="top") +
  scale_fill_manual(values = c("grey80", "grey20")) +
  facet_nested(. ~ Session + Group )

p + guides(fill=guide_legend(title="Test:")) + theme(legend.text=element_text(size=16),legend.title=element_text(size=16) ) +
  theme(strip.text = element_text(face="bold", size=12))  + theme(aspect.ratio = 2)

reprex 包于 2021-10-28 创建(v2.0.1)


推荐阅读