2017-04-22 109 views
1

我的代碼工作:我不能讓scale_x_discrete(限制)重新排序我軸線

library(ggplot2) 
library(reshape2) 

hslresto <- read.delim("HSL_Resto.txt", header = TRUE, sep = "\t") 
hslrestomelted <- melt(hslresto, id.vars = c("HSL")) 

colnames(hslrestomelted) 
colnames(hslrestomelted) <- c("HSL", "Treatment", "Index") 

HSLplot <- ggplot(hslrestomelted, aes(HSL, Index, fill=Treatment)) + geom_bar(stat="identity", position=position_dodge(), show.legend = TRUE,scale_x_discrete(limits=c("Control", "10 mM C6", "100 mM C6", "1000 mM C6", "10 mM C10", "100 mM C10", "1000 mM C10"))) 


HSLplot 

我想我的X軸上顯示的順序,我在scale_x_discrete概述,但而是以字母數字順序(1000mM C10,1000mM C6,100mM C10,...對照)顯示。我敢肯定,這只是一個簡單的問題,無論我放置scale_x_discrete代碼,還是我的x軸不被視爲離散的,但由於我是R新手,我無法弄清楚。我也嘗試將它移動到最初的代碼之外,所以它的內容如下:

HSLplot<- ggplot(hslrestomelted, aes(HSL, Index, fill=Treatment)) + geom_bar(stat="identity", position=position_dodge(), show.legend = TRUE) 
HSLplot + scale_x_discrete(limits=c("Control", "10 mM C6", "100 mM C6", "1000 mM C6", "10 mM C10", "100 mM C10", "1000 mM C10")) 
HSLplot 

而且這也不起作用。我可以通過一些幫助來弄清楚這一點嗎?

回答

0

我認爲如果您將HSL轉換爲一個因子(指定關卡),它會以您想要的方式進行繪製。例如:

levs <- c("Control", "10 mM C6", "100 mM C6", "1000 mM C6", "10 mM C10", "100 mM C10", "1000 mM C10") 

hslrestomelted$HSL <- factor(hslrestomelted$HSL, levels = lev) 

然後嘗試繪製沒有scale_x_discrete()

+0

您可能還需要'ordered = TRUE'作爲'factor'的參數來始終保持序列 – epi99

+0

絕對精美!謝謝!!! –