# How to plot two graphs in the same plot in R and R Shiny We as data analyst always have to do basic EDA or data manipulating before sorting the variable for developing any model. One of the first steps that we follow in EDA is bi-variate analysis. I will not go into details how or why we do bi-variate analysis, as you must know already and just want to learn how to plot 2 graphs in the same plot area in R. So let’s get started. I simply wrote an R code with comments to understand each step. Please check out the code below and comment if any step is not clear. Good luck.

```################################################################
################### R Tutorial ###############################

# Purpose: Draw 2 graphs in the same plot using par function
# Author: Deepesh Singh

################################################################

#Creating random dataset
set.seed(199123)

# Creating random population by year
men <- seq (10, 22, by = ((22-10)/(35 - 1)))
income <- sample(25, size = 35, replace = T)
year <- seq(1982,2016)

# Checking the length of each variable to create a dataframe
length(men)
length(income)
length(year)

population_income <- data.frame(year,men,income)

# Now let's plot both variables (men & income) in the same graph

# Setting margin
par(mar= c(5,4,4,5) + 0.1)

# Plot graph with one variable
attach(population_income)
plot(x = year, y = men, col = "blue", type = "l", ylab = "Men population",
main = "Men vs Income over the year")
par(new = T)
# Plot with another variable
plot(x = year, y = income, col = "green", type = "l", xaxt = "n",
yaxt = "n", xlab = "", ylab = "")
axis(4)
mtext("Income by year", side = 4, line = 3)
detach(population_income)

``` ##### Signature

Deepesh Singh ## 3 thoughts on “How to plot two graphs in the same plot in R and R Shiny”

1. Gerardo

Tried to use it in shiny app, but its not working, are there any additional steps to make it work? thanks a lot

2. Anonymous

Works perfectly in R. Exactly what I needed – thank you 🙂

• Deepesh Singh

Welcome 🙂
{"ft_lang_label":"__label__en","ft_lang_prob":0.8120967,"math_prob":0.99034876,"size":1891,"snap":"2022-40-2023-06","text_gpt3_token_len":468,"char_repetition_ratio":0.18335983,"word_repetition_ratio":0.022151899,"special_character_ratio":0.36805922,"punctuation_ratio":0.11815562,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.998656,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12],"im_url_duplicate_count":[null,null,null,5,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-04T06:50:44Z\",\"WARC-Record-ID\":\"<urn:uuid:548f5cb6-1c94-4ff9-9603-da3422e0eae2>\",\"Content-Length\":\"98782\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a601a212-04fc-4e59-bc8d-c4cfc12f8db7>\",\"WARC-Concurrent-To\":\"<urn:uuid:fd789d61-bbcc-4bc0-ab9e-66bc7163e4f9>\",\"WARC-IP-Address\":\"192.0.78.24\",\"WARC-Target-URI\":\"https://topbullets.com/2017/02/18/how-to-plot-two-graphs-in-the-same-plot-in-r-and-r-shiny/\",\"WARC-Payload-Digest\":\"sha1:TMZZCU7XUMBPHW3JXVGG7DDU7PYETIFJ\",\"WARC-Block-Digest\":\"sha1:XOQPS3OEQVZHCA66R5WW55TNAMRFYRTO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030337480.10_warc_CC-MAIN-20221004054641-20221004084641-00693.warc.gz\"}"}