DS4PS / cpp-526-sum-2020

Course shell for CPP 526 Foundations of Data Science I for Summer 2020.
http://ds4ps.org/cpp-526-sum-2020/
MIT License
2 stars 1 forks source link

Lab-02 Figure Captions and Alignment #12

Open RTrehern-ASU opened 4 years ago

RTrehern-ASU commented 4 years ago

Has anyone else figured out how to add captions to the plots and how to get two plots to align side by side as on question 3?

I know the formatting issue is not a specific requirement for this lab, but I have been playing around with it for a while and just trying to figure out how to do it.

I was able to add captions in bold at the bottom center of each plot using the character string " {r, fig.cap='Vacant Lots', fig.align='center'}"

Just wondering if there is a way to get them to appear at the top center as they do in the lab file. Thanks.

jamisoncrawford commented 4 years ago

Great questions, @RTrehern-ASU, and way to go above and beyond for this assignment!

I actually responded to this by memory from reading it late last night/this morning and so did not notice that you found one approach to solve this issue (which is awesome!). Here's the answer you already found plus a couple other approaches.


Captions Using Code Chunk Options

Although you can insert an image into an R Markdown product using regular Markdown syntax, any plots that you create should be done inside "code chunks". Because that's the case, you use "chunk options" to be able to format any plots output from your code (i.e. "figures").

We add options inside the {r} of each "code chunk", and separate each argument with commas (,). You've likely seen these already in the lab templates. Most options are "logical" (i.e. TRUE or FALSE), but some accept numeric values and text, like customizing captions.

All "chunk options" related to plot or graphics output begin with fig., for example:

The argument you need for captions is: fig.cap =, and you follow it with a string of text in quotations. For example:

{r fig.cap = "This is a caption."}

You may want a centered caption, in which case you'll want to combine this with fig.align =, like so:

{r fig.cap = "This is a caption", fig.align = "center"}


Captions Using Markdown

I happen to find captions with "chunk options" a bit limited, so I tend to add them outside of code chunks. You can wrap words in * for italics and <center> and </center> to center them outside of a code chunk!.

Here's what that might look like:

<center> *This is my caption.* </center>

You can use the same method for adding a centered title above "chunk" output - and you can format in bold by wrapping the text in **.

<center> **This is my title.** </center>
jamisoncrawford commented 4 years ago

@RTrehern-ASU in response to how to make images side by side, at least for Base R plots, this involves setting the graphical parameters parameters using function par(). Inside, you can specify the number of plots in each row and each column. For example, a side by side pair of plots is technically a single row and two columns.


Creating Side By Side Plots

To do this, inside par(), use arguments mfrow = for row-wise alignment and mfcol = for column-wise alignment - in other words, because images are entered in an order (i.e. the first image, the second image), do you want them to fill in the rows first, or the columns first? You should only use one of these arguments in par().

Both mfrow = or mfcol = take a numeric argument with two elements, so you combine and separate those elements with c(). The first element is the number of rows, and the second, columns. So mfrow = c(1, 2) will get you a single row and two columns.

par(mfrow = c(1, 2))


Testing It in R Markdown

Try testing the following code in a .Rmd document. The datasets are built-in, so you don't need any other code except this:

<center> **This is my title.** </center>

```{r fig.align = "center", echo = FALSE}

par(mfrow = c(1, 2))
plot(pressure)
plot(faithful)
*This is my caption.*


<br>

Hope this helps!
lecy commented 4 years ago

If you are curious, this is the code for Q3 on the lab (logical statements suppressed so not fully reproducible, but you can see the use of par( mfrow=() ) ):

par( mfrow=c(1,2), mar=c(0,0,2,0) )

plot( downtown,  border="gray20", col=group.colors,
      main="Built After 1980")    

plot( downtown,  border="gray20", col=group.colors,
      main="Commercial Properties")    

The second graphic uses the layout() function, which is more customizable but also a more complicated because you create a grid that designates the size of each graph in the plot. Each new plot will be added to the layout in the order specified in the grid matrix.

par( mar=c(0,0,2,0) )

grid <- c( 3, 3, 3, 3, 1, 1, 
           3, 3, 3, 3, 1, 1,  
           3, 3, 3, 3, 2, 2, 
           3, 3, 3, 3, 2, 2 )

layout( matrix( grid , 4, 6, byrow = TRUE) )

# plot(...)    # number 1
# plot(...)    # number 2
# plot(...)    # number 3
lecy commented 4 years ago

Also note that when you were asking about captions you might have been referring to the plot titles, designated by the argument main= in the plot function.

Captions in code chunks will add text below the plots. Titles add text to the images themselves. Try highlighting the text and you will see the difference.

plot( downtown,  border="gray20", col=group.colors,
      main="Built After 1980")    
RTrehern-ASU commented 4 years ago

Thank you both for providing solutions! I guess what I was referring to as "captions" were actually "titles". When I added the argument main= it added the titles as I was wanting them to appear. I also applied the par( mfrow) argument and was able to align the plots side by side.

jamisoncrawford commented 4 years ago

Thank you @lecy and you're welcome @RTrehern-ASU!

P.S. Wow, I've created, run, and published hundreds of plots, and I have never used main =! If only it started with fig.main =!