ggarrange的常见传说

时间:2019-06-28 12:54:05

标签: r ggplot2 plot

图例并没有包含所有颜色

Legend doesn't contain all colors in graph

我做了一个普通的图例,但我意识到该图例只显示某些颜色或变量,我如何才能显示每种颜色? 这是我的ggplot函数

ggheat <- function(df)
{

  df1 <- df %>%  # spread table by name 
    select(name, value, DATEANDTIME) %>% 
    unique() %>% 
    spread(name, value) %>% 
    StatutCrypt() # change values name
  Code <- df1 %>% select(- DATEANDTIME) %>% names() # df1 variables names 
  df1 <- df1 %>% 
    mutate(val = 1) %>% 
    spread(key = Code, value = val)

  Levels <- df1 %>% select(- DATEANDTIME) %>% names() 
  df1[is.na(df1)] <- 0

  df2 <- df1 %>% 
    melt(id.vars = "DATEANDTIME")

  df2$variable <- factor(df2$variable, levels = Levels)


  ames_colours <- c("#00a90a", "#af6600", "#b58f00", "#ff0000", "#bbb90f", "#c1e368", "#bf0000")

  df2$colours <- df2$variable %>% as.numeric() %>% ames_colours[.]

  status_areaStep <- bind_rows(old = df2, 
                               new = df2 %>% mutate(value = lag(value)),
                               .id = "source") %>%
    arrange(DATEANDTIME, source)

  g <- ggplot() +
    # thm +
    # facet_grid(variable ~ .) +
    # theme(plot.margin = unit(c(10, 1, 10, 1), "cm")) +
    geom_ribbon(data = status_areaStep, aes(x = DATEANDTIME, ymin = 0, ymax = value, fill = variable)) +
    scale_fill_manual(values = ames_colours, drop = F) +
    scale_colour_manual(values = ames_colours, drop = F) +
    # scale_colour_manual(values=c("Prod" = "#00a90a", "Tool_change" = "#af6600","Serie_change" = "#b58f00",
    #                             "Breakdown" = "#ff0000","Starved" = "#bbb90f","Blocked" = "#c1e368","STOP" = "#bf0000",
    #                             "0" = "#080505")) +
    # scale_fill_manual(breaks = c("Prod", "Tool_change", "Serie_change", "Breakdown", "Starved", "Blocked", "STOP","0" ),
    #                   values=c("#00a90a", "#af6600", "#b58f00", "#ff0000", "#bbb90f", "#c1e368", "#bf0000", "#080505" ), drop = F) +
    theme(axis.title.y = element_blank(),
          axis.text.y = element_blank(),
          axis.ticks.y = element_blank(),
          axis.title.x = element_blank(),
          axis.text.x = element_blank(),
          axis.ticks.x = element_blank(),
          legend.title=element_blank())
  return(g)
}

映射ggplot列表

gglist <- map(listoftables, ggheat)
ggarrange(plotlist = gglist, ncol=1, common.legend = T ,legend="bottom", labels = gglabels, font.label = list(size = 8))

0 个答案:

没有答案