我正在研究这个df:
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)
我知道通常,如果我想在x轴上每周打印一个值,我应该创建这个ggplot函数:
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"))
我的问题是我必须使用此函数创建相同的x轴值:
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)
我不知道如何用聚集转换x值的日期以及如何跳转值,就像上一个ggplot函数一样
答案 0 :(得分:1)
你能不能只输入mutate
声明?
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)