R / ggplot2 - 在facet_grid上重叠标签 [英] R/ggplot2 - Overlapping labels on facet_grid
问题描述
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)),color = subtype,x = -Inf,y = Inf),data = df,size = 4,hjust = -0.1,vjust = 2)
结果是:
问题是子类型a和b重叠。我想解决这个问题。
我试过这个位置,包括闪避和堆叠,例如:
<$ (aes(label = paste(mean =,mean(value)),color = subtype,x = -Inf,y = Inf),position =stack ,data = df,size = 4,hjust = -0.1,vjust = 2)
帮帮我。事实上,它发出了关于宽度的警告。
你愿意帮忙吗?
Thx,
Riad。
我认为您可以在绘制新数据框之前预先计算平均值。
library(plyr)
df.text< -ddply(df,。(type,subtype),summarize ,mean.value =平均(价值))
df.text
类型子类型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),
color = subtype,x = -Inf,y = Inf),size = 4,hjust = -0.1,vjust = c(2,4))
Folks,
I am plotting histograms using geom_histogram and I would like to label each histogram with the mean value (I am using mean for the sake of this example). The issue is that I am drawing multiple histograms in one facet and I get labels overlapping. This is an example:
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)
Result is:
The problem is that the labels for Subtypes a and b are overlapping. I would like to solve this.
I have tried the position, both dodge and stack, for example:
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)
This did not help. In fact, it issued warning about the width.
Would you pls help ? Thx, Riad.
I think you could precalculate mean values before plotting in new data frame.
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
Then use this new data frame in geom_text()
. To ensure that values do not overlap you can provide two values in vjust=
(as there are two values in each facet).
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))
这篇关于R / ggplot2 - 在facet_grid上重叠标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!