Reputation: 9018
I want to plot the 4 columns of a matrix and have the x-axis say (1,2,3,4). I have provided a simple example you can run.
require("reshape")
require("ggplot2")
a<-rbind(.493,.537,.50,.462)
b<-rbind(.846,-.117,-.349,-.385)
c<-rbind(-.181,.0657,.135,-.719)
d<-rbind(-.09,.51,-.77,.34)
me<- cbind(a,b,c,d)
data<-c(me[,1], me[,2], me[,3], me[,4])
description<-rep( c("PC1","PC2","PC3","PC4"), each=NROW(me) )
d<- cbind(data, description)
ax <- rep( cbind(seq(from= 1, to = 4, by =1)), NROW(me) )
stacked <- data.frame(d , xaxis= ax )
stacked
ggplot(data=stacked, aes( x = xaxis, y=data, colour=description)) + geom_line()
The problem is that there should be 4 lines showing on the chart but it is blank.
Any ideas?
Thank you.
Upvotes: 0
Views: 124
Reputation: 58845
As I mentioned in my comment, you are converting your numeric values to character strings when you use cbind
to create the matrix you call d
. You are making it much harder than it is. Going back to your starting matrix me
(simpler way specify the starting matrix rather than creating the steps you had; got it using dump("me", "")
)
me <-
structure(c(0.493, 0.537, 0.5, 0.462, 0.846, -0.117, -0.349,
-0.385, -0.181, 0.0657, 0.135, -0.719, -0.09, 0.51, -0.77, 0.34
), .Dim = c(4L, 4L))
You are using "PC1", "PC2", to identify the columns, so include that in the matrix directly
colnames(me) <- c("PC1", "PC2", "PC3", "PC4")
Now me
looks like
> me
PC1 PC2 PC3 PC4
[1,] 0.493 0.846 -0.1810 -0.09
[2,] 0.537 -0.117 0.0657 0.51
[3,] 0.500 -0.349 0.1350 -0.77
[4,] 0.462 -0.385 -0.7190 0.34
The rows don't have names, but are just indexed counts which match exactly what you want for xaxis
later. Now just melt this:
library("reshape2")
stacked <- melt(me)
which looks like
> stacked
Var1 Var2 value
1 1 PC1 0.4930
2 2 PC1 0.5370
3 3 PC1 0.5000
4 4 PC1 0.4620
5 1 PC2 0.8460
6 2 PC2 -0.1170
7 3 PC2 -0.3490
8 4 PC2 -0.3850
9 1 PC3 -0.1810
10 2 PC3 0.0657
11 3 PC3 0.1350
12 4 PC3 -0.7190
13 1 PC4 -0.0900
14 2 PC4 0.5100
15 3 PC4 -0.7700
16 4 PC4 0.3400
and the columns are of the appropriate type
> str(stacked)
'data.frame': 16 obs. of 3 variables:
$ Var1 : int 1 2 3 4 1 2 3 4 1 2 ...
$ Var2 : Factor w/ 4 levels "PC1","PC2","PC3",..: 1 1 1 1 2 2 2 2 3 3 ...
$ value: num 0.493 0.537 0.5 0.462 0.846 -0.117 -0.349 -0.385 -0.181 0.0657 ...
The column names are different than what you had in your stacked
, but they are all there. The graphing call is then just
ggplot(stacked, aes(x=Var1, y=value, colour=Var2)) +
geom_line()
Upvotes: 0
Reputation: 3711
your data
variable is factor not numeric.
ggplot(data=stacked, aes( x = xaxis, y=as.numeric(as.character(data)), colour=description)) + geom_line()
You are making dataframe hardway. I cleaned a little bit.
a<-c(.493,.537,.50,.462)
b<-c(.846,-.117,-.349,-.385)
c<-c(-.181,.0657,.135,-.719)
d<-c(-.09,.51,-.77,.34)
data<-c(a,b,c,d)
description<-rep(c("PC1","PC2","PC3","PC4"), each=4)
ax <- rep(cbind(seq(from= 1, to = 4, by =1)), 4 )
stacked <- data.frame(data, description, xaxis= ax)
ggplot(data=stacked, aes( x = xaxis, y=data, colour=description))+geom_line()
Upvotes: 1