본문 바로가기
R

(R) add label to dot plots / annotate("text")/geom_text(aes(label=))

by jangpiano 2020. 9. 1.
반응형


Add label one by one to the dot plot 

                                                    <+annotate("text",x=,y=,label="")


> ggplot(data=fertility_life,aes(x=Lifeexpectancy,y=Fertility))+geom_point()


x: horizontal location of the label "South Korea" 

y: vertical location of the label "South Korea"


> ggplot(data=fertility_life,aes(x=Lifeexpectancy,y=Fertility))+geom_point()+annotate("text",x=83,y=1.1,label="South Korea")

> ggplot(data=fertility_life,aes(x=Lifeexpectancy,y=Fertility))+geom_point()+annotate("text",x=83,y=1.1,label="South Korea")+annotate("text",x=58,y=5.9,label="Somalia")

> ggplot(data=fertility_life,aes(x=Lifeexpectancy,y=Fertility))+geom_point()+annotate("text",x=83,y=1.1,label="South Korea",size=2.5)+annotate("text",x=58,y=5.9,label="Somalia",size=2.5)

> ggplot(data=fertility_life,aes(x=Lifeexpectancy,y=Fertility))+geom_point()+annotate("text",x=83,y=1.1,label="South Korea",vjust=1)+annotate("text",x=58,y=5.9,label="Somalia",vjust=1)

Add label to every dot in dot plots 
                                              - geom_text(aes(label=))
> ggplot(data=fertility_life,aes(x=Lifeexpectancy,y=Fertility))+geom_point()+geom_text(aes(label=Country))

> ggplot(data=fertility_life,aes(x=Lifeexpectancy,y=Fertility))+geom_point()+geom_text(aes(label=Country),size=2.5)

<Ways to move label - geom_text(aes(label=)),vjust=)> 

> ggplot(data=fertility_life,aes(x=Lifeexpectancy,y=Fertility))+geom_point()+geom_text(aes(label=Country),vjust=1)
> ggplot(data=fertility_life,aes(x=Lifeexpectancy,y=Fertility))+geom_point()+geom_text(aes(label=Country),vjust=0)
> ggplot(data=fertility_life,aes(x=Lifeexpectancy,y=Fertility))+geom_point()+geom_text(aes(y=Fertility+0.1,label=Country),vjust=0)
> ggplot(data=fertility_life,aes(x=Lifeexpectancy,y=Fertility))+geom_point()+geom_text(aes(label=Country),hjust=1)

> ggplot(data=fertility_life,aes(x=Lifeexpectancy,y=Fertility))+geom_point()+geom_text(aes(label=Country),hjust=0)

<Add labels to some dots what I want to specify - Make the others NA>

> representative<-fertility_life$Country%in%c("Mexico","USA","Republic of Korea","Zimbabwe","Denmark")
> representative
 [1]  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
[10] FALSE  TRUE  TRUE

> fertility_life$Country[!representative]<-NA
> table(fertility_life$Country)

          Denmark            Mexico Republic of Korea 
                1                 1                 1 
              USA          Zimbabwe 
                1                 1 

> ggplot(data=fertility_life,aes(x=Lifeexpectancy,y=Fertility))+geom_point()+geom_text(aes(label=Country))
> ggplot(data=fertility_life,aes(x=Lifeexpectancy,y=Fertility))+geom_point()+geom_text(aes(label=Country),vjust=1,size=2.5)







반응형