我想知道如何用ggplot2中的directlabels标记所有点

时间:2015-09-27 17:33:29

标签: r ggplot2 direct-labels

我想将 direct.labels 的标签添加到我的Gower残差图中(用于适用性分析)。但是,我发现 direct.labels 函数只会向某些点添加标签,而不是向所有点添加标签。我想知道是否有一个功能可以使用 direct.lables 这样做,或者有人可以推荐我一个更好的方法来做到这一点。

谢谢

以下是我正在使用的代码

#Loading the data frame "R" to be used with ggplot
R <- read.csv(header=T, text='
Sector,Group,Type,Value
SBF,1,Residual,0.03
SBF,4,Residual,-0.06
SBF,5,Residual,-0.02
SBF,6,Residual,0.07
SDF,1,Residual,0.13
SDF,4,Residual,-0.08
SDF,5,Residual,0.08
SDF,6,Residual,0.03
SPF,1,Residual,0.04
SPF,4,Residual,0.16
SPF,5,Residual,-0.11
SPF,6,Residual,-0.08
SOF,1,Residual,0
SOF,4,Residual,0.16
SOF,5,Residual,0.32
SOF,6,Residual,-0.48
SSF,1,Residual,0
SSF,4,Residual,-0.06
SSF,5,Residual,0.01
SSF,6,Residual,0.2
SCO,1,Residual,-0.21
SCO,4,Residual,-0.06
SCO,5,Residual,0.07
SCO,6,Residual,0.2
SMA,1,Residual,0.28
SMA,4,Residual,-0.04
SMA,5,Residual,-0.17
SMA,6,Residual,-0.07
SNT,1,Residual,-0.11
SNT,4,Residual,-0.01
SNT,5,Residual,0
SNT,6,Residual,0.13
')
## Converting integers into factors so i can make groups of sectors 
R$Group<-factor(R$Group)
install.packages("directlabels")
library(directlabels)
## Creating p for the direct.labels function using ggplot
p<-ggplot(data=R, aes(x=Group, y=Value, group=Sector, colour=Sector))+
geom_text(aes(label=format(Sector),
            size=3,
            hjust=-0.2,
            vjust=-0.05),
        show_guide=FALSE)+
geom_segment(aes(xend=Group),yend=0,colour="grey50",linetype="solid")+
geom_hline(aes(yintercept=0), linetype="solid")+
geom_point(size=3)+
scale_x_discrete(labels=c(
"1"="Group 1",
"2"="Group 2",
"3"="Group 3",
"4"="Group 4",
"5"="Group 5",
"6"="Group 6"), 
expand=c(0.13,0.05))+
scale_y_continuous()+
theme_bw()+
theme(panel.grid.minor=element_blank(), 
    panel.grid.major=element_blank(),
    axis.title.x=element_blank(),
    axis.title.y=element_blank(),
    axis.text.x=element_text(size=13,
                             face="bold", 
                             hjust=0.5),
    axis.text.y=element_text(size=10,
                             face="bold", 
                             hjust=0.5))
direct.label(p,dl.combine(first.qp,last.qp))

0 个答案:

没有答案