簡體   English   中英

R / ggplot2-facet_grid上的標簽重疊

[英]R/ggplot2 - Overlapping labels on facet_grid

民間,

我正在使用geom_histogram繪制直方圖,我想用平均值標記每個直方圖(在此示例中,我使用均值)。 問題是我在一個構面上繪制了多個直方圖,並且標簽重疊。 這是一個例子:

library(ggplot2)
df <- data.frame (type=rep(1:2, each=1000), subtype=rep(c("a","b"), each=500), value=rnorm(4000, 0,1))
plt <- ggplot(df, aes(x=value, fill=subtype)) + geom_histogram(position="identity", alpha=0.4)
plt <- plt +  facet_grid(. ~ type)
plt + geom_text(aes(label = paste("mean=", mean(value)), colour=subtype, x=-Inf, y=Inf), data = df, size = 4, hjust=-0.1, vjust=2)

結果是:

這是我得到的結果

問題是子類型a和b的標簽重疊。 我想解決這個問題。

我嘗試過這個位置,例如躲閃和堆疊,例如:

plt + geom_text(aes(label = paste("mean=", mean(value)), colour=subtype, x=-Inf, y=Inf), position="stack", data = df, size = 4, hjust=-0.1, vjust=2)

這沒有幫助。 實際上,它發出了有關寬度的警告。

您能幫忙嗎? 謝謝,里亞德

我認為您可以在繪制新數據框之前預先計算平均值。

library(plyr)
df.text<-ddply(df,.(type,subtype),summarise,mean.value=mean(value))

df.text
  type subtype   mean.value
1    1       a -0.003138127
2    1       b  0.023252169
3    2       a  0.030831337
4    2       b -0.059001888

然后在geom_text()使用此新數據框。 為確保值不重疊,您可以在vjust=提供兩個值(因為每個方面都有兩個值)。

ggplot(df, aes(x=value, fill=subtype)) + 
  geom_histogram(position="identity", alpha=0.4)+
  facet_grid(. ~ type)+
  geom_text(data=df.text,aes(label=paste("mean=",mean.value),
                 colour=subtype,x=-Inf,y=Inf), size = 4, hjust=-0.1, vjust=c(2,4))

在此處輸入圖片說明

只是為了擴展@Didzis:

您實際上在這里有兩個問題。 首先,文本重疊,但更重要的是,當您在aes(...)使用聚合函數時,如下所示:

geom_text(aes(label = paste("mean=", mean(value)), ...

ggplot不尊重構面(或與此相關的組)中隱含的子集。 因此, mean(value)基於完整數據集,而不管構面或分組如何。 結果,您必須使用輔助表,如@Didzis所示。

順便說一句:

df.text <- aggregate(df$value,by=list(type=df$type,subtype=df$subtype),mean)

使您有能力並且不需要plyr

暫無
暫無

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

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