将图例文本包裹在ggplot2中 [英] Wrap legend text in ggplot2
问题描述
在使用ggplot2生成图形时,我有一些很长的图例名称,我希望用两行来包装。例如:
a < - (1:10)
b < - c(1,1.5,2 ,4,5,5.3,7,9,9.5,9.8)
places = c(Birmingham,Chester-le-street,Cambridge,Newcastle upon Tyne,Peterborough ,剑桥,纽卡斯尔泰恩河,彼得伯勒,利物浦,埃文河畔斯特拉特福)
df1 = data.frame(a,b,places)
library (ggplot2)
i = ggplot(df1,aes(x = a,y = b))+ geom_point(aes(color = places),size = 3)+ opts(legend.position =bottom)
当箱子设置在底部时,我将如何去包装图例项目 - 例如2或3行?此刻传说中的七个项目彼此相邻。我宁愿他们显示在两行(第一排上有四个城镇,第二排上有三个城镇)。
非常感谢你提前致谢。
好吧,考虑到您的编辑,您可能需要这样做:
<$ p $ (color = guide_legend(nrow = 2))
但是您可能会发现您仍然希望采用文本包装技术,以适应它。
Whilst producing graphs using ggplot2, I have some long legend names which I wish to wrap over two lines. For example:
a <- (1:10)
b <- c(1,1.5,2,4,5,5.3,7,9,9.5,9.8)
places = c("Birmingham","Chester-le-street","Cambridge", "Newcastle-upon-Tyne","Peterborough","Cambridge", "Newcastle-upon-Tyne","Peterborough","Liverpool","Stratford-upon-Avon")
df1 = data.frame(a,b,places)
library(ggplot2)
i = ggplot(df1, aes(x=a, y=b)) + geom_point(aes(colour = places), size=3) + opts(legend.position="bottom")
How would I go about wrapping the legend items when the box is set to be at the bottom - say in 2 or 3 lines? At the moment the seven items of the legend are next to each other. I would prefer that they are displayed in two rows (with say four towns on the top row and three towns on the second row).
Many thanks in advance.
Ok, given your edits, you probably wanted this:
library(scales)
i + guides(colour = guide_legend(nrow = 2))
But you may find that you still want to employ the text wrapping technique as well, to get it to fit.
这篇关于将图例文本包裹在ggplot2中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!