Categories

Nov 14, 2019 in R Language

Q: Add information to the graph in R Language

1 Answer

Nov 14, 2019

So far, we haven't added information in the graphs. Graphs need to be informative. The reader should see the story behind the data analysis just by looking at the graph without referring additional documentation. Hence, graphs need good labels. You can add labels with labs()function.

The basic syntax for lab() is :

lab(title = "Hello Guru99")
argument:
- title: Control the title. It is possible to change or add title with:			
- subtitle: Add subtitle below title			
- caption: Add caption below the graph			
- x: rename x-axis			
- y: rename y-axis			
Example:lab(title = "Hello Guru99", subtitle = "My first plot")	

Add a title

One mandatory information to add is obviously a title.

my_graph +
    labs(
        title = "Plot Mile per hours and drat, in log"
         )

Code Explanation

  • my_graph: You use the graph you stored. It avoids rewriting all the codes each time you add new information to the graph.
  • You wrap the title inside the lab().
  • Code for the red color of the line
  • se = FALSE: Don't display the standard error
  • size = 1: the size of the line is 1
  • Add a title with a dynamic name

    A dynamic title is helpful to add more precise information in the title.

    You can use the paste() function to print static text and dynamic text. The basic syntax of paste() is:

    paste("This is a text", A)
    arguments
    - " ": Text inside the quotation marks are the static text
    - A: Display the variable stored in A
    - Note you can add as much static text and variable as you want. You need to separate them with a comma	
  • 
    

    Add a subtitle

    Two additional detail can make your graph more explicit. You are talking about the subtitle and the caption. The subtitle goes right below the title. The caption can inform about who did the computation and the source of the data.

    my_graph +
        labs(
            title =
            "Relation between Mile per hours and drat",
            subtitle =
            "Relationship break down by gear class",
            caption = "Authors own computation"
        )
  • 
    

    Rename x-axis and y-axis

    Variables itself in the dataset might not always be explicit or by convention use the _ when there are multiple words (i.e. GDP_CAP). You don't want such name appear in your graph. It is important to change the name or add more details, like the units.

    my_graph +
        labs(
            x = "Drat definition",
            y = "Mile per hours",
            color = "Gear",
            title = "Relation between Mile per hours and drat",
            subtitle = "Relationship break down by gear class",
            caption = "Authors own computation"
        )
Click here to read more about Loan/Mortgage
Click here to read more about Insurance

Related questions

Madanswer
Jul 28, 2019 in R Language
Nov 4, 2019 in R Language
Jul 28, 2019 in R Language
...