在ggplot2中添加图例标题和主标题 [英] Adding legend title and main title in ggplot2

查看:3551
本文介绍了在ggplot2中添加图例标题和主标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试使用ggplot2来完成以下操作:
$ b $ 1将图例名称更改为asset
2)将标题添加到顶部
3)将每个面板的边框更改为更坚实的黑线
4)希望更改每个面板图标题corp等的名称和颜色

这是我的,我不知道如何做到这一点任何帮助,非常感激

  p_ggplot(mystratcodes,aes(x = annRisk,y = annRet))
p <-p + facet_grid(._ sector)+ facet_wrap(〜sector)
p <-p + geom_point (大小= 6,aes(color =(mystratcodes $ subsector1)))
p <-p + scale_x_continuous(labels = percent,name =Annualized Risk)
p <-p + scale_y_continuous =%,name =Annualized Return)$​​ b $ b p <-p + theme(legend.position =bottom,legend.key = element_rect(color =gray))
p <-p + scale_colour_manual(values = c(UTIL=#fdcc8a,IND=#fc8d59,FIN=#d7301f,ABS=#74a9cf,CMBS =#0570b0,LA=#8c96c6,SOV=#88419d,SUPRA=#b3cde3))
print(p)

非常感谢

解决方案

1 )您可以将<资产>作为第一个参数放入 scale_color_manual 函数中,将图例名称更改为资产。

  scale_colour_manual(Asset,values = ...)



2)您可以做

pre $ lt; code> p <-p + labs(title =PUT TITLE HERE)

为标题

<3>您可以添加$ theme()来改变背景颜色(这会在框的周围出现一个边框)。 $ p> p <-p + theme(panel.background = element_rect(fill = NA,col =black))

来源:如何在多面网格线之间进行分割



您也可以尝试在表达式的前面添加 p = p + theme_bw(),但那可能会改变太多的东西。



4)对于网格标签,您有2个选项(以及更多,但这些是最简单的)。首先,您可以重命名数据的级别。如果你不想这样做,你可以在 facet_grid()中创建一个标签程序函数作为参数。看到这里的例子:



https://stackoverflow.com/a/ 12104207/1362215



对于面板的颜色,您还可以使用 theme()

  p <-p +主题(strip.background = element_rect(fill ='purple'))#将方块变成紫色


Hi I am trying to use ggplot2 to do the following

1) Change the legend name to "asset" 2) Add a title to the top 3) Change the border of each panel to a more solid dark line 4) Would like to change the name and color of each panel chart title "corp" etc

This is what I have and I am not sure how to do this Any help is greatly appreciated

p <- ggplot(mystratcodes, aes(x=annRisk, y = annRet))
p<- p + facet_grid(. ~ sector) + facet_wrap(~sector)
p<- p + geom_point(size=6, aes(color = (mystratcodes$subsector1))) 
p<-p+scale_x_continuous(labels = percent, name = "Annualized Risk")
p<-p+scale_y_continuous(labels = percent, name = "Annualized Return")
p<-p+ theme( legend.position = "bottom", legend.key = element_rect(colour = "grey"))
p<-p + scale_colour_manual(values = c("UTIL" = "#fdcc8a", "IND" = "#fc8d59", "FIN" =          "#d7301f","ABS" = "#74a9cf", "CMBS" = "#0570b0", "LA" = "#8c96c6", "SOV"= "#88419d", "SUPRA" = "#b3cde3"))
print(p)

Thanks so much

解决方案

1) You can change the legend name to "Asset" by putting "Asset" as the first parameter in the scale_color_manual function.

scale_colour_manual("Asset",values = ...)

2) You can do

p<-p+labs(title="PUT TITLE HERE")

for the title

3) You can add an additional argument to theme() to change the background color (which will make a border appear around the box)

p<-p+theme(panel.background = element_rect(fill=NA, col="black"))

source: How to place divisions between facet grid lines

You could also try adding p=p+theme_bw() earlier in the expression, but that might change too many things.

4) For the grid labels, you have 2 options (well, more, but these are the easiest). Firstly, you can rename the levels of your data. If you don't want to do that, you can make a labeller function to pass as an argument in facet_grid(). See the example here:

https://stackoverflow.com/a/12104207/1362215

For the color of the panel, you can also use theme() for that

p<-p+theme(strip.background = element_rect(fill = 'purple'))#turns boxes purple

这篇关于在ggplot2中添加图例标题和主标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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