簡體   English   中英

r ggplot2:在條形圖中對圖例進行分組

[英]r ggplot2: grouping legend in bar plots

我嘗試使用ggplot2分別使用fillalpha來可視化barplot中的兩個離散變量。 這樣做的標准方法如下:

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

數據的初始條形圖

默認圖例根據兩種不同的表示方式進行分組(lett和灰度的着色或num的不透明度)。

我需要將圖例分組為情節上的數據欄。 即,三個色帶,每個色帶具有變化的Alpha級別。 部分解決方案是分別用所需的三個圖例條生成圖,如下所示:

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

輸出為: 人工創建的地塊以及所需的圖例條

因此,現在我只能在主圖上剪切並粘貼三個圖例條,例如在inkscape中,以產生所需的結果:

具有所需圖例的最終劇情

如何以體面的方式對此進行編程?

您可以使用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)

在此處輸入圖片說明

編輯這是@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)

在此處輸入圖片說明

暫無
暫無

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

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