我在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
现在,如果我获取数据的子集:
df3 <- slice(df2, 1:10)
ggplot(df3, aes(x = Sequence, y = y, color = as.factor(group))) +
geom_point(shape=19, alpha = 0.8)
我试过了:
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")
答案 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)