我有一个名为prod.dat.ordered.distinct1
的日期向量,看起来像这样:
prod.dat.ordered.distinct1 <- c("2012-07", "2012-08", "2012-09", "2012-10", "2012-11", "2012-12",
"2013-01", "2013-02", "2013-03", "2013-04", "2013-05", "2013-06",
"2013-07", "2013-08", "2013-09", "2013-10", "2013-11", "2013-12"
)
现在在我的绘图中,我在y轴上收到的数字是1到18,而不是我的prod.dat.ordered.distinct1
矢量。
为了在y轴上获取日期值,我必须进行哪些更改?这是我的代码:
mydatal <- testdatensatz[testdatensatzSbaureihe == "F10",]
prod.dat1 <- as.Date(mydata1$produktionsdatumf2, format = "%Y-%m-%d")
prod.dat1 <- format(prod.dat1, format = "%Y-%m")
befund1 <- mydata1$befundnummer
prod.dat.befund1 <- data.frame(p = prod.dat1, b = befund1)
prod.dat.befund.ordered1 <- prod.dat.befund1[order(prod.dat.befund1$p), ]
prod.dat.ordered.distinct1 <- prod.dat.befund.ordered1$p[!duplicated(prod.dat.befund.ordered1$p)]
t1 <- as.data.frame(table(prod.dat.befund1))
matt = matrix(nrow = length(prod.dat.ordered.distinctl), ncol = 2)
Produktionsdatum <- c()
relativeHaufigkeit <- c()
for (i in 1:length(prod.dat.ordered.distinct1))
{
tl.date <- tl[t1$p == prod.dat.ordered.distinct1[i], ]
number.ref1 <- tl.date[tl.date$b == 0, 3]
number.faults1 <- sum(t1.date[as.numeric(t1.date$b) > 1, 3])
Produktionsdatum[i] <- prod.dat.ordered.distinctl[i]
relativeHaufigkeit[i]<- round((number.faults1 / number.refl) * 100, digits = 0)
}
df1 <- data.frame(Produktionsdatum, relativeHaufigkeit)
df1$gr <- df1$Produktionsdatum %in% r
ggplot(df1, aes(x=Produktionsdatum,y=relativeHaufigkeit, color=gr)) +
geom_vline(xintercept=r1, alpha=0.5) +
geom_point()
答案 0 :(得分:0)
与
相同的问题 Produktionsdatum[i] <- prod.dat.ordered.distinct1[i]
它将因子转换为整数。如果将其包装到as.character
函数中,则日期将在x轴上指示。此外,如果您想使用geom_vline
,则需要使用单独的美学。
请参见下面的代码:
prod.dat.ordered.distinct1 <- c("2012-07", "2012-08", "2012-09", "2012-10", "2012-11", "2012-12",
"2013-01", "2013-02", "2013-03", "2013-04", "2013-05", "2013-06",
"2013-07", "2013-08", "2013-09", "2013-10", "2013-11", "2013-12"
)
testdatensatz <- data.frame(baureihe = "F10",
produktionsdatumf2 = c("2012-08-02", "2012-08-03", "2012-09-01", "2012-10-05"),
befundnummer = c(1, 0, 2, 1))
relativeHaufigkeit <- c(1, 2, 3, 1)
t1 <- data.frame(p = factor("2012-07", "2012-08", "2012-09", "2012-10"), b = 0)
mydata1 <- testdatensatz[testdatensatz$baureihe == "F10",]
prod.dat1 <- as.Date(mydata1$produktionsdatumf2, format = "%Y-%m-%d")
prod.dat1 <- format(prod.dat1, format = "%Y-%m")
befund1 <- mydata1$befundnummer
prod.dat.befund1 <- data.frame(p = prod.dat1, b = befund1)
prod.dat.befund.ordered1 <- prod.dat.befund1[order(prod.dat.befund1$p), ]
prod.dat.ordered.distinct1 <- prod.dat.befund.ordered1$p[!duplicated(prod.dat.befund.ordered1$p)]
t1 <- as.data.frame(table(prod.dat.befund1))
matt = matrix(nrow = length(prod.dat.ordered.distinct1), ncol = 2)
Produktionsdatum <- c()
relativeHaufigkeit <- c()
for (i in 1:length(prod.dat.ordered.distinct1)) {
t1.date <- t1[t1$p == prod.dat.ordered.distinct1[i], , drop = FALSE]
# number.ref1 <- t1.date[t1.date$b == 0, 3]
number.ref1 <- t1.date[t1.date$b == 0, 3]
number.faults1 <- sum(t1.date[as.numeric(t1.date$b) > 1, 3])
Produktionsdatum[i] <- as.character(prod.dat.ordered.distinct1[i]) #
relativeHaufigkeit[i]<- round((number.faults1 / number.ref1) * 100, digits = 0)
}
df1 <- data.frame(Produktionsdatum, relativeHaufigkeit, gr = sample(1:2, length(Produktionsdatum), replace = TRUE))
ggplot(df1, aes(x = Produktionsdatum, y = relativeHaufigkeit, color = gr)) +
geom_line() +
geom_vline(aes(xintercept = 1, alpha=0.5)) +
geom_point()
输出: