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

时间:2018-01-27 11:24:42

标签: r plot ggplot2 linechart

我有以下数据:

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

我得到了以下图表: enter image description here

我无法获得回购&amp; revrepo var绘制清楚,因为nse变量的值相对大于这些变量。因为我必须显示nse与repo&amp;的共同运动。 revrepo var,有没有办法可以挤压y轴,以便能清楚地看到所有三个系列?

2 个答案:

答案 0 :(得分:1)

您可以尝试免费比例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")

结果:

enter image description here

数据:

melt()

PS:

  1. 您可以使用reshape2包中的ggplot2将数据转换为geom_line()。像这样,您不需要使用相同的参数添加size = 1.4三次(例如,breaks = c(2,4,6,8,10))。
  2. 您可以写breaks = seq(2, 10, 2)
  3. 而不是color = NULL
  4. 使用color = ""代替.featured-foods{ position: relative; } .food-cards{ display: flex; flex-wrap: wrap; position: absolute; left: calc(40% - 30%); li{ width: 20rem; height: 20rem; margin-top: 1rem; margin-left: 1rem; list-style-type: none; } } ,因为仍会添加空字符。

答案 1 :(得分:1)

您可以将nse值除以1000 并在y轴上放置注释"nse in thousands“,或者作为面板的可用空间中的注释,(或作为副标题) ,正如我在下面所做的那样。)

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