如何重新排列图例中的项目? [英] How to reorder the items in a legend?

查看:233
本文介绍了如何重新排列图例中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变图例项目出现的顺序。我在这里花了大约一个小时,没有结果。



以下是一个示例设置:

<$ p $ (0)
d < - data.frame(x = runif(3),y = runif(3),a = c ('1','3','10'))

这是许多事情之一我试过了:

  ggplot(d,aes(x = x,y = y))+ 
geom_point (size = 7,aes(color = a,order = as.numeric(a)))



(当然,我的天真希望是图例项目会以数字顺序显示:1,3 ,10。)

解决方案

ggplot 通常会根据您的因子值到该因子的 levels()。你最好确定这是你想要的顺序,否则你会用R中的许多函数来对抗,但是你可以通过操纵色标来手动改变它:



< pre $ ggplot(d,aes(x = x,y = y))+
geom_point(size = 7,aes(color = a))+
scale_color_discrete (break = c(1,3,10))


I'm trying to change the order in which legend items appear. I've spent about an hour at this, with no results.

Here's an example setup:

library(ggplot2)
set.seed(0)
d <- data.frame(x = runif(3), y = runif(3), a = c('1', '3', '10'))

And here's one of the many things I've tried:

ggplot(d, aes(x = x, y = y)) + 
    geom_point(size=7, aes(color = a, order = as.numeric(a)))

(My naive hope, of course, was that the legend items would be shown in the numeric order: 1, 3, 10.)

解决方案

ggplot will usually order your factor values according to the levels() of the factor. You are best of making sure that is the order you want otherwise you will be fighting with a lot of function in R, but you can manually change this by manipulating the color scale:

ggplot(d, aes(x = x, y = y)) + 
    geom_point(size=7, aes(color = a)) + 
    scale_color_discrete(breaks=c("1","3","10"))

这篇关于如何重新排列图例中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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