【发布时间】:2016-05-15 20:15:49
【问题描述】:
我正在尝试绘制像this Stack Overflow answer 中的累积总和线图。这是我的数据:
example = structure(list(date = structure(c(16594, 16611, 16612, 16616,
16686, 16702, 16723, 16772, 16825, 16827), class = "Date"), endorse = c(13,
1, 1, 3, 2, 1, 2, 5, 1, 1)), .Names = c("date", "endorse"), row.names = c(8L,
10L, 12L, 14L, 26L, 34L, 40L, 53L, 68L, 69L), class = "data.frame")
这是我正在尝试执行的 ggplot2 命令:
ggplot(data = example, aes(x = date, y = cumsum(endorse))) + geom_line() +
geom_point() + theme(axis.text.x = element_text(angle=90, hjust = 1)) +
scale_x_discrete(labels = example$date) + scale_y_continuous(limits=c(0,30)) + xlab("Date")
我收到“错误:提供给连续刻度的离散值”错误。但是背书变量(应该是 y 变量)是数字,所以我不确定是什么问题。日期显然是离散的。
【问题讨论】:
-
但请注意,如果您从情节中删除
scale_x_discrete,错误就会消失。你的date变量的class是Date...如果你希望它是离散的,你需要把它变成factor或character或其他东西。