【发布时间】:2018-05-26 20:49:45
【问题描述】:
d1 <- data.frame(x = runif(n = 10000, min = 0, max = 1), y =
rnorm(10000, sd = 0.2), type = "d1")
d2 <- data.frame(x = runif(n = 10000, min = 0, max = 1), y =
rnorm(10000, mean = 0.2, sd = 0.2), type = "d2")
all_d <- rbind(d1, d2)
ggplot(all_d, aes(x = x, y = y, color = type)) + geom_point()
在这里您可以看到d2 点绘制在d1 点之上。因此,我尝试使用forcats::fct_relevel 进行更改
all_d_other <- all_d
all_d_other$type <- forcats::fct_relevel(all_d_other$type, "d2", "d1")
ggplot(all_d_other, aes(x = x, y = y, color = type)) + geom_point()
并且d2 点仍然在d1 点之上。有没有办法改变它,使d1 点位于d2 点之上?
【问题讨论】:
-
你试过普通的基础
?relevel吗?最坏的情况,您可以为 d1 添加单独的+geom_point(),这将迫使它们被绘制在第二个(在顶部) -
R 3.4.2,ggplot2 2.2.1.9000
-
@C8H10N4O2,我没有尝试基本的“relevel”,因为“levels”似乎按照我的预期进行了修改,并且颜色的变化也显示了
-
参见stackoverflow.com/questions/15706281/…,显然绘制顺序受数据框当前顺序的影响,而不是因子水平。这可能算作对那个问题的欺骗。