在R代码中使用ggplot时在图例列表中创建列 [英] creating columns within a legend list while using ggplot in R code

查看:142
本文介绍了在R代码中使用ggplot时在图例列表中创建列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ggplot (包名称:ggplot2)绘制15行,每行代表一个单独的实体,并且希望为此创建图例。但是,我无法将图例条目划分为多个列。有人可以建议如何在ggplot环境中做同样的事情。

I am plotting 15 lines using ggplot (package name: ggplot2), each representing a separate entity and wish to create a legend for the same. However, I am not able to divide the legend entries into more than one column. Can someone please suggest how to do the same in ggplot environment.

目前,我正在使用以下命令来创建图例:

Presently, I am using the following command to create legend:

opts(title=plotName,legend.position='bottom')

然而,这给出了一个列的图例。结果,图表中的大面积是由传奇本身。将它分成2列或3列在将图例保留在图表底部的情况下确实有助于原因。
我也尝试过 legend.direction ,但是这个命令在一行中显示图例,除非我可以传播2-3行,否则这是不可取的。 >

However, this gives a one column legend. As a result a large area in the chart is taken by legend itself. Dividing it into 2 or 3 columns would really help the cause while keeping the legend at the bottom of the chart. I also tried legend.direction but this command displays legend in one row which is not desirable either unless I may spread it across 2-3 rows.

opts(title=plotName,legend.position='bottom',legend.direction="horizontal")

在此先感谢您,
Munish

Thanks in advance, Munish

推荐答案

使用ggplot的新主题环境只需要一个简单的: + guides(col = guide_legend(ncol = 2))来将图例格式化为2列。

Using the new themes environment of ggplot requires only a simple: + guides(col=guide_legend(ncol=2)) to format your legend in 2 columns.

这篇关于在R代码中使用ggplot时在图例列表中创建列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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