首页 > 代码库 > text label overlap probelm

text label overlap probelm

常见的解决方法有三种:

1. 使用wordcloud

#install.packages(c("wordcloud","tm"),repos="http://cran.r-project.org")
library(wordcloud)
library(tm)

wordcloud("May our children and our children‘s children to a
thousand generations, continue to enjoy the benefits conferred
upon us by a united country, and have cause yet to rejoice under
those glorious institutions bequeathed us by Washington and his
compeers.",colors=brewer.pal(6,"Dark2"),random.order=FALSE)

data(SOTU)
SOTU <- tm_map(SOTU,function(x)removeWords(tolower(x),stopwords()))
wordcloud(SOTU, colors=brewer.pal(6,"Dark2"),random.order=FALSE)
 
states <- c(Alabama, Alaska, Arizona, Arkansas,
    California, Colorado, Connecticut, Delaware,
    Florida, Georgia, Hawaii, Idaho, Illinois,
    Indiana, Iowa, Kansas, Kentucky, Louisiana,
    Maine, Maryland, Massachusetts, Michigan, Minnesota,
    Mississippi, Missouri, Montana, Nebraska, Nevada,
    New Hampshire, New Jersey, New Mexico, New York,
    North Carolina, North Dakota, Ohio, Oklahoma, Oregon,
    Pennsylvania, Rhode Island, South Carolina, South Dakota,
    Tennessee, Texas, Utah, Vermont, Virginia, Washington,
    West Virginia, Wisconsin, Wyoming)

loc1 <- rnorm(50,c(0,0),matrix(c(1,.7,.7,1),ncol=2))
loc2 <- rnorm(50,c(0,0),matrix(c(1,.7,.7,1),ncol=2))

plot(loc1,loc2,type="n")
text(loc1,loc2,states) #overlap
textplot(loc[,1],loc[,2],states) #eliminate overlap 

2. 使用direct label:

library(ggplot2)
library(directlabels)
x<-runif(10)
y<-rnorm(10)
z<-as.character(midwest$county[1:10])
q<-qplot(x,y)+geom_point(aes(colour=z))
direct.label(q, first.qp)

 

cited from:

http://drunks-and-lampposts.com/2012/02/22/labelling-a-ggplot-scatterplot/

http://blog.fellstat.com/?cat=11

http://stackoverflow.com/questions/21585128/how-to-avoid-overlapping-labels-with-identical-data-points-in-scatterplot-ggpl