如何在较大范围内缩放变量并在ggplot中的单个图中显示它们

时间:2018-10-19 11:50:29

标签: r ggplot2 plot

我创建了以下数据框

 # 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 个答案:

答案 0 :(得分:2)

类似以下的内容可能会满足您的要求。

首先,我将重新制作数据集,这次将设置RNG种子。

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

答案 1 :(得分:1)

您快到了,只需将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