代码之家  ›  专栏  ›  技术社区  ›  Raghavan vmvs

如何在ggplot中以宽范围缩放变量并在单个绘图中显示相同的变量

  •  0
  • Raghavan vmvs  · 技术社区  · 7 年前

    我创建了以下数据帧

     # Create a dataframe
    
      Column1 <- c(1:30)
      Column1 <- paste0('Month_', as.character(Column1))
      paste0('Column', as.character(Column1))
      Variable <- c("A", "B", "C", "D", 'E')
      DF <- data.frame(Column1, Variable)
      DF$value <- 0
      DF$value[DF$Variable == "A"] <- runif(length(DF$value[DF$Variable == "A"]), 
          min = 10000, max = 50000)
      DF$value[DF$Variable == "B"] <- runif(length(DF$value[DF$Variable == "A"]), 
          min = 100, max = 500)
      DF$value[DF$Variable=="C"] <- runif(length(DF$value[DF$Variable=="A"]),
          min = 100, max = 500)
      DF$value[DF$Variabl e== "D"] <- runif(length(DF$value[DF$Variable == "A"]), 
          min = 100, max = 500)
      DF$value[DF$Variable == "E"] <- runif(length(DF$value[DF$Variable=="A"]),
          min = 100, max = 500)
    

    接下来,我导入了这些库来创建图形

      # We  now import the libraries
    
       library(readxl)
       require(plotly)
       require(reshape2)
       require(ggplot2)
       require(janitor)
    
       # We now create a chart
    
       p1 <- ggplot(data = DF, aes(x = Column1, y = value, colour = 
           Variable)) +
       geom_bar(data = DF[DF$Variable == "A",], aes(x = Column1, y = value,
           fill=Variable), stat = 'identity') + 
       scale_y_continuous("New", sec.axis = sec_axis(~./10, name = "Value"), 
           position = "left") +
       geom_line(data = DF[DF$Variable!="A",], aes(x = Column1, y = value, 
          fill = Variable))
       p1
    

    代码正常工作。但是,我无法在同一个图表中显示沿条形图创建的折线图。我试过调整天平,但没能使它工作。 我能要求一些指导吗。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Jordo82    7 年前

    你就快到了,只要把fill参数改成group。

    ggplot(data = DF, aes(x = Column1, y = value, colour = 
                            Variable)) +
      geom_bar(data = DF[DF$Variable == "A",], aes(x = Column1, y = value,
                                                   fill=Variable), stat = 'identity') + 
      scale_y_continuous("New", sec.axis = sec_axis(~./10, name = "Value"), 
                         position = "left") +
      geom_line(data = DF[DF$Variable!="A",], aes(x = Column1, y = value, group = Variable))
    

    虽然我不能说它看起来很漂亮。。。

    ( messy plot )

        2
  •  2
  •   Rui Barradas    7 年前

    下面的内容可能会满足您的需要。

    set.seed(4821)    # Make the code reproducible
    
    Column1 <- paste0('Month_', 1:30)
    Variable <- c("A", "B", "C", "D", 'E')
    DF <- data.frame(Column1, Variable)
    nA <- sum(DF$Variable == "A")
    DF$value <- 0
    DF$value[DF$Variable == "A"] <- runif(nA, min = 10000, max = 50000)
    DF$value[DF$Variable == "B"] <- runif(nA, min = 100, max = 500)
    DF$value[DF$Variable == "C"] <- runif(nA, min = 100, max = 500)
    DF$value[DF$Variable == "D"] <- runif(nA, min = 100, max = 500)
    DF$value[DF$Variable == "E"] <- runif(nA, min = 100, max = 500)
    

    现在是图表。请注意,我已将次y轴的比例从 10 到 100 .

    library(ggplot2)
    
    DFA <- subset(DF, Variable == "A")
    DFX <- subset(DF, Variable != "A")
    
    scale_y <- 100
    
    p1 <- ggplot(DF) +
      geom_bar(data = DFA, aes(x = as.integer(sub("Month_", "", Column1)), y = value, fill = Variable), stat = 'identity') +
      geom_line(data = DFX, aes(x = as.integer(sub("Month_", "", Column1)), y = scale_y*value, colour = Variable)) +
      scale_y_continuous("New", sec.axis = sec_axis(~./scale_y, name = "Value"), position = "left") +
      scale_x_discrete("Column1", labels = as.integer(sub("Month_", "", Column1)))
    p1
    

    enter image description here