3

我有一个六列的矩阵。我想分别绘制每一列。

首先我创建矩阵:

a<-replicate(6,rnorm(100))
colnames(a)<-c("one", "two", "three", "four", "five","six")

然后我melt()变量:

b<-melt(a, id.vars=1:6)
    colnames(b)<-c("c","variable","value")

现在我想绘制这些变量:

ggplot(b,aes(x = c, y = value, colour = variable, linetype = variable,size = variable)) + 
     geom_line() + 
     scale_x_continuous(breaks=seq(0,100,5)) +
     scale_colour_manual(values=c("blue1", "blue1","blue1","blue1","blue1","blue1")) + 
     scale_linetype_manual(values = c(0,0,1,0,0,0)) + 
     scale_size_manual(values = c(0.2,0.2,0.2,0.2,0.2,0.2)) + 
     xlab("\nT") + 
     ylab("O\n") +
     theme_bw()

但是,不是按照指定的“一”、“二”、“三”等顺序绘制变量,而是将顺序混合为:五四一六三二。如何按指定的列名顺序绘制变量?

4

2 回答 2

3

b$variable将显示该列的因子水平。这就是订单ggplot。您可以像这样更改级别 -b$variable <- factor(b$variable, levels =c("one","two","three","four","five","six"))

于 2013-11-05T14:25:24.303 回答
0

您需要更改因子水平的顺序才能手动定义沿 x 轴的顺序。

b$c <- 因子(b$c,级别 = c(“一”、“二”、“三”、“四”、“五”、“六”))

请注意,这也决定了图例中项目的顺序,如果您使用一个。但是,图例中项目的顺序也可以设置为scale_fill_discrete(limits = c("item1", "item2"))

于 2013-11-05T14:33:51.793 回答