geom_text and axis.y.title

345 views
Skip to first unread message

pradip...@samhsa.hhs.gov

unread,
Sep 17, 2012, 2:19:18 PM9/17/12
to ggp...@googlegroups.com
Hi,
 
I would appreciate if someone could advise me on resolving the following two issues:
 
1)  Although I got the bar chart correct, I could not figure out why I got the following message after each of the last two lines of the code in the console: ymax not defined: adjusting position using y instead
 
2)  I don't get the desired results for: axis.title.y = theme_text(size = 10, face = "bold".  In the output of the chart, the title of the y-axis is of the small fonti size and not emboldedened.
 
Thanks,
 
Pradip Muhuri
 
 
#The code is shown below:
 setwd ('E:/Rdata')
 load ("data_smk_spd.Rdata")
 tempdata <- subset (data_smk_spd, add_col != "formr_smk")
 tempdata$spd <- factor(tempdata$spd,levels=c ("No","Yes"), 
                                     labels=c('No SPD', 'SPD'), ordered=TRUE)
 tempdata$age <- factor (tempdata$age, levels=c ('75+', '65-74', '45-64', '18-44'),
                          ordered=TRUE)       
 
library(ggplot2)      
                      
 
 p <- ggplot(data = tempdata, aes(x = age, y = est,  fill = spd)) +         
         geom_bar(position="dodge", stat="identity", width=0.50) +
        #geom_errorbar(aes(ymax = est + se,ymin = est),position=position_dodge(width=0.5), width=0.25)+
          geom_text (aes(label=round(est,1)), position=position_dodge(width=0.9),
                hjust=-.2, vsust=-.5, colour="red", size=8) +
        scale_fill_manual(values=c("black", "#996633"))+
        ylim (0,55)+
        labs (x = "  ", y= "Current Cigatette Use (%)",  fill=" ")+
        coord_flip()+
        opts(axis.text.y=theme_text(size=24, hjust=1, face="bold"),
             axis.title.y = theme_text(size = 10, face = "bold"),
             axis.text.x=theme_text(size=24, hjust=0, face="bold"),
             legend.text = theme_text(size= 20,hjust=0, face="bold"), 
             legend.position="bottom"
        )
 print (p)
 ggsave(file='curr_smk_spd.png', width=11, height=7)

Dennis Murphy

unread,
Sep 17, 2012, 4:10:09 PM9/17/12
to pradip...@samhsa.hhs.gov, ggp...@googlegroups.com
Hi:

On Mon, Sep 17, 2012 at 11:19 AM, pradip...@samhsa.hhs.gov <pradip...@samhsa.hhs.gov> wrote:
Hi,
 
I would appreciate if someone could advise me on resolving the following two issues:
 
1)  Although I got the bar chart correct, I could not figure out why I got the following message after each of the last two lines of the code in the console: ymax not defined: adjusting position using y instead

Harmless warning.
 
 
2)  I don't get the desired results for: axis.title.y = theme_text(size = 10, face = "bold".  In the output of the chart, the title of the y-axis is of the small fonti size and not emboldedened.

In theory, the code seems OK on the surface, but I wonder if ggplot2 adjusted on the fly because the y-axis margin was being pushed, possibly by the font size of the axis labels. (If so, see the theme element plot.margin.)

Additional comments:

1. You have two typos in the code: vsust should be vjust and cigarette rather than cigatette.
2. In the latest version of ggplot2 (0.9.2), the new theming system allows you to better organize common argument values. Here's what I *think* it would look like under the new system:

ggplot(...) + ... +
    theme(text = element_text(size = 25, face = "bold"),  # default text size and type face
              axis.text.x = element_text(hjust = 0),
              axis.text.y = element_text(hjust = 1),
              axis.title = element_text(size = rel(0.4)),
              axis.title.x = element_text( face = "plain"),
              legend.text = element_text(size = rel(0.8), hjust = 0),
              legend.position = "bottom")

This specification illustrates several of the features of the new theming system:
   * theme()  replaces opts()
   * element_xx()  replaces  theme_xx()
   * text is now a (top level) theme element along with line and rect;
     the other two new theme elements are axis.text and strip.text
   * the new theming system supports inheritance of theme properties =>
     theme elements now have a hierarchical structure
   * the new system supports relative sizing (e.g., rel(0.4)) -
     this is advantageous if you change the base size but want to maintain
     proportionality of sizes of related theme elements.

This is why I specified size = 25 (it makes the later relative sizes easier to define) in the top level theme element text - all other text-related theme elements inherit that as the base size unless you specify otherwise with element_text(). Similarly, axis.title.x and axis.title.y inherit from axis.title (similarly for axis.text). The idea is to streamline the amount of code written and to be more computationally efficient in handling these aspects of a ggplot.

Caveat emptor on these suggestions since you did not supply a reproducible example.

Dennis
             

--
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,
Sep 18, 2012, 12:18:47 AM9/18/12
to Dennis Murphy, Muhuri, Pradip (SAMHSA/CBHSQ), ggp...@googlegroups.com

Hi Dennis,

Thank you so much for sending me the information about the recent release of ggplot2. I have revised the code, replacing the old opt () with the theme () systems and got the new output. It seems that I failed to tweak the code to resolve the issue with respect to the y-axis title. The font for the y-axis title is still small, not of size = 25,not emboldened.

I would appreciate receiving any advice you may have to resolve the issue.

#Below is the revised code, if you decide to reproduce on your end.

setwd ('E:/Rdata')

d1 <-
"age spd sample wgt_count est se
18-44 Yes 3998 3054167 51.6 0.96
18-44 No 117834 104285177 26.1 0.22
45-64 Yes 3121 2258341 42.9 1.07
45-64 No 67565 58586570 22.9 0.21
65-74 Yes 666 424118 20.9 1.63
65-74 No 23141 17157354 13.1 0.25
75+ Yes 628 403163 8.6 1.31
75+ No 20898 14019976 6.3 0.18"

tempdata <- read.table(textConnection(d1), header=TRUE, as.is=TRUE)

tempdata$spd <- factor(tempdata$spd,levels=c ("No","Yes"),
labels=c('No SPD', 'SPD'), ordered=TRUE)
tempdata$age <- factor (tempdata$age, levels=c ('75+', '65-74', '45-64', '18-44'),
ordered=TRUE)

library(ggplot2)


p <- ggplot(data = tempdata, aes(x = age, y = est, fill = spd)) +
geom_bar(position="dodge", stat="identity", width=0.50) +
geom_text (aes(label=round(est,1)), position=position_dodge(width=0.9),
hjust=-.1, vjust=-.1, colour="red", size=8) +
scale_fill_manual(values=c("black", "#996633"))+
ylim (0,55)+
labs (x = " ", y= "Current Cigarette Use (%)", fill=" ")+
coord_flip()+
theme(text = element_text(size = 25, face = "bold"),
axis.text.x = element_text(hjust = 0, colour="black"),
axis.text.y = element_text(hjust = 1, colour="black"),
axis.title = element_text(size = rel(0.4)),
axis.title.x = element_text( face = "plain"),
#panel.border = theme_border(c("left","bottom")),
legend.text = element_text(size = rel(0.8), hjust = 0),
legend.position = "bottom")

print (p)
ggsave(file='test_curr_smk_spd.png', width=11, height=7)




________________________________________
From: Dennis Murphy [djm...@gmail.com]
Sent: Monday, September 17, 2012 4:10 PM
To: Muhuri, Pradip (SAMHSA/CBHSQ)
Cc: ggp...@googlegroups.com
Subject: Re: geom_text and axis.y.title

Hi:
To post: email ggp...@googlegroups.com<mailto:ggp...@googlegroups.com>
To unsubscribe: email ggplot2+u...@googlegroups.com<mailto:ggplot2%2Bunsu...@googlegroups.com>
More options: http://groups.google.com/group/ggplot2

Dennis Murphy

unread,
Sep 18, 2012, 3:28:19 AM9/18/12
to Muhuri, Pradip (SAMHSA/CBHSQ), ggp...@googlegroups.com
Hi:

Thanks for the reproducible example.

If you want the axis title to be size 25 and bold, just take out the two lines in theme() involving axis.title(.y) - since you coord_flip()ped, the title in question is the y-axis title, not the x-axis one (which is empty). If you want the title to be of size 10 and emboldened, then remove the axis.title.y = line.

I'd also suggest expanding the upper y-limit to 60 - the .6 trailing off the graph is distracting, more so than the 60 at the bottom right when the change is made, at least IMO...

Dennis

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Sep 18, 2012, 10:15:01 AM9/18/12
to Dennis Murphy, ggp...@googlegroups.com, Muhuri, Pradip (SAMHSA/CBHSQ)

Hi Dennis,

 

My replacing the axis.title = element_text(size = rel(0.4)) with the axis.title.y = element_text(size = rel(0.4), colour="black") has given the desired results.  Now, the y-axis (which is x-axis in the present cord_flip()ped chart ) is of large font size and emboldened.

 

The y-limit has also been expanded.

 

Thanks,

 

Pradip

 

Pradip K. Muhuri, PhD

Statistician

Substance Abuse & Mental Health Services Administration

The Center for Behavioral Health Statistics and Quality

Division of Population Surveys

1 Choke Cherry Road, Room 2-1071

Rockville, MD 20857

 

Tel: 240-276-1070

Fax: 240-276-1260

e-mail: Pradip...@samhsa.hhs.gov

 

The Center for Behavioral Health Statistics and Quality your feedback.  Please click on the following link to complete a brief customer survey:   http://cbhsqsurvey.samhsa.gov

Srinivasan, Sathish K

unread,
Sep 18, 2012, 1:05:27 PM9/18/12
to Muhuri, Pradip (SAMHSA/CBHSQ), Dennis Murphy, ggp...@googlegroups.com

I think you should be getting the following warning: “ymax not defined: adjusting position using y instead”.

To understand more about this warning, refer this ggplot2 group discussion specifically to Hadley’s comment at the bottom of this page: https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/PGzx4u6U84Y

 

I would suggest you to remove ylim (0,55) + and instead, aesthetically map the ymin to 0 and ymax to 60 (as Dennis suggested) in line 1. See the modified code. Let me know if your still getting this warning.

 

tempdata <- read.table("d1.txt", header = TRUE, sep = " ")

 

dim(tempdata)

 

tempdata$spd <- factor(tempdata$spd,

                       levels=c ("No","Yes"),

                       labels=c('No SPD', 'SPD'),

                       ordered=TRUE)

 

tempdata$age <- factor (tempdata$age,

                        levels=c ('75+', '65-74', '45-64', '18-44'),

                        ordered=TRUE)

 

library("ggplot2")

 

loadedNamespaces()

#[1] "base"         "colorspace"   "datasets"     "dichromat"    "digest"       "ggplot2"      "graphics"     "grDevices"  

# [9] "grid"         "gtable"       "labeling"     "MASS"         "memoise"      "methods"      "munsell"      "plyr"       

#[17] "proto"        "RColorBrewer" "reshape2"     "scales"       "stats"        "stringr"      "tools"        "utils"  

 

p <- ggplot(data = tempdata, aes(x = age, y = est, ymin=0, ymax=60, fill = spd)) +

         geom_bar(position="dodge", stat="identity", width=0.50) +

        geom_text (aes(label=round(est,1)), position=position_dodge(width=0.9),

                hjust=-.1, vjust=-.1, colour="red", size=8) +

        scale_fill_manual(values=c("black", "#996633"))+

        labs (x = "  ", y= "Current Cigarette Use (%)",  fill=" ")+

        coord_flip()+

          theme(text = element_text(size = 25, face = "bold"),

              axis.text.x = element_text(vjust = 0, colour="black"),

              axis.text.y = element_text(hjust = 1, colour="black"),

              axis.title.y = element_text(size = rel(0.4), colour="black"),

              axis.title.x = element_text( face = "plain"),

              legend.text = element_text(size = rel(0.8), hjust = 0),

              legend.position = "bottom")

print (p)

ggsave(file='test_curr_smk_spd.png', width=11, height=7)

 

 

HTH

Sathish

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Sep 18, 2012, 2:25:21 PM9/18/12
to Srinivasan, Sathish K, Dennis Murphy, ggp...@googlegroups.com, Muhuri, Pradip (SAMHSA/CBHSQ)

Hi Sathish,

 

With your modification of the code, that warning is gone!  Excellent!

 

One question- Is there a way to reverse the legend key/text without changing the present order of the fill=” “?

 

Thank you so much!

 

Pradip

 

Pradip K. Muhuri, PhD

Statistician

Substance Abuse & Mental Health Services Administration

The Center for Behavioral Health Statistics and Quality

Division of Population Surveys

1 Choke Cherry Road, Room 2-1071

Rockville, MD 20857

 

Tel: 240-276-1070

Fax: 240-276-1260

e-mail: Pradip...@samhsa.hhs.gov

 

The Center for Behavioral Health Statistics and Quality your feedback.  Please click on the following link to complete a brief customer survey:   http://cbhsqsurvey.samhsa.gov

 

Winston Chang

unread,
Sep 18, 2012, 5:26:28 PM9/18/12
to Muhuri, Pradip (SAMHSA/CBHSQ), Srinivasan, Sathish K, Dennis Murphy, ggp...@googlegroups.com
You can use something like this:
  guides(fill = guide_legend(reverse=TRUE))

http://wiki.stdout.org/rcookbook/Graphs/Legends%20(ggplot2)/#reversing-the-order-of-items-in-the-legend


-Winston

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Sep 18, 2012, 6:40:13 PM9/18/12
to Winston Chang, Srinivasan, Sathish K, Dennis Murphy, ggp...@googlegroups.com

Hi,

 

Yes,  that - guides(fill = guide_legend(reverse=TRUE)) - has worked.

 

Thank you so much!

 

Pradip

 

Pradip K. Muhuri, PhD

Statistician

Substance Abuse & Mental Health Services Administration

The Center for Behavioral Health Statistics and Quality

Division of Population Surveys

1 Choke Cherry Road, Room 2-1071

Rockville, MD 20857

 

Tel: 240-276-1070

Fax: 240-276-1260

e-mail: Pradip...@samhsa.hhs.gov

 

The Center for Behavioral Health Statistics and Quality your feedback.  Please click on the following link to complete a brief customer survey:   http://cbhsqsurvey.samhsa.gov

 

Reply all
Reply to author
Forward
0 new messages