10

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
  • Update: For those who find labels = percent will not work with new ggplot, i,e, if you see Error in check_breaks_labels(breaks, labels) : object 'percent' not found error, then add this scales:: bit in front of percent scales::percent Commented Feb 16, 2016 at 8:59

1 Answer 1

16

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks very much for your help. I have one more question. How do I size the points based on a column called capwt. I would like three sizes above 10%, 5-10%, and below 5%. Thanks so much for your help
You can use aes(...,size=colvariable) and scale_size_identity(values=c(colvariable=1.1*a,varother1=a,varother2=a,...).
You can also use two separate geom_point expressions, where you use the parameter subset=(mystratcodes$sector=='capwt') and subset=(mystratcodes$sector!='capwt') See here for more details: mikewhitfield.wordpress.com/2013/11/07/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.