2016-06-20 24 views
0

Im auf dieser df arbeiten:Facet überspringen Wert x-Achse

library("ggplot2") 
library("reshape2") 
library("tidyr") 
library("scales") 
library("dplyr") 


Col0 <- c("AA", "BB", "CC", "DD","EE","FF") 
D01012015 <- c(2,2,2,6,1,NA) 
D02012015 <- c(2,2,2,1,3,1) 
D03012015 <- c(2,2,3,4,6,4) 
D04012015 <- c(2,2,3,1,2,4) 
D05012015 <- c(2,1,1,1,1,0) 
D06012015 <- c(2,4,2,5,4,9) 
D07012015 <- c(2,4,2,5,4,1) 
D08012015 <- c(2,2,3,4,5,3) 
D09012015 <- c(1,3,3,2,2,1) 
D10012015 <- c(1,3,3,2,2,1) 
D11012015 <- c(1,3,3,2,4,1) 
D12012015 <- c(1,3,3,4,2,1) 
D13012015 <- c(1,3,5,2,2,1) 
D14012015 <- c(1,3,3,7,2,1) 
D15012015 <- c(1,3,3,7,2,7) 

df<-data.frame(Col0,D01012015,D02012015,D03012015,D04012015,D05012015,D06012015,D07012015,D08012015,D09012015,D10012015,D11012015, 
       D12012015,D13012015,D14012015,D15012015) 

Ich weiß normalerweise, dass, wenn ich auf der x-Achse einen Wert pro Woche drucken mag, dass ich diese ggplot Funktion erstellen soll:

f<-melt(df,id =c("Col0")) 
f$date<-as.Date(f$variable, format="D%d%m%Y") 
pl<- ggplot(f, aes(date, value, fill=Col0))+ geom_line(aes(color=Col0,group=Col0))+ scale_x_date(breaks = date_breaks("1 week")) 

Mein Problem ist, dass ich die gleichen x-Achse Werte zu schaffen haben, diese Funktion verwenden:

plotfun = function(data) { 

    xval<-"dates" 
    column<- names(data)[1] 
    data %>% 
    gather_(xval, "Val", select_vars_(names(.), 
             names(.), 
             exclude = column)) %>% 
    ggplot(aes_string(xval, "Val", group = column, col = column)) + 
    facet_grid(as.formula(paste(column, "~."))) + 
    geom_line() 
} 

plotfun(df) 

ich weiß nicht, wie in Datum umwandeln s die x-Werte mit sammeln und wie Werte wie in der vorherigen ggplot-Funktion zu springen

+0

Sie bitte ein [reproduzierbares Beispiel] (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example); Ihr aktueller Code wird nicht in einer neuen Umgebung ausgeführt, da nicht alle Pakete geladen sind. – Heroka

+0

@Heroka Ich habe die Frage bearbeitet und die Bibliotheken hinzugefügt – juse

Antwort

1

Können Sie nicht einfach in eine mutate Aussage setzen?

plotfun <- function(data) { 

    xval <- "dates" 
    column <- names(data)[1] 
    data %>% 
    gather_(xval, "Val", select_vars_(names(.), 
             names(.), 
             exclude = column)) %>% 
    mutate(dates = as.Date(f$variable, format = "D%d%m%Y")) %>% 
    ggplot(aes_string(xval, "Val", group = column, col = column)) + 
    facet_grid(as.formula(paste(column, "~."))) + 
    geom_line() 
} 

plotfun(df) 
+0

Es hat perfekt funktioniert, danke – juse