如何使用ggplot2将x轴标签置于y = 0轴上?

时间:2015-11-03 10:28:16

标签: r ggplot2

我有以下R代码:

library("ggplot2")

x=read.table('some_file')

df <- data.frame(V1 = x$V1, V2 = x$V2)

jpeg('file.jpg',width=1600,height=800)
ggplot(df, aes(x=as.numeric(row.names(df)), y=V1)) +
  geom_bar(stat="identity", fill="grey", width=0.7) +
  geom_errorbar(aes(ymin = V1 - V2, ymax = V1 + V2), width=0.5) +
  scale_x_continuous(breaks=c(1,seq(1,length(x$V1),1)), expand=c(0,0)) +
  theme(axis.text.x=element_text(size = 9, angle = 90, vjust = 0.5, hjust=8.5)) + xlab("position") + ylab("Y axis") + labs(title = "Title")
dev.off

我需要x轴标签,即数字1,2,3 ......位于y = 0轴或正下方。我尝试过玩耍,但无论我用它做什么价值,它都行不通。例如,在hjust = 8.5的情况下,从100到167的数字是我需要它们的数字,但之前的数字是在y = 0轴之上的数字。

有人可以帮忙吗?以下是有问题的两个列向量:

-0.11285825 0.0134043866
-0.00427275 0.0012140211
-0.00825875 0.0025256419
-0.08434275 0.0153227983
0.111627    0.0149667631
-0.00591775 0.0028993564
-0.00710475 0.0014442042
-0.00660175 0.0005508105
0.1259935   0.0176350505
-0.02468425 0.0049306177
-0.0046885  0.0005749567
-0.002225   0.0009090044
-0.01276025 0.0040398386
-0.11821675 0.0699125335
-0.0035805  0.0010421268
-0.00276825 0.0011773666
-0.014487   0.0004579208
-0.06269425 0.0181558159
-0.0028185  0.0004570594
0.00163075  0.0004970686
-0.003703   0.0005301641
-0.0059895  0.00101644
-0.00164725 0.0005756263
-0.001748   0.0008530894
-0.00265675 0.0001855982
0.0617135   0.0148209217
-0.20685225 0.0116178605
0.001209    0.0015875276
-0.000297   0.0006022711
0.00447675  0.0012625121
-0.00088225 0.0040488959
-0.1444145  0.0142768668
-0.01167825 0.0012083802
0.01813575  0.0031464967
-0.007798   0.0106417775
-0.18151    0.0073565153
0.36773125  0.0181246164
0.562059    0.1015533768
-0.00886525 0.0131807785
0.174133    0.0431058507
0.57896425  0.2444875207
-0.8418545  0.138881625
0.02951775  0.0654937622
-0.12007175 0.034225026
-1.6405275  0.5553336954
-1.88679875 0.7850254112
-0.231041   0.0164808312
-0.1266595  0.0617940077
-0.19167575 0.0507706792
-1.055467   0.4429050182
-0.116364   0.0357189981
-0.19901725 0.0509971349
-1.7670315  0.1542821762
-0.10039375 0.0352431413
-0.11883575 0.0114192525
-0.3665885  0.1246847809
-1.9132725  0.5623975368
-0.31451275 0.1145112995
-0.11942425 0.0375176069
-0.04436425 0.0204052159
-0.0098315  0.0023110857
-0.007022   0.0030733554
-0.1755365  0.1161738267
-0.081695   0.0216418659
-0.06446825 0.006684648
-0.5098405  0.5403101751
-0.05826375 0.1454962774
-0.28179575 0.0358286362
-0.201353   0.0584529251
-0.7666615  0.143889849
-3.36481375 0.2914767538
-0.358104   0.0669525527
-0.17513975 0.0110406376
-3.76037275 0.7028178063
-2.91671325 0.0631300928
-0.382157   0.0164434459
0.28983275  0.1447389319
-0.1522485  0.6711400536
-0.21333325 0.0195889096
-0.135311   0.019034877
-2.54135775 0.1089711993
-1.02318925 0.096562207
-1.52091    0.1405089663
-9.649117   0.2311164764
-0.36733    0.0339848999
-0.89744525 0.0296846006
-0.69533325 0.0863715014
-0.30171975 0.0261684616
-0.0915285  0.0101355205
0.11442675  0.0466793911
-1.05659675 0.1963164192
-0.01967525 0.0056679875
0.00163325  0.0047527004
-0.008276   0.035198076
-0.070888   0.0277879036
0.0003515   0.0048609681
-0.00769575 0.0051084448
-0.0628 0.0277348014
-0.00517225 0.0050401386
-0.002678   0.0015185852
0.000215    0.0106790497
0.0303  0.0148194041
0.0157995   0.0098726426
-0.01274725 0.0026780256
-0.00394375 0.0006370312
0.00103025  0.0003137287
-0.00834475 0.0004048057
-0.010351   0.0012089266
-0.00144675 0.0003594665
-0.019411   0.0143840738
-0.01995875 0.0014986806
-0.00944875 0.0008936362
-0.00012    0.0001115437
-0.017019   0.0007678965
-0.0146025  0.0058825609
-0.00330075 0.000519175
0.0034255   0.001374384
-0.03580625 0.0039048393
-0.0008405  0.0010331095
0.006152    0.000903144
0.08482425  0.0169920168
0.013677    0.0030233132
-0.00002725 0.0015909331
0.15528725  0.0129757832
0.0062995   0.000357788
-0.0886525  0.0078460151
0.01510875  0.0010974966
0.00863325  0.0004734867
0.32934625  0.0152748957
0.067786    0.0029502796
0.00300625  0.0004219979
0.04193375  0.0018489418
-0.0000245  0.009160398
0.3010235   0.0192036334
-0.01484775 0.0286606604
-0.183226   0.050428693
-0.026292   0.005153877
0.53286075  0.050951844
-1.5150745  0.2209543557
0.2785605   0.0408203229
0.4550995   0.027406819
-0.3947985  0.4327885451
0.23381825  0.2238431016
-0.22846625 0.0188915484
-0.31804825 0.2029861787
-1.7850215  0.5281493487
-1.67640075 0.2481773479
-0.661103   0.0595275708
-4.67910175 1.0021441745
-0.22585775 0.2284251402
0.4079195   0.2637898013
-0.82208    0.1246448589
-3.8169575  0.2426232257
-5.66482025 0.5167259264
-0.65876175 0.1498171905
-3.275229   0.4303526931
-0.80324675 0.11413495
-2.2098325  0.1510774807
-0.8855575  0.6300580366
-4.4606575  0.2082186001
0.95024225  0.1393782289
-0.26379    0.0212151113
-0.436935   0.1433744083
-2.34355175 0.0412962458
0.60204025  0.0836441211
-0.16453425 0.0148762548
0.4368215   0.1924153651

1 个答案:

答案 0 :(得分:3)

手动放置将为您提供完全控制。此外,x已经是data.frame read.table()生成data.frames。

library(ggplot2)

df <- read.table('somefile')

jpeg('file.jpg',width=1600,height=800)
gg <- ggplot(df, aes(x=as.numeric(row.names(df)), y=V1))
gg <- gg + geom_bar(stat="identity", fill="grey", width=0.7)
gg <- gg + geom_errorbar(aes(ymin = V1 - V2, ymax = V1 + V2), width=0.5)
gg <- gg + geom_text(data=data.frame(x=1:length(df$V1)),
                     aes(x=x, y=0, label=x), size=2.5, angle=90, hjust=0)
gg <- gg + scale_x_continuous(expand=c(0,0))
gg <- gg + labs(x="position", y="Y axis", title="Title")
gg <- gg + theme(axis.ticks.x=element_blank()) 
gg <- gg + theme(axis.text.x=element_blank()) 
gg
dev.off()
system("open file.jpg")