1
votes

I have an older ggplot2 book and it is outdated. It mentions using qplot, and opts.

I am trying to learn how to setup a ggplot2 graph correctly. I would like to know if my following code is the "correct" or "accepted" way of doing things.

I would like to pass themes to a plot, like title, axis titles, grid lines, colors of plot window, etc...

Currently this code block works until I attempt to pass a theme where I change the title. Here is the code, and a picture of the graph it makes before I pass the theme

brooks.sr <- ggplot(data=sim_dat, aes(x=rates, y=means))
brooks.sr.line <-  brooks.sr + geom_line(data=sim_dat[sim_dat$stream=="Brooks", ]) + 
geom_hline(aes(yintercept=.944), colour = "red")

enter image description here

then I try to add this line of code

brooks.sr.line + theme(title = element_text("Mean LWD Density across Sampling Rates"))

And i get this error

> brooks.sr.line + theme(title = element_text("Mean LWD Density across    Sampling Rates"))
Warning messages:
1: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
  font family not found in Windows font database
2: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
  font family not found in Windows font database
3: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
  font family not found in Windows font database
4: In grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y,  :
  font family not found in Windows font database
5: In grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y,  :
  font family not found in Windows font database

I know have the following code block, which seems better, and I have added a title and line. But, I can not find out how to change the Axis titles, I can see where you can pass the theme and element to modify the text of the title, but not change the title itself

speelyai.sr <- ggplot(data=sim_dat, aes(x=rates, y=means))
speelyai.sr.line <-  speelyai.sr +   
geom_line(data=sim_dat[sim_dat$stream=="Speelyai", ]) + 
geom_hline(aes(yintercept=.584), colour = "red");speelyai.sr.line
speelyai.sr.line + ggtitle("Mean LWD Density using Simple Random 
 Sampling\nunder different Sampling Efforts") +
               theme(plot.title=element_text(size=rel(1.5), family="Times", face="bold"))

enter image description here Thanks for any help

1
theme is used to control the appearance, while the content has to be set with other functions, e..g ggtitle() for the title text, labs() for the axis titles, etc. - baptiste
Spot on. Thanks Baptiste. - Christopher

1 Answers

2
votes

This should work for titling the plot:

+ labs(title = expression("Mean LWD Density across Sampling Rates"))

You can also label the y and x-axes from the same line of code:

+ labs(x="X-Title", y="Y-Title", title = expression("Mean LWD Density across Sampling Rates"))