R:在ggplot2

时间:2018-05-18 10:04:43

标签: r ggplot2 colors legend

在R中我试图使用ggplot2绘制条形图和线条/点的图形,但是我对图例和颜色设置有困难。

首先,这是我的数据框架。

vol_reshape <- data.frame(date = c("2018-01-01", "2018-02-01", "2018-03-01", "2018-04-01"),
                          variable = rep("total", 4),
                          value = as.integer(c("8029", "8164", "9536", "9482")))

qua_reshape <- data.frame(date = rep(c("2018-01-01", "2018-02-01", "2018-03-01", "2018-04-01"), 4),
                          variable = c(rep("quality_1", 4), rep("quality_2", 4), rep("quality_3", 4), rep("pod", 4)),
                          value = as.double(c("0.26", "0.26",   "0.30", "0.32", "0.27", "0.27", "0.30", "0.32", "0.45", "0.42", "0.51", "0.55", "0.05", "0.04", "0.05", "0.05")))

我想使用vol_reshape绘制条形图,在此条形图上,我想使用qua_reshape绘制点和线。这是我的输出。

enter image description here

以下是我遇到的问题:

  1. 图例
  2. 显然现在我有多余和奇怪的传说。我想要的是一个传说显示该栏是“总音量”,另一个传说显示每个点/线表示“Détectéeautomatique”,“Détectéeautomatiqueoudédéclaréeparle conducteur”,“Détectéeautomatique,déclaréepalele有利于ou par l'exploitant“,”RémontéePOD“。

    1. 颜色
    2. 这个情节的颜色非常糟糕。我设置颜色变量cols,使条形为蓝色,三条线/点(quality_1 ~ 3)为黑色,pod线条/点为橙色。我在scale_fill_manual中设置了这些值,但这并不反映我的愿望。

      如果有人帮我解决这些问题会很棒。

      这是我的尝试。

          p <- ggplot(data = vol_reshape[which(vol_reshape$value > 0),], 
                      aes(x = date, y = value, label = value, 
                          fill = variable
                          )
                      ) +
            geom_bar(stat = "identity", position = "stack", show.legend = T) +   
            theme(legend.title=element_blank()) + 
            geom_text(size = size$label,  position = position_stack(vjust = 0.9), color = "#FFFFFF") +                        
            geom_point(data = qua_reshape, mapping = aes(x = date,
                                                         y = value *max(vol_reshape$value),
                                                         color = quality
                                                         )
                       ) +
            theme(legend.title=element_blank()) + 
      
            geom_line(data = qua_reshape, mapping = aes(x = date,
                                                        y =value*max(vol_reshape$value),
                                                        color = variable),
                      size = size$line) +
            geom_text(data = qua_reshape, mapping = aes(x = date, 
                                                        y =value*max(vol_reshape$value), 
                                                        label =paste0(100*value, '%'),
                                                        color = variable),
                      size = size$label, vjust = -0.9, hjust = 1.5
                      ) +
            theme_classic() +
      
            scale_y_continuous(sec.axis = sec_axis(trans = ~.*(1/max(vol_reshape$value)))) +
            scale_fill_manual(name = NULL, values = cols, labels = labs, drop = T) +
      
            theme(legend.position = "right",
              plot.title = element_text(hjust = 0.5, size = size$title, family="Proxima Nova"),
              plot.subtitle = element_text(size = size$subtitle, family="Proxima Nova"),
              axis.title.x=element_blank(), 
              axis.text.x = element_text(angle = 45, margin=margin(t = 8), 
                                         family="Proxima Nova"))
              )
      
           cols <- c("total" = "#3D8BDA",  "quality_1" = "#000000", "quality_2" ="#000000",  "quality_3" = "#000000",  "pod" = "#ff8142")
           labs <- c("Total", "Détectée automatique", "Détectée automatique ou déclarée par le conducteur", "Détectée automatique, déclarée par le conducteur ou par l'exploitant", "Rémontée POD")
      

1 个答案:

答案 0 :(得分:0)

你可以尝试

ggplot() + 
    geom_col(data=vol_reshape, aes(x=as.POSIXct(date), y=value, fill=factor("Total"))) + 
    geom_line(data=qua_reshape, aes(x=as.POSIXct(date), y=value2, group=variable, color=variable), size=1.5) + 
    scale_fill_manual(name="", values="skyblue1") + 
    scale_color_manual(values = c("orange", rep(1, 3))) 

enter image description here