2014-10-03 33 views
-1

我使用以下代碼設置x軸的刻度: scale_x_discrete(breaks = c(「0」,「25」,「50」,「75 「,」100「),labels = c(」 - 5000「,」 - 2500「,」TSS「,」+ 2500「,」+ 5000「)) 但是,x軸左下角的刻度是缺少(「-5000」)。謝謝! enter image description hereggplot2 x軸上左下角的勾號缺失

UPDATE:

代碼如下:

library(ggplot2) 
setwd("E:\\") 
group<-rep("xxxx",200) 
window_index<-rep(1,200) 
methy<-rep(0.0001,200) 

dev.new(width=5, height=5) 


mydata <- c(2.69523,2.68113,2.701,2.71295,2.70934,2.7296,2.72514,2.68913,2.73231,2.71893,2.69485,2.68669,2.68183,2.66985,2.67812,2.7338,2.73466,2.8215,2.85903,2.90604,2.90006,2.89354,2.8911,2.97816,2.97491,2.87716,2.92016,2.86783,2.89455,2.83303,2.82433,2.79304,2.75092,2.76338,2.75429,2.70159,2.70007,2.73415,2.75342,2.81465,2.82124,2.80865,2.89053,2.90289,2.94132,2.97236,2.92759,2.92887,2.82978,2.91186,2.93556,2.92286,3.1038,3.11793,3.02738,3.09251,3.03509,2.83917,2.88059,2.89928,2.90047,2.91454,2.97956,2.87407,2.80076,2.83614,2.94722,2.86846,2.88684,2.8567,2.92221,3.01379,2.81978,2.8499,2.84203,2.83075,2.88386,2.8297,2.86195,2.79944,2.75654,2.79975,2.76778,2.80503,2.8923,2.84483,2.86865,2.93443,2.93266,2.91609,2.84964,2.80428,2.80971,2.8714,2.8648,2.95276,2.96739,2.92146,2.84643,2.8208) 
for(ii in 1:100) 
{ 
    group[ii]<-"aaa" 
    window_index[ii]<-ii 
    methy[ii]<-mydata[ii] 
} 


mydata <- c(2.34237,2.34268,2.35625,2.36485,2.3783,2.37418,2.35682,2.36853,2.3608,2.3571,2.35735,2.35252,2.35252,2.33272,2.34562,2.32457,2.34611,2.34031,2.33919,2.34066,2.33347,2.32424,2.33652,2.32585,2.29247,2.27918,2.25712,2.26037,2.24051,2.22711,2.21825,2.18171,2.16823,2.14479,2.10432,2.09132,2.05602,2.03091,2.03254,1.97737,1.95969,1.91187,1.85156,1.80697,1.73918,1.66702,1.60589,1.5536,1.51319,1.50754,1.49283,1.4818,1.51782,1.55003,1.6109,1.69999,1.78381,1.85273,1.94049,1.97149,1.99167,2.01945,2.05049,2.0917,2.12086,2.15047,2.17492,2.18193,2.18286,2.21,2.23119,2.24763,2.23394,2.24945,2.28581,2.29289,2.3308,2.32653,2.33957,2.33636,2.33611,2.34112,2.33125,2.34421,2.34622,2.36038,2.42041,2.44383,2.43497,2.40866,2.3726,2.34923,2.33929,2.33217,2.31134,2.32536,2.32067,2.31716,2.31672,2.28108) 
for(ii in 1:100) 
{ 
    group[ii+100]<-"bbb" 
    window_index[ii+100]<-ii 
    methy[ii+100]<-mydata[ii] 
} 

mydata<-data.frame(group=group,window_index=window_index,methy=methy) 

ggplot(mydata, aes(x = window_index, y =methy , colour = group)) + geom_line() + 
ggtitle("ATs") + xlab("")+ ylab("level")+ 
scale_x_discrete(breaks=c("0", "25", "50","75","100"), labels=c("-5000", "-2500", "TSS","+2500","+5000"))+ 
theme(legend.position="bottom")+ theme(legend.title=element_blank()) 
+0

請張貼[再現的示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。我無法重現:'ggplot(data.frame(y = rnorm(10),x = c(「0」,「25」,「50」,「75」,「100」),g = rep(c (x,y,color = g,group = g))+ geom_line()+ scale_x_discrete(breaks = c(「0」,「25」, 「50」,「75」,「100」),labels = c(「 - 5000」,「 - 2500」,「TSS」,「+ 2500」,「+ 5000」))' – MrFlick 2014-10-03 01:39:39

+1

您可能剛發佈了dput (mydata)和ggplot代碼。 – rnso 2014-10-03 02:51:27

回答

1

在我看來,X比例是連續的。以下修復左斷裂,

scale_x_continuous(breaks=c(0, 25, 50, 75, 100), 
    labels=c("-5000", "-2500", "TSS","+2500","+5000"), 
    expand=c(0,0), lim=c(0,100)) 
+0

謝謝。有用。 – Eman 2014-10-06 08:34:49

+0

我相信您遇到的問題是scale_discrete將數據解釋爲因子級別,因子級別從1到N,因此0被丟棄。 – baptiste 2014-10-06 10:52:22

+0

是的。將break = c(「0」,「25」,「50」,「75」,「100」)改爲break = c(「1」,「25」,「50」,「75」,「100」 ), 有效。 – Eman 2014-10-11 04:57:00