簡體   English   中英

使用ggsave將ggplot2圖形導出為PDF時出現問題

[英]Problems exporting ggplot2 graph as PDF using ggsave

這是我使用dput()的數據框:

AggBar <- structure(list(Visual = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L), .Label = c("Left (40ms)", "Left (90ms)", 
    "SIM", "Right (40ms)", "Right (90ms)"), class = "factor"), Audio = structure(c(1L, 
    1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 
    3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 
    3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("Left Beep 1st", 
    "No Beep", "Right Beep 1st"), class = "factor"), Report = structure(c(1L, 
    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
    3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Left", 
    "SIM", "Right"), class = "factor"), Prob = c(0.83837774456841, 
    0.0582319772265122, 0.103390278205078, 0.376964076581909, 0.533271295345261, 
    0.0897646280728304, 0.491246357933239, 0.138301223292309, 0.370452418774452, 
    0.967653676751111, 0.00938599635932435, 0.0229603268895651, 0.877493727693669, 
    0.0720126632217432, 0.0504936090845876, 0.876575180584984, 0.00762664462699526, 
    0.115798174788021, 0.492520101540142, 0.29634748728839, 0.211132411171468, 
    0.0853528134021257, 0.803322136564915, 0.11132505003296, 0.155147359805197, 
    0.250345330474703, 0.5945073097201, 0.301905843152117, 0.140332387165982, 
    0.557761769681901, 0.0572000601130054, 0.503570857472542, 0.439229082414452, 
    0.0700895268446092, 0.0751875639266702, 0.854722909228721, 0.100018323936676, 
    0.0125390266853255, 0.887442649377999, 0.0431475160621075, 0.068269348744299, 
    0.888583135193594, 0.0235399740203181, 0.0042071142101905, 0.972252911769491
    ), SD = c(0.132514958276699, 0.0456130005972609, 0.118780003012801, 
    0.135944534822725, 0.143955013979635, 0.0570946432581132, 0.197212542689416, 
    0.107930186460654, 0.177082458822958, 0.0408127107496353, 0.0199945720490766, 
    0.0364346081328841, 0.133304906228258, 0.0819834012009533, 0.0660651710233039, 
    0.121660680076734, 0.01958675910736, 0.117439307692672, 0.205346740985586, 
    0.197451097807221, 0.190264024509737, 0.0770279870403184, 0.144412752633359, 
    0.0856446869844664, 0.201202913892, 0.161633110440843, 0.185655750581604, 
    0.138719102634961, 0.104516121191566, 0.166845454185104, 0.0580320672043022, 
    0.17918882055406, 0.155080139699953, 0.0923569889436162, 0.063020378352565, 
    0.104479039597072, 0.0856849197498157, 0.0208368690589841, 0.0967941898575448, 
    0.0783775922876842, 0.0882057647363221, 0.126874479704313, 0.0444900553037874, 
    0.011210924284355, 0.0485436288069594), SE = c(0.0276312788788636, 
    0.00951096809292287, 0.0247673427299119, 0.0283463950228221, 
    0.0300166953905415, 0.0119050560847632, 0.0411216577835893, 0.0225049995889141, 
    0.0369242451412551, 0.00851003846804108, 0.00416915647513517, 
    0.00759714096622745, 0.0277959944131186, 0.0170947208638228, 
    0.0137755404255046, 0.0253680054199854, 0.00408412159854824, 
    0.024487788430806, 0.0428177553751394, 0.0411713999641989, 0.0396727915867888, 
    0.0160614455837079, 0.030112140497714, 0.0178581517236676, 0.0419537076967664, 
    0.0337028333157947, 0.0387119000488496, 0.0289249324044521, 0.0217931177697776, 
    0.0347896821174935, 0.0121005224896259, 0.0373634519234081, 0.0323364444614249, 
    0.0192577634336539, 0.0131406572658402, 0.0217853857228293, 0.0178665408351603, 
    0.00434478754261805, 0.0201829837822752, 0.0163428577310708, 
    0.0183921733504504, 0.026455157794341, 0.00927681781303608, 0.00233763930819051, 
    0.0101220463168873), CI = c(0.0573037650986205, 0.0197245405776072, 
    0.0513643250511128, 0.0587868252099994, 0.0622508161579229, 0.0246895751860773, 
    0.0852810985849519, 0.0466725125406402, 0.0765761975511351, 0.0176487395856976, 
    0.00864630132966052, 0.0157555060429295, 0.0576453642089773, 
    0.0354522812028192, 0.0285687222842772, 0.052610023226951, 0.00846994978927434, 
    0.0507845649191804, 0.0887985897006044, 0.0853842575536569, 0.0822763340004107, 
    0.0333093994276579, 0.0624487571951513, 0.0370355399022581, 0.0870066644910416, 
    0.0698953983256852, 0.0802835669187016, 0.0599866383045376, 0.0451961600084025, 
    0.072149384784412, 0.025094947698802, 0.0774870566678828, 0.0670616812803235, 
    0.0399381569331217, 0.0272520551981834, 0.0451801247244766, 0.0370529378546465, 
    0.00901053787040049, 0.0418569464960043, 0.0338930125010548, 
    0.0381430329717212, 0.054864639256835, 0.0192389426183078, 0.0048479672037301, 
    0.0209918392486697)), .Names = c("Visual", "Audio", "Report", 
    "Prob", "SD", "SE", "CI"), row.names = c(NA, -45L), class = "data.frame")

這是我的生成圖形的代碼:

#####################################################
######### ADD THIS LINE TO CREATE THE HIGHLIGHT SUBSET
HighlightDataShort<-AggBar[AggBar$Report==gsub("\\s|\\(40ms)","", AggBar$Visual),]
HighlightDataLong <-AggBar[AggBar$Report==gsub("\\s|\\(90ms)", "", AggBar$Visual),]
HighlightDataShort<-AggBar[AggBar$Report==gsub("\\s|\\(40ms)","", AggBar$Visual),]
HighlightDataSIM <-AggBar[AggBar$Report==gsub("\\s|\\Circle Presentation", "", AggBar$Visual),]
#####################################################


prob.bar = ggplot(AggBar, aes(x = Report, y = Prob, fill = Report)) + theme_bw() + facet_grid(Audio~Visual)
Exp2BGrid <- prob.bar + geom_bar(position=position_dodge(.9), stat="identity", colour="black", width=0.8) + theme(legend.position = "none") + labs(x="Report", y="Probability of Report") + scale_fill_grey() +

  ######### ADD THIS LINE TO CREATE THE HIGHLIGHT SUBSET
  geom_bar(data=HighlightDataShort, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.80)+
  geom_bar(data=HighlightDataShort, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.60)+
  geom_bar(data=HighlightDataShort, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.40)+
  geom_bar(data=HighlightDataShort, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.20)+
  geom_bar(data=HighlightDataShort, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.0) +
  ######################################################

######### ADD THIS LINE TO CREATE THE HIGHLIGHT SUBSET
geom_bar(data=HighlightDataLong, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.80)+
  geom_bar(data=HighlightDataLong, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.60)+
  geom_bar(data=HighlightDataLong, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.40)+
  geom_bar(data=HighlightDataLong, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.20)+
  geom_bar(data=HighlightDataLong, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.0) +
  ######################################################
######### ADD THIS LINE TO CREATE THE HIGHLIGHT SUBSET
geom_bar(data=HighlightDataSIM, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.80)+
  geom_bar(data=HighlightDataSIM, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.60)+
  geom_bar(data=HighlightDataSIM, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.40)+
  geom_bar(data=HighlightDataSIM, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.20)+
  geom_bar(data=HighlightDataSIM, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.0) +
  ######################################################



labs(title = expression("Visual Condition")) +
  scale_fill_grey(start=.4) + # CODE TO LIGHTEN RELATIVE COLOUR SCHEME OF BARS
  # Move the y-axis label Source: http://stackoverflow.com/questions/8454243/move-axis-labels-ggplot
  theme(axis.title.y = element_text(vjust=2)) +
  theme(axis.title.x = element_text(vjust=-0.05)) +
  theme(text=element_text(size=30))+
  theme(axis.text.y =element_text(size=20))+ # THIS CHANGES SIZE OF VALUES ON Y-AXIS
  theme(axis.title.x=element_text(size=40))+
  theme(axis.title.y=element_text(size=40))+
  #theme(axis.text.x=element_text(size=30))+
  geom_errorbar(aes(ymin=Prob-CI, ymax=Prob+CI),
                width=.1, # Width of the error bars
                position=position_dodge(.9))+
  theme(plot.title = element_text(size = 40, vjust=1))+ #vjust respositions text
  scale_y_continuous(limits = c(0, 1), breaks = (seq(0,1,by = .25)))

Exp2BGrid

生成的圖形是應該的(請注意,這是jpeg,但是當我手動保存PDF時也應該是):

在此處輸入圖片說明

但是,當我保存使用:

ggsave(Exp2BGrid,file="Experiment_2A.pdf", width=22, height=13, path='/home/filelocation')

以下代碼被忽略:

theme(axis.title.y = element_text(vjust=2)) +
  theme(axis.title.x = element_text(vjust=-0.05))

通過ggsave保存時,輸出將忽略軸文本的重新定位,並且與值等重疊。

任何見解將是最歡迎的。

問題實際上歸結於冗余編碼。 ggsave似乎無法與axis.title代碼重復播放。 我這樣整理:

theme(axis.title.y = element_text(size=40, vjust=2)) +
theme(axis.title.x = element_text(size=40, vjust=-0.05))

擺脫大小和ggsave的單獨行再次工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM