right-justifying the geom_text labels

1,689 views
Skip to first unread message

pradip...@samhsa.hhs.gov

unread,
Feb 5, 2013, 2:36:06 PM2/5/13
to ggp...@googlegroups.com
Hello,
 
Using the reproducible example below, I have drawn forest plots.  I am using large font sizes  since my goal is to use the plot for visualization by inserting  the plots (.png file from the output) into a PowePoint slide.
 
There are two issues with this plot: 1) The text on the plot (OR and confiedence interval)  is not right-justified, and 2) There still remains unused space on left and right margins.  Your advice on how to right-justify the text and how to increase the plot area (by minimizing the left and right margins) would be greratly appreciated.
 
Thanks and regards,
 
Pradip Muhuri
 
###### reproducible example  begins here
setwd ("E:/CSAP_P1/data")
df.alc <- read.csv(text=
"id,state,or,lb,ub, rp_factor
1,Total,2.66,2.54,2.78,pp
2,California,2.59,2.18,3.07,pp
3,Florida,2.71,2.32,3.16,pp
4,Illinois,2.35,1.99,2.77,pp
5,Michigan,3.08,2.67,3.56,pp
6,New York,2.68,2.23,3.21,pp
7,Ohio,2.52,2.16,2.94,pp
8,Pennsylvania,2.95,2.52,3.46,pp
9,Texas,2.43,2.05,2.87,pp
10,Total,1.94,1.85,2.04,ya
11,California,1.89,1.57,2.27,ya
12,Florida,1.96,1.65,2.33,ya
13,Illinois,1.92,1.64,2.25,ya
14,Michigan,2.02,1.72,2.37,ya
15,New York,2.22,1.85,2.67,ya
16,Ohio,2.06,1.75,2.42,ya
17,Pennsylvania,2.18,1.84,2.57,ya
18,Texas,1.68, 1.40,2.02,ya"
, header=TRUE, 
 colClasses =c("character", "character", "numeric", "numeric", "numeric", "character"),
 as.is=TRUE)
df.alc
str (df.alc)
#age <- factor( age,levels=c (1,2,3),labels=c('12-17','18-25','26+'))
df.alc<-within(df.alc,{
                        rp_factor <- factor(rp_factor,
                                levels=c("pp","ya"),
                                labels=c("Risk Factor:\nHigh Perceived Prevalence\nAlc Use by Classmates",
                                    "Risk Factor:\nLack of Peer Disapproval\nYouth Using Alc Daily")
                                           )
                        state <- factor(state,
                                levels=c("Texas", "Pennsylvania", "Ohio", "New York",
                                          "Michigan","Illinois", "Florida",  "California", "Total")
                                        )                   
                      }
                )
df.alc

require(ggplot2)
loadedNamespaces()
require(grid)
p1 <- ggplot(df.alc, aes(x=state,y=or, ymin=lb, ymax=ub))+
      geom_pointrange(linetype = 1,size=1.0, colour = "navyblue")+
      geom_text( aes ( label=(paste( sprintf('%.2f', or)," ",
                                "(", sprintf('%.2f', lb),",",
                                 sprintf('%.2f', ub), ")", sep="" )) ),
             hjust = -.5, colour="black",face="bold", size=7)+
  geom_hline(aes(yintercept=1), size=1.0, colour="white", lty=0) +
  facet_wrap(~ rp_factor)+
  coord_flip()+
  ylim(1,4.0)+
  scale_fill_grey()+ theme_bw()+
  xlab ("  ")+
  ylab("  ")+
 
 
  theme(text = element_text(size = 36, face = "bold"),
        plot.background = element_rect (),
        axis.text.x = element_text(vjust = 1.5, face="bold"),
        axis.text.y = element_text(hjust = 1, face="bold"),
        axis.title.y = element_text(hjust=1, size = rel(0.4), face="bold"),
        axis.title.x = element_text(vjust=1, face = "bold"),
        strip.text.x = element_text (size=24, face="bold"),
        legend.text = element_text(size = rel(0.4), hjust = 0),
        plot.margin = unit(c(1, 1, 1, 1), "lines"),
        legend.position = "none"
  )
print(p1)
setwd ("E:/CSAP_P1/output")
ggsave(file='forest_plots_alc_by_factor.png', width=14, height=10)
 

Allan Just

unread,
Feb 6, 2013, 11:55:16 AM2/6/13
to ggp...@googlegroups.com
Thanks for the example code. Since you are using coord_flip, you can right justify your geom_text by overwriting the y aes for odds ratio with y = Inf and setting hjust = 1. To decrease the right and left margins you can decrease the 2nd and 4th positions in your specification of plot.margin. Here is just the portion of your code generating the plot with these changes:


p1 <- ggplot(df.alc, aes(x=state,y=or, ymin=lb, ymax=ub))+
      geom_pointrange(linetype = 1,size=1.0, colour = "navyblue")+
      geom_text( aes (y = Inf, label=(paste( sprintf('%.2f', or)," ",

                                "(", sprintf('%.2f', lb),",",
                                 sprintf('%.2f', ub), ")", sep="" )) ),
             hjust = 1, colour="black",face="bold", size=7)+

  geom_hline(aes(yintercept=1), size=1.0, colour="white", lty=0) +
  facet_wrap(~ rp_factor)+
  coord_flip()+
  ylim(1,4.0)+
  scale_fill_grey()+ theme_bw()+
  xlab ("  ")+
  ylab("  ")+
  theme(text = element_text(size = 36, face = "bold"),
        plot.background = element_rect (),
        axis.text.x = element_text(vjust = 1.5, face="bold"),
        axis.text.y = element_text(hjust = 1, face="bold"),
        axis.title.y = element_text(hjust=1, size = rel(0.4), face="bold"),
        axis.title.x = element_text(vjust=1, face = "bold"),
        strip.text.x = element_text (size=24, face="bold"),
        legend.text = element_text(size = rel(0.4), hjust = 0),
        plot.margin = unit(c(1, 0, 1, 0), "lines"),

        legend.position = "none"
  )
print(p1)


# - Allan
# R 2.15.1, x86_64, ggplot2_0.9.3
Reply all
Reply to author
Forward
0 new messages