2017-08-15 189 views
0

我嘗試使用ggplot2分別使用fillalpha對barplot中的兩個離散變量進行可視化。這樣做的標準方法如下:r ggplot2:在條形圖中對圖例進行分組

#creating data and building the basic bar plot 
library(ggplot2) 
myleg<-read.csv(text="lett,num 
a,1 
a,2 
b,1 
b,2 
h,1 
h,2 
h,3 
h,4") 
ggplot(myleg,aes(lett,alpha=factor(num),fill=lett)) +geom_bar(position=position_stack(reverse=T)) +scale_alpha_discrete(range=c(1,.1), name="alpha legend",labels=c("alpha lab 4","alpha lab 3","alpha lab 2", "alpha lab 1")) +labs(title="initial bar plot for data") 

initial bar plot of data

默認圖例是根據呈現的兩種不同的方式進行分組(着色爲快報和灰度,或不透明度爲NUM)。

我需要將圖例分組爲數據條。即三個色帶,每個色帶具有不斷變化的α級別。部分解決方案是,分別生成與期望的3圖例條情節如下:

ggplot(myleg,aes(lett,alpha=factor(num),fill=lett)) +geom_bar(position="stack",fill="#f8766d") +scale_alpha_discrete(name="red legend",labels=c("red lab 2","red lab 1"),breaks=c("3","4")) 
ggplot(myleg,aes(lett,alpha=factor(num),fill=lett)) +geom_bar(position="stack",fill="#00ba38") +scale_alpha_discrete(name="green legend",labels=c("green lab 2","green lab 1"),breaks=c("3","4")) 
ggplot(myleg,aes(lett,alpha=factor(num),fill=lett)) +geom_bar(position="stack",fill="#619cff") +scale_alpha_discrete(name="blue legend",labels=c("blue lab 4","blue lab 3","blue lab 2", "blue lab 1")) 

輸出: artificially created plots with needed legend strips

所以現在我只能切割和三個圖例條粘貼到主圖形,例如,在Inkscape中,以產生期望的結果:

final plot with desired legend

它是如何可能在一個體面的方式來編程嗎?

+0

馬可的回答是「正確」的方式做到這一點,但對於特別繁瑣的數字,在某些時候你在Inkscape中做這件事的方式是更好,更加個性化的方式。 – Brian

+0

我編輯了我的答案,爲@starsk發佈的新問題提出瞭解決方案 –

回答

4

可以繪製使用scale_fill_manual定製的傳說:

df <- mtcars 
df$cyl <- factor(df$cyl) 
df$vs <- factor(df$vs) 

library(ggplot2)  
p <- ggplot(df,aes(x=cyl, fill=interaction(cyl,vs))) + geom_bar(position="stack") 

# Breaks 
brks <- levels(interaction(df$cyl,df$vs)) 

# Values - Colors 
library(scales) 
pal <- hue_pal()(3) 
cls <- as.character(c(sapply(pal,alpha,0.3),sapply(pal,alpha,1))) 

# Labels 
lbls <- paste(levels(df$cyl), "-", rep(levels(df$vs),each=3)) 

p + scale_fill_manual(name ='Cyl - Vs', breaks=brks, values=cls, labels=lbls) 

enter image description here

編輯這裏被張貼@astrsk的(新的)問題的解決方案(他/她的變化後,最初的問題)。下面

library(ggplot2) 
library(grid) 
library(gridExtra) 
myleg <- structure(list(lett = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 3L, 
3L), .Label = c("a", "b", "h"), class = "factor"), num = c(1L, 
2L, 1L, 2L, 1L, 2L, 3L, 4L)), .Names = c("lett", "num"), 
class = "data.frame", row.names = c(NA, -8L)) 

getLegend <- function(p) { 
    g <- ggplotGrob(p) 
    k <- which(g$layout$name=="guide-box") 
    g$grobs[[k]] 
} 

p1 <- ggplot(myleg,aes(lett,alpha=factor(num),fill=lett)) +geom_bar(position="stack",fill="#f8766d") +scale_alpha_discrete(name="red legend",labels=c("red lab 2","red lab 1"),breaks=c("3","4")) 
p2 <- ggplot(myleg,aes(lett,alpha=factor(num),fill=lett)) +geom_bar(position="stack",fill="#00ba38") +scale_alpha_discrete(name="green legend",labels=c("green lab 2","green lab 1"),breaks=c("3","4")) 
p3 <- ggplot(myleg,aes(lett,alpha=factor(num),fill=lett)) +geom_bar(position="stack",fill="#619cff") +scale_alpha_discrete(name="blue legend",labels=c("blue lab 4","blue lab 3","blue lab 2", "blue lab 1")) 

p <- ggplot(myleg,aes(lett,alpha=factor(num),fill=lett)) + 
    geom_bar(position=position_stack(reverse=T)) + 
    scale_alpha_discrete(range=c(1,.1), name="alpha legend", 
     labels=c("alpha lab 4","alpha lab 3","alpha lab 2", "alpha lab 1")) + 
    labs(title="initial bar plot for data") 
g <- ggplotGrob(p) 

k <- which(g$layout$name=="guide-box") 
g$grobs[[k]] <- grid.arrange(getLegend(p1),getLegend(p2),getLegend(p3),ncol=1) 
grid.draw(g) 

enter image description here

+0

我仍然會說你的答案是「正確」的方法:)從gtables中拉出傳說很漂亮! – Brian