2017-12-29 33 views
1

用のカスタム凡例を追加するにはどうすれば、次のデータセットを持っている:私はグラフの次「のような弾丸」を作成するために使用することを弾丸チャート

incidents.pct <- data.frame(
    measure=c("Total Events (%)", "Security Events (%)", "Filtered (%)", "Tickets (%)"), 
    high=c(100,100,100,100), 
    mean=c(45,40,50,30), 
    low=c(25,20,10,5), 
    target=c(55,40,45,35), 
    value=c(50,45,60,25)) 

g <- ggplot(incidents.pct) + 
    geom_bar(aes(measure, high), fill="goldenrod2", stat="identity", width=0.5, alpha=0.2) + 
    geom_bar(aes(measure, mean), fill="goldenrod3", stat="identity", width=0.5, alpha=0.2) + 
    geom_bar(aes(measure, low), fill="goldenrod4", stat="identity", width=0.5, alpha=0.2) +   
    geom_point(aes(measure, target), colour="red", size=2.5) 

これは機能しますが、色を説明するカスタム凡例を含めたいと思います。だから、fe "low"、 "medium"、 "value"などのカラーサインだけ。

これを含める方法についてのアドバイスはありますか?

答えて

4

ggplotでは、審美的なオプションのために凡例が自動的に生成されます(内側がaes()の場合)。だから、scale_fill_manual()と次の回避策はあなたに伝説を与える:。

ggplot(incidents.pct) + 
    geom_col(aes(measure, high, fill = "high"), width=0.5, alpha=0.2) + 
    geom_col(aes(measure, mean, fill = "mean"), width=0.5, alpha=0.2) + 
    geom_col(aes(measure, low, fill = "low"), width=0.5, alpha=0.2) +   
    geom_point(aes(measure, target), colour="red", size=2.5) + 
    scale_fill_manual(name = "Legend", 
        values = c("high" = "goldenrod2", 
           "mean" = "goldenrod3", 
           "low" = "goldenrod4"), 
        breaks = c("high", "mean", "low")) 

を(ちなみに、geom_col()は&がすっきり見えます、geom_bar(stat = "identity")と同等です

plot

私は低いアルファ値を使用して警戒うしかし、凡例の色がプロットの色と正確に一致しないため、3つの明るい色合いを選択して、アルファを1のままにしておくときれいになります。

0

プロットする前にデータを再形成することもできます。

library(ggplot2) 

#reshape data for plotting 
library(tidyverse) 
df <- incidents.pct %>% 
    gather(fe, fe_value, -measure, -target, -value) 
df$fe <- factor(df$fe, levels=c("high", "mean", "low")) 

ggplot(df, aes(x=measure, y=fe_value, fill=fe)) + 
    geom_bar(stat="identity", alpha=0.3) + 
    scale_fill_manual(values=c("goldenrod2", "goldenrod3", "goldenrod4")) + 
    geom_point(aes(measure, target), colour="red", size=2.5) + 
    theme_bw() 

出力プロットである: enter image description here

#sample data 
> dput(incidents.pct) 
structure(list(measure = structure(c(4L, 2L, 1L, 3L), .Label = c("Filtered (%)", 
"Security Events (%)", "Tickets (%)", "Total Events (%)"), class = "factor"), 
    high = c(100, 100, 100, 100), mean = c(45, 40, 50, 30), low = c(25, 
    20, 10, 5), target = c(55, 40, 45, 35), value = c(50, 45, 
    60, 25)), .Names = c("measure", "high", "mean", "low", "target", 
"value"), row.names = c(NA, -4L), class = "data.frame") 
0

は、私は少し遅れとZ.Linの答え@だと思え、私はそれを指摘すると思うので、ちょうど良いですが、私は同様に鉱山を投稿ggplot2の素敵な特性、すなわち長いデータの使用。一度データを整理して整理すれば、簡単に印刷できます(例:データを変換するにはもっと多くの行が必要になります;-)):

incidents.pct %>% 
    # ggplot2 likes long data: 
    gather(key = key, value = val, high, mean, low) %>% 
    # give nice order to factors: 
    mutate(key = factor(key, levels = c("high", "mean", "low"))) %>% 
    # arrange and group to take difference such that we can stack the values (sum up to 100): 
    arrange(measure, desc(key)) %>% 
    group_by(measure) %>% 
    mutate(val = val-ifelse(is.na(lag(val)), 0, lag(val))) %>% 
    # plot it, giving aes in ggplot, not necessarily below: 
    ggplot(aes(x = measure, y = val, fill = key)) + 
    # use geom_col if you want to use identity anyway: 
    geom_col(width = .5, alpha = .2) + 
    # just change the y-value in the aes, don't add it to legend: 
    geom_point(aes(y = target), colour = "red", size = 2.5, show.legend = FALSE) + 
    # now define your colours: 
    scale_fill_manual(values = c("goldenrod2", "goldenrod3", "goldenrod4")) 
関連する問題