我有一些图,我想将x轴和/或y轴折断成不同的n。 为了实现这一点,我有很多功能,例如:
by_two <- function(x) {
seq(0, max(x), by = 2)
}
每张图我都通过哪张纸
:p1 <- ggplot(users_d_total %>% filter(isSame, D_rank == 2), aes(x = D, fill = as.factor(train_user_id))) +
geom_density(alpha = .3) +
labs(title = paste0("Without Normalization Analysis [K = 2]")) +
scale_fill_discrete(name = "Users") +
scale_x_continuous(breaks = by_two)
当我尝试更简单时:
by_n <- function(x,n) {
seq(0, max(x), by = n)
}
但是当我通过n = 0.5或1或任何其他正数的by_n时,我会因为类型错误而出错。
p1 <- ggplot(users_d_total %>% filter(isSame, D_rank == 2), aes(x = D, fill = as.factor(train_user_id))) +
geom_density(alpha = .3) +
labs(title = paste0("Without Normalization Analysis [K = 2]")) +
scale_fill_discrete(name = "Users") +
scale_x_continuous(breaks = by_n(1))
请告知如何使这种更智能的解决方案可行。
答案 0 :(得分:2)
我不知道如何将序列限制为给定数据的范围,但是我不确定是否有必要,因为ggplot无论如何都只使用数据的范围。
# Hacky, but ggplot ignores breaks beyond what's needed for the data
by_n <- function(n) { seq(0, 1000, by = n) }
ggplot(iris,
aes(x = Sepal.Length,
fill = Species)) +
geom_density(alpha = .3) +
labs(title = paste0("Without Normalization Analysis [K = 2]")) +
scale_fill_discrete(name = "Users") +
scale_x_continuous(breaks = by_n(0.5))