2017-11-21 3 views
2

ggplot2でスロープグラフを作成する際、データポイントが近くにあるときに多くのラベルが重なることがわかります。重複がある場合、自動的にラベルをずらすようにラベルを変更するにはどうすればよいですか?r:ggplotスロープグラフで重なり合ったラベルを自動的にずらす

library(ggplot2) 
library(scales) 
install.packages("Lock5Data", repos = "http://cran.us.r-project.org") # you might need this 
library(Lock5Data) 
data("NBAStandings1e") 
data("NBAStandings2016") 


colnames(NBAStandings1e)[4] <- "year1" # 2010-2011 
colnames(NBAStandings2016)[4] <- "year2" # 2015-2016 
nba_df <- merge(NBAStandings1e[,c('Team','year1')], NBAStandings2016[,c('Team','year2')]) 
scale <- dim(nba_df)[1] 

a<-nba_df 
p<-ggplot(nba_df) + geom_segment(aes(x=0,xend=scale,y=year1,yend=year2),size=.75) 

# clear junk 
p<-p + theme(panel.background = element_blank()) 
p<-p + theme(panel.grid=element_blank()) 
p<-p + theme(axis.ticks=element_blank()) 
# p<-p + theme(axis.text=element_blank()) 
p<-p + theme(panel.border=element_blank()) 
# p<-p + theme(panel.grid.major = element_line(linetype = "dashed", fill = NA)) 
p<-p + theme(panel.grid.major = element_line(linetype = "dashed",color = "grey80")) 
p<-p + theme(panel.grid.major.x = element_blank()) 
p<-p + theme(axis.text.x = element_blank()) 


# annotate 
p<-p + xlab("") + ylab("Percentage Wins") 
p<-p + xlim((-5),(scale+12)) 
p<-p + geom_text(label="2010-2011 Season", x=0,  y=(1.1*(max(a$year2,a$year1))),hjust= 1.2,size=3) 
p<-p + geom_text(label="2015-2016 Season", x=months,y=(1.1*(max(a$year2,a$year1))),hjust=-0.1,size=3) 
p<-p + geom_text(label=nba_df$Team, y=nba_df$year2, x=rep.int(scale,dim(a)[1]),hjust=-0.2,size=2) 
p<-p + geom_text(label=nba_df$Team, y=nba_df$year1, x=rep.int(0,dim(a)[1]),hjust=1.2,size=2) 
p 
+1

'ggrepel :: geom_text_repel'はすごいです、ジッタではなく、ずらしてはいけませんが、重なり合っていないラベルレイアウト – Nate

+0

ありがとう、まずはその機能について聞いたことがあります。私のプログラムの文脈でそれをどのように使うのですか? –

答えて

4

重複するチームの勝率が同じであるため、同じ勝率のチームのラベルを組み合わせることで、より簡単に重複を処理できます。また、プロセスを合理化するためにコードにいくつかの変更を加えました。ここで

library(Lock5Data) 
library(tidyverse) 
library(scales) 

data("NBAStandings1e") 
data("NBAStandings2016") 
colnames(NBAStandings1e)[4] <- "2010-11" # 2010-2011 
colnames(NBAStandings2016)[4] <- "2015-16" # 2015-2016 
nba_df <- merge(NBAStandings1e[,c('Team','2010-11')], NBAStandings2016[,c('Team','2015-16')]) 

# Convert data to long format 
dat = gather(nba_df, Season, value, -Team) 

# Combine labels for teams with same winning percentage (see footnote * below) 
dat_lab = dat %>% group_by(Season, value) %>% 
    summarise(Team = paste(Team, collapse="\U2014")) # \U2014 is the emdash character 

ggplot(dat, aes(Season, value, group=Team)) + 
    geom_line() + 
    theme_minimal() + theme(panel.grid.minor=element_blank()) + 
    labs(y="Winning Percentage") + 
    scale_y_continuous(limits=c(0,1), labels=percent) + 
    geom_text(data=subset(dat_lab, Season=="2010-11"), aes(label=Team, x=0.98), hjust=1, size=2) + 
    geom_text(data=subset(dat_lab, Season=="2015-16"), aes(label=Team, x=2.02), hjust=0, size=2) 

enter image description here

は、ラベルがどのように見えるかのクローズアップです:

enter image description here


*非常に近いが、等しくない有することに起因するオーバーラップするチームがある場合は、あなたが勝つパーセンテージであっても、丸めによってそれらをグループ化することができます。たとえば、あなたが最も近い2パーセントに丸めたときに、あなたができる同じ勝率で、グループのチームに望んだ場合:

dat_lab = dat %>% group_by(Season, group=round(value/0.02)*0.02) %>% 
    summarise(Team = paste(Team, collapse="\U2014"), 
      value = mean(value)) 

これは彼らのグループの平均valueに配置されているラベルにつながります。

関連する問題