2017-04-22 111 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

绝对精美!谢谢!!! –