我无法获得scale_x_discrete(限制)来重新排序我的轴

时间:2017-04-22 20:00:13

标签: r ggplot2

我正在使用代码:

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中概述的顺序显示,但它以字母数字顺序显示(1000 mM C10,1000 mM C6,100 mM C10,...... Control)。我确定这只是一个简单的问题,我放置了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

这也没有用。我可以帮忙解决这个问题吗?

1 个答案:

答案 0 :(得分: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()的情况下进行绘图。