ggplot2:绘制几何图形中的因子顺序 [英] ggplot2: plotting order of factors within a geom

查看:178
本文介绍了ggplot2:绘制几何图形中的因子顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由5个组组成的(密集)数据集,所以我的data.frame看起来像x,y,group。我可以绘制这些数据并根据他们的组使用以下几种颜色来绘制点:

I have a (dense) dataset that consist of 5 groups, so my data.frame looks something like x,y,group. I can plot this data and colour the points based on their group using:

p= ggplot(dataset, aes(x,y))
p = p + geom_point(aes(colour = group))

我的问题是现在只是我想控制哪个组在顶部。目前看起来这是随机决定的(至少我似乎无法弄清楚什么使得某些东西成为顶点)。在ggplot2中有什么方法可以告诉geom_point点的顺序应该是什么?

My problem is now only that I want to control which group is on top. At the moment it looks like this is randomly decided for (at least I don't seem to be able to figure out what makes something be the "top" dot). Is there any way in ggplot2 to tell geom_point what the order of dots should be?

推荐答案

创建因子变量时,可以使用levels参数影响排序

When you create the factor variable, you can influence the ordering using the levels parameter

f = factor(c('one', 'two'), levels = c('one', 'two'))
dataset = data.frame(x=1:2, y=1:2, group=f)
p = ggplot(dataset, aes(x,y))
p = p + geom_point(aes(colour = group))

现在,ggplot使用此顺序作为图例。

Now, ggplot uses this order for the legend.

这篇关于ggplot2:绘制几何图形中的因子顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆