在单个图表中绘制具有不同值范围的多个系列

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

我有以下数据:

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

我尝试将三个系列绘制在一起,如下所示:

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

我得到了以下图表:

我无法清楚地绘制出 repo & revrepo var,因为 nse 变量的值相对大于这些 variables.Since 我必须展示 nse 与 repo & revrepo var 的共同运动,是有什么方法可以压缩 y 轴,以便可以清楚地看到所有三个系列?

您可以尝试 facet_wrap 自由秤。

代码:

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()

结果:

数据:

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. 您可以使用 reshape2 包中的 melt() 将数据转换为 ggplot2。像这样,您不需要使用相同的参数添加三次 geom_line()(例如,size = 1.4)。
  2. 而不是 breaks = c(2,4,6,8,10) 你可以写 breaks = seq(2, 10, 2).
  3. 使用 color = NULL 而不是 color = "" 因为仍然添加空字符。

您可以将 nse 值除以 1000 并在 y-axis 上发表评论 "nse in thousands" 或在免费 space 的面板,(或作为副标题,就像我在下面所做的那样)。

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