ggplot2中出现意外的主题变化

时间:2017-02-02 17:11:45

标签: r ggplot2

我在ggplot2的外观中遇到了意外行为。当我绘制大量数据时,默认主题似乎从theme_grey变为theme_bw。我可以在我正在处理的特定数据集上重现这一点,但不能在模拟数据上重现它。

无论如何,这里是代码:

ggplot(df2, aes(x = Sequence, y = y, color = as.factor(group))) +
geom_point(shape=19, alpha = 0.8)
nrow(df2)
[1] 4330

导致: enter image description here

现在,如果我获取数据的子集:

df3 <- slice(df2, 1:10) 
ggplot(df3, aes(x = Sequence, y = y, color = as.factor(group))) +
    geom_point(shape=19, alpha = 0.8)

导致: enter image description here

我试过了:

  • 卸载/重新安装ggp​​lot2
  • 手动指定主题
  • 卸载除ggplot2之外的所有包
  • 在项目之外工作

5个样本:

> dput(df2[1:5, ])
structure(list(Sequence = c("1", "2", "3", "4", "5"), group = c(0, 
0, 0, 0, 0), y = c(7711.945, 7695.075, 3432.585, 8081.19, 7344.455
)), .Names = c("Sequence", "group", "y"), row.names = c(NA, 5L
), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

您对'x'的输入目前存储为一个因素(我猜)。以下代码将重现您遇到的问题,将x转换为数字的最后一行修复了该问题。

# make some test input
n <- 5000
df <- data.frame(x = factor(1:n), y = rnorm(n), group = sample(0:1, n, replace = T))

library(ggplot2)

# Using the x "as is" which is currently a factor
ggplot(df, aes(x = x, y =y, color = as.factor(group))) + geom_point(shape = 19, alpha = 0.8)
# Converting to numeric we see the desired result
ggplot(df, aes(x = as.numeric(x), y =y, color = as.factor(group))) + geom_point(shape = 19, alpha = 0.8)