Patthebug
Patthebug

Reputation: 4797

Small multiples line plot

I am trying to create a small multiples line plot from a data frame which looks something like this:

Time    A   B   C   D
2013-01-01  69.23580    8.708641    NaN 3.452663
2013-02-01  69.46016    8.353662    NaN 4.601604
2013-03-01  69.62315    9.156498    NaN 3.953704
2013-04-01  71.04144    7.817074    NaN 3.762032
2013-05-01  74.29557    7.971487    NaN 4.475369

I would ideally want 4 plots on a single graph showing line plots for each of the variables in my data frame. I am using the following code (after melting the data frame) but it gives me some very weird results.

q <- ggplot(data=temp) + 
  geom_line(aes(x=Time, y=value, color=variable), size = 1) + 
  facet_grid(variable~value) +
  xlab("Time")

Any help in this regard would be much appreciated.

Upvotes: 0

Views: 870

Answers (1)

B.Mr.W.
B.Mr.W.

Reputation: 19648

I don't know if you would think about using the facet_wrap function.

And the scales argument will change the scale since fixed is the default option.

should scales be fixed ("fixed", the default), free ("free"), or free in one dimension ("free_x", "free_y")

data_text <- "
Time    A   B   C   D
2013-01-01  69.23580    8.708641    NaN 3.452663
2013-02-01  69.46016    8.353662    NaN 4.601604
2013-03-01  69.62315    9.156498    NaN 3.953704
2013-04-01  71.04144    7.817074    NaN 3.762032
2013-05-01  74.29557    7.971487    NaN 4.475369
"
temp <- read.table(text=data_text, header=TRUE, stringsAsFactors=FALSE)
library(reshape2)

temp <- melt(temp, id.vars=c("Time"))
temp$Time <- as.Date(temp$Time)
library(ggplot2)

library(plyr)
temp <- subset(temp, value > 0)
q2 <- ggplot(data=temp) + geom_line(aes(x=Time, y=value, color=variable), size = 1) + facet_wrap(~ variable, scales = "free_y")

q2 

enter image description here

Upvotes: 2

Related Questions