簡體   English   中英

geom_bars下面的重要字母

[英]Significant letters beneath geom_bars

這是iris數據集的條形圖。 每個條上方的字母表示從kruskal-wallis測試獲得的顯着差異。 在此處輸入圖片說明 盡管在此示例圖上它們不太混亂,但我的真實數據具有更大的誤差線和長度差異。 這樣,字母無處不在,很難閱讀。 我想知道是否可以將字母放在每個條形圖的正下方,正好位於x軸的上方。 這樣,它們將全部對齊並易於閱讀。 你算什么?

碼:

library(reshape2)
library(ggplot2)
library(agricolae)
library(Rmisc)

file<-iris

melt <- melt(file, id=c("Species"))

x1 <- summarySE(melt, measurevar = "value", groupvars = c("variable", "Species"), na.rm=TRUE)

d=list()
tmp=list()

for(i in 1:4){
  if(var(file[,i]) > 0){
    tmp<-c(tmp,colnames(file[i]))
    krusk <- kruskal(file[,i],file[,5],group=TRUE)
    krusk$groups<-krusk$groups[order(krusk$groups[,'trt']),]
    d[[i]]<-as.data.frame(krusk$groups)       
  }
} 

big_data=do.call(rbind,d)

plot<- ggplot(x1, aes(x = variable, y = value, fill = Species)) + 
  coord_flip()+
  geom_bar(stat = "identity", position =position_dodge(),colour="black",width=.7,size=.5)+ 
  geom_errorbar(aes(ymin=value-se, ymax=value+se), width=.1,size=.5,position=position_dodge(.7))+
  theme(
    axis.text = element_text(angle=0, vjust=1,size=8,face="bold"),legend.title=element_blank(),legend.position="bottom",
    legend.text=element_text(face="italic"))+
  labs(title=NULL,x=NULL,y=NULL)+
  geom_text(aes(label=big_data$M,colour=Species),position=position_dodge(width=1),vjust=.8,hjust=-1,size=3)

plot

像這樣:
對於geom_text ,將y = 0設置y = 0 hjust = 1.5
請注意,條形圖,誤差線和文本的閃避寬度是相同的。 還要注意,條的寬度等於躲避寬度,因此,條在每個變量中彼此對接。

plot <- ggplot(x1, aes(x = variable, y = value, fill = Species)) + 
  coord_flip() +
  geom_bar(stat = "identity", position = position_dodge(width = .7),
     colour = "black", width = .7, size = .5) + 
  geom_errorbar(aes(ymin = value-se, ymax = value+se), position=position_dodge(width = .7),
     width = .1, size = .5) +
  geom_text(aes(y = 0, label = big_data$M, colour = Species), 
     position=position_dodge(width = .7), hjust = 1.5, size = 3) +
  theme(
    axis.text = element_text(angle=0, vjust=1,size=8,face="bold"),legend.title=element_blank(),legend.position="bottom",
    legend.text=element_text(face="italic")) +
  labs(title=NULL,x=NULL,y=NULL)

plot

暫無
暫無

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

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