2017-04-18 25 views
0

Asssume這是數據集:řGGPLOT2:不能改變在x軸的值的名稱,不會損壞圖表

n value 
100000 20 
200000 30 
300000 25 
400000 40 
500000 12 

下面是創建情節的代碼:

require(ggplot2) 
data <- read.table("test", sep = "\t", header = TRUE,) 
ggplot(data, aes(n, value)) + 
geom_point(aes(n,value)) + geom_line(aes(n,value)) + ylim(0,60)+ 
dev.off() 

enter image description here

我想使x軸上的值如下所示:100k,200k,300k,400k,500k。我曾嘗試下面的代碼:

require(ggplot2) 
data <- read.table("test", sep = "\t", header = TRUE,) 
ggplot(data, aes(n, value)) + 
geom_point(aes(n,value)) + 
geom_line(aes(n,value)) + 
ylim(0,60)+ 
scale_x_discrete(limit=c(100000,200000,300000,400000,500000), 
labels=c("100k","200k","300k","400k","500k")) 
dev.off() 

x軸的價值觀很好地改變,但最左邊的點勉強可以看出,同爲最右邊的點:

enter image description here

是有可能解決這個問題?我希望圖形看起來像在第一張圖片中,但只有軸軸上的名稱已更改。請注意,這是一個小數據集,所以我可能會手動更改數據集內的值,但我的實際數據集非常大,這使得此方法無法實現。

+0

如果你不希望有每次寫出來的標籤,你可以使用'從包unit_format' *鱗*。 'scale_x_continuous(labels = scales :: unit_format(unit =「K」,scale = 1/1000,sep =「」))'' – aosmith

回答

2

使用scale_x_continuous,不scale_x_discrete

+ scale_x_continuous(limits = c(100000, 500000), 
        labels = c("100k","200k","300k","400k","500k"))