본문 바로가기
R

(R) Add label to bar graphs / geom_text(aes(label=))

by jangpiano 2020. 8. 16.
반응형

<Add label to bar graphs - geom_text(aes(label=))>


> airquality2<-airquality%>%group_by(Month)%>%summarise(mean_temp=mean(Temp))


> airquality2

# A tibble: 5 x 2

  Month mean_temp

  <int>     <dbl>

1     5      65.5

2     6      79.1

3     7      83.9

4     8      84.0

5     9      76.9


<The way to add labels to each bin - geom_text(aes(label=y))>


> ggplot(data=airquality2,aes(x=Month,y=mean_temp))+geom_bar(stat="identity")+geom_text(aes(label=mean_temp))

<adjust the label placement - geom_text(aes(label=y),vjust=)>


> ggplot(data=airquality2,aes(x=Month,y=mean_temp))+geom_bar(stat="identity")+geom_text(aes(label=mean_temp),vjust=1.5)

> ggplot(data=airquality2,aes(x=Month,y=mean_temp))+geom_bar(stat="identity")+geom_text(aes(label=mean_temp),vjust=0.1)

<adjust the label color - geom_text(aes(label=y),color="")>


> ggplot(data=airquality2,aes(x=Month,y=mean_temp))+geom_bar(stat="identity")+geom_text(aes(label=mean_temp),vjust=1.5,color="blue")


> ggplot(data=airquality2,aes(x=Month,y=mean_temp))+geom_bar(stat="identity")+geom_text(aes(label=mean_temp),vjust=0.1,color="blue")

> ggplot(data=airquality2,aes(x=Month,y=mean_temp))+geom_bar(stat="identity")+geom_text(aes(label=mean_temp),vjust=-0.2,color="blue")

<adjust the size of the label - geom_text(aes(label=y),size=)>


the font size of label is basically set as 0.5. 

If you want to make the size smaller than the original graph, you can adjust the size of the label by setting smaller than 0.5


> ggplot(data=airquality2,aes(x=Month,y=mean_temp))+geom_bar(stat="identity")+geom_text(aes(label=mean_temp),vjust=-0.2,color="blue",size=2)

> ggplot(data=airquality2,aes(x=Month,y=mean_temp))+geom_bar(stat="identity")+geom_text(aes(label=mean_temp),vjust=-0.2,color="blue",size=3.5)



> airquality_<-airquality%>%filter(!is.na(Ozone))%>%mutate(Ozone_rank=ifelse(Ozone<=31.50,"little",ifelse(Ozone<=63.25,"middle","much")))%>%mutate(temp_rank=ifelse(Temp<=72,"warm",ifelse(Temp<=85,"hot","very hot")))


> Ozone_temp<-airquality_%>%group_by(Ozone_rank)%>%summarise(mean_temp=mean(Temp))

`summarise()` ungrouping output (override with `.groups` argument)


> Ozone_temp_rate<-airquality_%>%count(Ozone_rank,temp_rank)%>%group_by(Ozone_rank)%>%mutate(per=n/sum(n)*100)


> ggplot(data=Ozone_temp_rate,aes(x=Ozone_rank,y=per,fill=temp_rank))+geom_bar(position="dodge",stat="identity")+geom_text(aes(label=per),vjust=1.5,position=position_dodge(0.9),size=3)


<Add label to stacked bar graph> 


> airquality_<-airquality%>%filter(!is.na(Ozone))%>%mutate(Ozone_rank=ifelse(Ozone<=31.50,"little",ifelse(Ozone<=63.25,"middle","much")))%>%mutate(temp_rank=ifelse(Temp<=72,"warm",ifelse(Temp<=85,"hot","very hot")))

> Ozone_temp<-airquality_%>%group_by(Ozone_rank)%>%summarise(mean_temp=mean(Temp))

`summarise()` ungrouping output (override with `.groups` argument)

> Ozone_temp_rate<-airquality_%>%count(Ozone_rank,temp_rank)%>%group_by(Ozone_rank)%>%mutate(per=n/sum(n)*100)


> Ozone_temp_rate

# A tibble: 7 x 4

# Groups:   Ozone_rank [3]

  Ozone_rank temp_rank     n   per

  <chr>      <chr>     <int> <dbl>

1 little     hot          30  51.7

2 little     warm         28  48.3

3 middle     hot          19  65.5

4 middle     very hot      5  17.2

5 middle     warm          5  17.2

6 much       hot           7  24.1

7 much       very hot     22  75.9



> ggplot(data=Ozone_temp_rate,aes(x=Ozone_rank,y=per,fill=temp_rank))+geom_bar(stat="identity")+geom_text(aes(label=per), position=position_stack())





> mpg2<-mpg%>%filter(manufacturer==c("audi","hyundai"))

> ggplot(data=mpg2,aes(x=manufacturer,fill=class))+geom_bar()



> ggplot(data=mpg2,aes(x=manufacturer,fill=class))+geom_bar()+geom_text(aes(label=..count..),stat="count",position=position_stack())



> ggplot(data=mpg2,aes(x=manufacturer,fill=class))+geom_bar()+geom_text(aes(label=..count..),stat="count",col="blue",vjust=1.5,position=position_stack())


> ggplot(data=mpg2,aes(x=manufacturer,fill=class))+geom_bar()+geom_text(aes(label=..count..),stat="count",col="blue",vjust=-0.2,position=position_stack())










반응형