如何格式化ggplot2图例中的标签? [英] How to format labels in ggplot2 legend?

查看:389
本文介绍了如何格式化ggplot2图例中的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ggplot2阴谋,通过两个变量对数据进行分组,我想对标签文本的格式进行一些控制。下面使用nlme软件包中的Oxboys数据集来说明这个问题。我在数据中添加了一个名为'label'的列,以便'Subject'和'label'的组合唯一标识'Subject'。在我实际使用的数据中,感兴趣的两个变量是x,y坐标,它们唯一地识别网格上的点,因此下面是对输出格式的奇怪建议。



以上面的图例项目为例,我们如何重新格式化它以显示:
10 10或10,10或10ºN,10ºW*?有什么方法可以利用sprintf(),或者你可以完全想到另一种方式吗?

一种解决方法是在数据框中创建一个新列来定义分组并提供标签。这会比直接在gglpot中进行格式化的工作少吗?

  library(nlme)
library(ggplot2)
df< - transform(Oxboys,label =主题)
p < - ggplot(df,aes(x = Occasion,y = height,
group = interaction(Subject,label),
color = interaction(Subject,label)))
p + geom_line()



*奖励积分可以改变图例标题。

解决方案


一种解决方法是在数据框中创建一个新列来定义组并提供标签。 / p>

这没什么错,而且它隐含地使用 interaction 。如果您由于某种原因而与 interaction 结合,则可以指定一个分隔符:

  interaction(Subject,label,sep =)#10 10 

  df $组<  -  paste(df $ Subject,N,df $ label,W)#10N 10W 

d只是在你的审美映射中使用它。这当然会给传说赋予集团的头衔,但您可以通过命名合适的比例来更改图例标题:

  p + scale_color_discrete(Your Title)


I have a ggplot2 plot that groups data by two variables and I want to exercise some control over how the label text is formatted. This question is illustrated below using the Oxboys data set from the nlme package. I've added a column called 'label' to the data such that 'Subjects' are now uniquely identified by the combination of 'Subject' and 'label'. In the data that I'm actually working with, the two variables of interest are x,y coordinates that uniquely identify points on a grid, hence the odd suggestion for output formatting below.

Taking the top legend item as an example, how can we reformat this so that it displays: "10 10" or "10,10" or "10ºN, 10ºW"*? Is there some way to utilize sprintf() or can you think of another way entirely?

One workaround would be to create a single new column in the data frame to define the groups and provide the labels. Would that be less work than doing the formatting directly in gglpot?

library(nlme)
library(ggplot2)
df <- transform(Oxboys, label = Subject)
p <- ggplot(df, aes(x = Occasion, y = height,
                    group = interaction(Subject, label),
                    color = interaction(Subject, label)))
p + geom_line()

*Bonus points for an answer that changes the legend title.

解决方案

One workaround would be to create a single new column in the data frame to define the groups and provide the labels.

There's nothing wrong with this, and it's implicitly what you're doing by using interaction. If you're wedded to interaction for some reason, you can specify a separator:

interaction(Subject, label, sep=" ") # 10 10

But it would be better to just do as you suggest and create an explicit column in your data:

df$Group <- paste(df$Subject, "N ", df$label, "W") # 10N 10W

Then you'd just use that in your aesthetic mapping. This would of course give the legend a title of "Group", but you can change the legend title by just naming the appropriate scale:

p + scale_color_discrete("Your Title")

这篇关于如何格式化ggplot2图例中的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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