Nishant
Nishant

Reputation: 1121

Plot multiple series with different range of values in a single chart

I have the following data:

df_nse_1

  Number repo revrepo  nse
1      1  5.0     4.0 4579
2      2  5.5     4.5 4781
3      3  5.8     4.8 4883
4      4  6.0     5.0 4984
5      5  6.0     5.0 5002
6      6  6.0     5.0 5038
7      7  6.2     5.2 5085
8      8  6.5     5.5 5187
9      9  7.0     6.0 5389

I am tried plotting the three series together as below:

library(ggplot2)
p_nse <- ggplot() + 
  geom_line(data = df_nse_1, aes(x=Number, y=repo, color = "Repo"),size=1.4) + geom_line(data = df_nse_1, aes(x=Number, y=revrepo, color = "Reverse Repo"),size=1.4) + geom_line(data = df_nse_1, aes(x=Number, y=nse, color = "nse"),size=1.4) + ylim(2,5400) + scale_x_continuous(breaks = c(2,4,6,8,10))+ labs(color="") + xlab('\nNumber') + ylab('LAF rates (%) & Estimated nse prices\n') + ggtitle("Estimated nse closing Prices given changes in LAF & CRR Rates") + theme(axis.text.x=element_text(size=12, color = "black")) + theme(plot.title = element_text(lineheight=.8, face="bold",colour = "black",hjust = 0.5,vjust = 2,size = 10))

p_nse

I got the following chart: enter image description here

I am unable to get the repo & revrepo var plotted clearly as the values of nse variable is relative bigger than these variables.Since i Have to show the co-movement of nse with repo & revrepo var, Is there a way where the y axis can be squeezed so that all three series can be seen clearly?

Upvotes: 0

Views: 1892

Answers (2)

knb
knb

Reputation: 9295

You could divide nse values by 1000 and put a comment "nse in thousands" on the y-axis, or as a comment in the free space of the panel, (or as a subtitle, as I did below).

library(tidyverse)

df_nse_1 <- dat <- structure(list(Number = 1:9, repo = c(
  5, 5.5, 5.8, 6, 6, 6, 6.2, 6.5, 7), 
revrepo = c(4, 4.5, 4.8, 5, 5, 5, 5.2, 5.5, 6), nse = c(
  4579L, 4781L, 4883L, 4984L, 5002L, 5038L, 5085L, 5187L, 5389L
)), .Names = c(
  "Number", "repo", "revrepo", "nse"
), row.names = c(NA, -9L), class = "data.frame")

df_nse_1 <- df_nse_1 %>% 
        mutate(nse = nse / 1000) 

p_nse <- ggplot() +
  geom_line(data = df_nse_1, 
            aes(x = Number, y = repo, color = "Repo"), size = 1.4) + 
        geom_line(data = df_nse_1, 
                  aes(x = Number, y = revrepo, color = "Reverse Repo"), 
                  size = 1.4) + 
        geom_line(data = df_nse_1, 
                  aes(x = Number, y = nse, color = "nse"), size = 1.4) + 
        #ylim(2, 5400) + 
        scale_x_continuous(breaks = c(2, 4, 6, 8, 10)) + 
        labs(color = "") + 
        xlab("\nNumber") + 
        ylab("LAF rates (%) & Estimated nse prices\n") + 
        ggtitle("Estimated nse closing Prices given changes in LAF & CRR Rates",
                "[nse closing Prices in thousands]") + 
        theme(axis.text.x = element_text(size = 12, color = "black")) + 
        theme(plot.title = element_text(lineheight = .8, 
                                        face = "bold", colour = "black", 
                                        hjust = 0.5, vjust = 2, size = 10))

p_nse

enter image description here

Upvotes: 1

pogibas
pogibas

Reputation: 28329

You can try facet_wrap with free scales.

Code:

library(ggplot2)
library(reshape2)

ggplot(melt(df, "Number"), aes(Number, value, color = variable)) +
    geom_line(size = 1.4) +
    facet_wrap(~ variable, scales = "free") +
    scale_x_continuous(breaks = seq(2, 10, 2)) +
    labs(title = "Estimated nse closing prices",
         subtitle = "Given changes in LAF & CRR Rates",
         x = "Number",
         y = "LAF rates (%) & Estimated nse prices",
         color = NULL) +
    theme_classic()

Result:

enter image description here

Data:

structure(list(Number = 1:9, repo = c(5, 5.5, 5.8, 6, 6, 6, 6.2, 
6.5, 7), revrepo = c(4, 4.5, 4.8, 5, 5, 5, 5.2, 5.5, 6), nse = c(4579L, 
4781L, 4883L, 4984L, 5002L, 5038L, 5085L, 5187L, 5389L)), .Names = c("Number", 
"repo", "revrepo", "nse"), row.names = c(NA, -9L), class = "data.frame")

PS:

  1. You can use melt() from reshape2 package to transform your data at pass it to ggplot2. Like this you won't need to add geom_line() three times with same parameters (eg., size = 1.4).
  2. Instead of breaks = c(2,4,6,8,10) you can write breaks = seq(2, 10, 2).
  3. Use color = NULL instead of color = "" as still adds empty character.

Upvotes: 1

Related Questions