【问题标题】:Discrete value/continuous scale error in ggplot2 even when I use a numeric即使我使用数字,ggplot2中的离散值/连续比例误差
【发布时间】: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 变量的classDate...如果你希望它是离散的,你需要把它变成factorcharacter 或其他东西。

标签: r ggplot2


【解决方案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")

【讨论】:

  • 啊!完美的。日期不仅仅是离散的。明白了。
猜你喜欢
  • 2014-11-14
  • 2023-03-06
  • 1970-01-01
  • 2021-12-04
  • 2015-11-09
  • 2018-06-20
  • 2014-05-27
  • 2020-10-23
  • 2019-12-18
相关资源
最近更新 更多