R ggplot2:y轴缩放问题

时间:2017-09-17 18:34:03

标签: r ggplot2

我使用以下代码生成以下图表:

require(ggplot2)
pdf( "time.pdf" )
data <- read.table("testOut.txt", sep = "\t", header = TRUE,)

number_ticks <- function(n) {function(limits) pretty(limits, n)}
       ggplot(data, aes(log2(n), time/n, color=alg, shape=alg))+ 
        stat_summary(fun.y=median,geom="point",size=3)+
        stat_summary(fun.y=median, geom="line")+             
        scale_x_continuous(limits = c(7,21))+
        theme_bw(base_size=18)+
        scale_color_discrete(name="")+
        scale_shape_discrete(name="")+
        theme(legend.position="none")+
        theme(axis.title.x=element_blank(),axis.title.y=element_blank())

dev.off()

enter image description here

您可以在此处找到testOut.txt的数据:

alg n   time
c   8   0.00687304
c   8   0.0129063
c   8   0.00888481
c   8   0.0077091
c   8   0.00802988
c   8   0.00717868
c   8   0.00697401
c   8   0.00641019
c   8   0.00634793
c   8   0.00637025
c   16  0.0115272
c   16  0.00914332
c   16  0.00731745
c   16  0.00655211
c   16  0.00643339
c   16  0.0121381
c   16  0.00960201
c   16  0.0086754
c   16  0.00952297
c   16  0.00855257
c   32  0.00743192
c   32  0.0067925
c   32  0.00636815
c   32  0.00643828
c   32  0.00642462
c   32  0.00656378
c   32  0.00639697
c   32  0.00638644
c   32  0.0064479
c   32  0.00639331
c   64  0.0064263
c   64  0.00651457
c   64  0.00753789
c   64  0.00660107
c   64  0.00654401
c   64  0.00638864
c   64  0.00728003
c   64  0.00914778
c   64  0.0122254
c   64  0.00774323
c   128 0.00648506
c   128 0.00645773
c   128 0.0064865
c   128 0.00651176
c   128 0.00647432
c   128 0.00652222
c   128 0.00644952
c   128 0.00653393
c   128 0.0065028
c   128 0.00647199
c   256 0.00667486
c   256 0.00660924
c   256 0.00689106
c   256 0.00725997
c   256 0.00679926
c   256 0.00660101
c   256 0.00656251
c   256 0.00661859
c   256 0.00696825
c   256 0.00671256
c   512 0.0130569
c   512 0.00712115
c   512 0.00712308
c   512 0.00786105
c   512 0.00699915
c   512 0.00708569
c   512 0.00722727
c   512 0.00730832
c   512 0.00702228
c   512 0.00702427
c   1024    0.00769353
c   1024    0.00799852
c   1024    0.00775348
c   1024    0.0111849
c   1024    0.0148818
c   1024    0.0125332
c   1024    0.0101701
c   1024    0.00963756
c   1024    0.00797308
c   1024    0.00786196
c   2048    0.0160792
c   2048    0.0138837
c   2048    0.0163598
c   2048    0.0164419
c   2048    0.0125773
c   2048    0.0169556
c   2048    0.0168481
c   2048    0.0127922
c   2048    0.0106919
c   2048    0.0193877
c   4096    0.0193414
c   4096    0.0157488
c   4096    0.0227552
c   4096    0.0236461
c   4096    0.0269124
c   4096    0.0234896
c   4096    0.0226144
c   4096    0.0255129
c   4096    0.0249015
c   4096    0.0230232
c   8192    0.0404277
c   8192    0.0369109
c   8192    0.0398501
c   8192    0.0395346
c   8192    0.0380251
c   8192    0.0353476
c   8192    0.0359275
c   8192    0.0410885
c   8192    0.0388139
c   8192    0.0439261
c   16384   0.0771477
c   16384   0.0679897
c   16384   0.0697958
c   16384   0.0682643
c   16384   0.067555
c   16384   0.0684642
c   16384   0.0705867
c   16384   0.0767897
c   16384   0.0521458
c   16384   0.0694778
c   32768   0.14835
c   32768   0.152035
c   32768   0.145777
c   32768   0.134127
c   32768   0.150998
c   32768   0.146009
c   32768   0.152961
c   32768   0.152523
c   32768   0.139659
c   32768   0.147933
c   65536   0.296175
c   65536   0.292786
c   65536   0.302141
c   65536   0.291874
c   65536   0.300501
c   65536   0.300566
c   65536   0.296219
c   65536   0.302094
c   65536   0.299999
c   65536   0.293905
c   131072  0.6803
c   131072  0.677881
c   131072  0.661812
c   131072  0.658814
c   131072  0.657554
c   131072  0.68929
c   131072  0.669475
c   131072  0.663656
c   131072  0.683655
c   131072  0.683738
c   262144  1.51887
c   262144  1.47033
c   262144  1.47715
c   262144  1.55989
c   262144  1.53933
c   262144  1.50982
c   262144  1.51902
c   262144  1.57128
c   262144  1.53607
c   262144  1.59583
c   524288  3.54328
c   524288  3.65543
c   524288  3.59729
c   524288  3.57792
c   524288  3.5323
c   524288  3.63049
c   524288  3.64606
c   524288  3.59082
c   524288  3.5942
c   524288  3.58232
c   1048576 8.8313
c   1048576 8.69534
c   1048576 9.04035
c   1048576 8.70845
c   1048576 8.70882
c   1048576 8.89728
c   1048576 9.0441
c   1048576 8.93243
c   1048576 8.77835
c   1048576 8.77891
c   2097152 21.0474
c   2097152 21.0328
c   2097152 20.9023
c   2097152 21.0539
c   2097152 21.3529
c   2097152 21.3792
c   2097152 21.311
c   2097152 21.1805
c   2097152 21.716
c   2097152 21.2846

每个点是10个点的中位数。我也想绘制10点。但是当我将第二行更改为geom_point(size = 3)时,一切都会中断......

所以对于这段代码:

require(ggplot2)
pdf( "time.pdf" )
data <- read.table("testOut.txt", sep = "\t", header = TRUE,)

number_ticks <- function(n) {function(limits) pretty(limits, n)}
       ggplot(data, aes(log2(n), time/n, color=alg, shape=alg))+ 
        geom_point(size=3)+
        stat_summary(fun.y=median, geom="line")+             
        scale_x_continuous(limits = c(7,21))+
        theme_bw(base_size=18)+
        scale_color_discrete(name="")+
        scale_shape_discrete(name="")+
        theme(legend.position="none")+
        theme(axis.title.x=element_blank(),axis.title.y=element_blank())

dev.off()

我得到以下内容:

enter image description here

我不明白发生了什么......我只需要10点...... 我认为问题出在scale_x_continuous(limits = c(7,21))上。出于某种原因,前7个点确定缩放大小,即使我显然不希望它们出现在我的图形中!

enter image description here 使用stat_summary(fun.y=median,geom="point",size=3)时,这些点会被忽略,但使用geom_point(size=3)时情况并非如此......

1 个答案:

答案 0 :(得分:1)

按如下方式设置y轴的限制:

require(ggplot2)
data <- read.table("testOut.txt", sep = "\t", header = TRUE)
data$y <- data$time/data$n
data$x <- log2(data$n)
xmin <- 7
xmax <- 21
ymax <- max(data$y[data$x>=xmin & data$x<=xmax])

ggplot(data, aes(x=x, y=y, color=alg, shape=alg))+ 
geom_point(size=3) +
stat_summary(fun.y=median, geom="line") +             
scale_x_continuous(limits = c(xmin,xmax) )+
scale_y_continuous(limits = c(0,ymax)) +
theme_bw(base_size=18) +
scale_color_discrete(name="") + scale_shape_discrete(name="")+
theme(legend.position="none") +
theme(axis.title.x=element_blank(),axis.title.y=element_blank())

enter image description here