簡體   English   中英

R:在ggplot2中設置圖例和顏色

[英]R: Setting legends and colours in ggplot2

在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 這是我的輸出。

在此處輸入圖片說明

這是我遇到的問題:

  1. 傳說

顯然,現在我有多余而奇怪的傳說。 我要擁有的一個圖例顯示的條形為“總體積”,另一個圖例顯示的是每個點/線都表示“自動檢測”,“自動檢測”,“自動檢測”,“自動檢測”指揮家ou par l'exploitant”,“RémontéePOD”。

  1. 色彩

該圖的顏色確實很糟糕。 我設置顏色變量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")

你可以試試

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))) 

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM