Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding legend title and main title in ggplot2

Tags:

r

ggplot2

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

like image 486
qfd Avatar asked Feb 04 '14 23:02

qfd


1 Answers

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
like image 54
Max Candocia Avatar answered Oct 24 '22 06:10

Max Candocia