我试图在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")
我得到"错误:提供给连续刻度的离散值"错误。但是endorse变量(应该是y变量)是数字的,所以我不确定问题是什么。日期显然是离散的。
答案 0 :(得分:1)
一个建议是使用scale_x_date
代替scale_x_discrete
。例如:
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_date() +
scale_y_discrete(limits=c(0,30)) +
xlab("Date")