geom_text () and formatC()

1,420 views
Skip to first unread message

pradip...@samhsa.hhs.gov

unread,
Jan 25, 2013, 2:37:18 PM1/25/13
to ggp...@googlegroups.com, pradip...@samhsa.hhs.gov
Hello,

Using the reproducible example appended below, I have drawn a bar chart.  But, the issues are as follows:

1) I am not getting the desired effect of the following on the chart (i.e., the font is not large enough, not black colored, not bold).

geom_text (aes(label=est, size=20, hjust=-.1, colour="black",face="bold")) +

2) I could not figure out how to modify the code so that the label is printed on the chart, retaining one position after the decimal like 67.0 rather than 67.  I have tried formatC(), which gives me the desired result but messes up the y.axis.text. Is there an alternative to formatC(), which I could use for printing one place after the decimal?

Any suggestions toward resolving these issues would be appreciated.

regards, and thanks in advance.

Pradip Muhuri

#### reproducible example is given below - both code and data

setwd ("E:/")
data <- read.csv(text =
"Fairly/Very Easy to Obtain Mrj for Youth,51.4,0.17
Low Level of Parental Monitoring,13.7,0.12
Parents  Not Strongly Disapprv Mon Mrj Use,7.2,0.09
Parents Not Strongly Disapprv Daily Alc Use,10.8,0.11
10+ Conflicts with Parents Past 12 M,23.4,0.15
High Level of Antisocial Behavior,17.3,0.13
Y Not Strongly Disapprv Peer Mon Mrj Use,33.8,0.17
Y Not Strongly Disapprv Peer D Alc Use,32.4,0.16
Friends Not Strongly Disapprv Y Mon Mrj Use,31.6,0.16
Friends Not Strongly Disapprv Y Daily Alc Use,35.5,0.17
Less than Great Risk of Y Mon Mrj Use,65.8,0.17
Less than Great Risk of Y Wkly Mrj Use,46.0,0.18
Less than Great Risk of Y 5+ Alc 1 or 2 Times a Wk,61.1,0.17
Less than Great Risk of Youth 4-5 Alc Drinks Daily,36.4,0.17
High Level of Risk-Taking Proclivity,11.7,0.10
Moved 2+ Times in the Past 5 Years,24.8,0.18
Most/All Students in Same Grade ... Use Mrj,24.2,0.16
Most/All Students in Same Grade ... Use Alc,44.7,0.18
Most /All Students Get Intox Alc Wkly,18.6,0.14
GPA D or Below in Last Grading Period,6.5,0.09"
, sep=",", dec=".", colClasses = c("factor", "numeric", "numeric") )

names (data) <- c ("rp_rev_f", "est", "se" )

head (data)

 
library("ggplot2")
loadedNamespaces()

p <- ggplot( data, aes(x=reorder(rp_rev_f,est), y=est, fill="#990000") )+
     geom_bar(stat="identity", width=.50) +
     geom_text (aes(label=est, size=20, hjust=-.1, colour="black",face="bold")) +
     scale_fill_manual(values="#990000")+
     ylim(0,90)+
     xlab("")+
     ylab(" ")+
     coord_flip ()+
     theme(text = element_text(size=25,face = "bold"),
        axis.text.x = element_text(hjust=1, face="bold"),
        axis.text.y = element_text(hjust=1, face="bold"),
        axis.title.y = element_text(hjust=1, face="bold"),
        axis.title.x = element_text( vjust=1, face="bold"),
        legend.position="none" )   
            
print (p)
 
ggsave(file='prevalence_r.png', width=11, height=7)

Winston Chang

unread,
Jan 25, 2013, 2:50:50 PM1/25/13
to pradip...@samhsa.hhs.gov, ggplot2
For the text, in geom_text, make sure to put all the aesthetics that you are _setting_ outside of aes, like this:
  geom_text (aes(label=sprintf('%.1f', est)), size=10, hjust=-.1, colour="black",face="bold")

Note that in geoms, size specifies size in mm, not in points. (For theme elements, size is in points.)

To get the proper number formatting, you can use sprintf:
  sprintf('%.1f', x)

This should do the trick for you:

p <- ggplot( data, aes(x=reorder(rp_rev_f,est), y=est, fill="#990000") )+
     geom_bar(stat="identity", width=.50) +
     geom_text (aes(label=sprintf('%.1f', est)), size=10, hjust=-.1, colour="black",face="bold") +
     scale_fill_manual(values="#990000")+
     ylim(0,90)+
     xlab("")+
     ylab(" ")+
     coord_flip ()+
     theme(text = element_text(size=25,face = "bold"),
        axis.text.x = element_text(hjust=1, face="bold"),
        axis.text.y = element_text(hjust=1, face="bold"),
        axis.title.y = element_text(hjust=1, face="bold"),
        axis.title.x = element_text( vjust=1, face="bold"),
        legend.position="none" )    
             
print (p) 


-Winston



--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
 
 

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Jan 25, 2013, 3:07:00 PM1/25/13
to Winston Chang, ggplot2
Hi Winston,

Excellent - your revision of the code has given me the results, which I wanted.

Many thanks for resolving the issues.

regards,

Pradip Muhuri

From: Winston Chang [winsto...@gmail.com]
Sent: Friday, January 25, 2013 2:50 PM
To: Muhuri, Pradip (SAMHSA/CBHSQ)
Cc: ggplot2
Subject: Re: geom_text () and formatC()

Reply all
Reply to author
Forward
0 new messages