简体   繁体   English

在ggplot2中添加图例标题和主标题

[英]Adding legend title and main title in ggplot2

Hi I am trying to use ggplot2 to do the following 嗨,我正在尝试使用ggplot2来执行以下操作

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 1)将图例名称更改为“asset”2)将标题添加到顶部3)将每个面板的边框更改为更加纯实的暗线4)想要更改每个面板图标题“corp”的名称和颜色等等

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. 1)您可以通过将“Asset”作为scale_color_manual函数中的第一个参数将图例名称更改为“Asset”。

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

2) You can do 2)你可以做到

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) 3)您可以为theme()添加一个额外的参数来更改背景颜色(这将使框架周围出现边框)

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

source: How to place divisions between facet grid lines source: 如何在facet网格线之间放置分隔

You could also try adding p=p+theme_bw() earlier in the expression, but that might change too many things. 你也可以尝试在表达式的前面添加p=p+theme_bw() ,但这可能会改变很多东西。

4) For the grid labels, you have 2 options (well, more, but these are the easiest). 4)对于网格标签,您有2个选项(嗯,更多,但这些是最简单的)。 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() . 如果您不想这样做,可以将facet_grid()函数作为参数传递给facet_grid() See the example here: 看这里的例子:

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

For the color of the panel, you can also use theme() for that 对于面板的颜色,您也可以使用theme()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM