Srinivasan, Sathish K | 18 Sep 19:05 2012
Picon

RE: geom_text and axis.y.title

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

 

From: ggplot2 <at> googlegroups.com [mailto:ggplot2-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Muhuri, Pradip (SAMHSA/CBHSQ)
Sent: Tuesday, September 18, 2012 10:15 AM
To: 'Dennis Murphy'
Cc: ggplot2-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org; Muhuri, Pradip (SAMHSA/CBHSQ)
Subject: RE: geom_text and axis.y.title

 

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.Muhuri-mg99Wg80Us6u4v3XbvJoYg@public.gmane.org

 

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

 

From: Dennis Murphy [mailto:djmuser-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org]
Sent: Tuesday, September 18, 2012 3:28 AM
To: Muhuri, Pradip (SAMHSA/CBHSQ)
Cc: ggplot2-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
Subject: Re: geom_text and axis.y.title

 

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

On Mon, Sep 17, 2012 at 9:18 PM, Muhuri, Pradip (SAMHSA/CBHSQ) <Pradip.Muhuri <at> samhsa.hhs.gov> wrote:


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 [djmuser-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org]
Sent: Monday, September 17, 2012 4:10 PM
To: Muhuri, Pradip (SAMHSA/CBHSQ)
Cc: ggplot2 <at> googlegroups.com
Subject: Re: geom_text and axis.y.title

Hi:


On Mon, Sep 17, 2012 at 11:19 AM, pradip.muhuri-mg99Wg80Us6u4v3XbvJoYg@public.gmane.org<mailto:pradip.muhuri-mg99Wg80Us6u4v3XbvJoYg@public.gmane.org> <pradip.muhuri-mg99Wg80Us6u4v3XbvJoYg@public.gmane.org<mailto:pradip.muhuri-mg99Wg80Us76hG3dV+HrMw@public.gmane.orgv>> 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






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)


--
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 ggplot2-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<mailto:ggplot2-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
To unsubscribe: email ggplot2+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<mailto:ggplot2%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
More options: http://groups.google.com/group/ggplot2

 

--
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 ggplot2-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe: email ggplot2+unsubscribe <at> googlegroups.com
More options: http://groups.google.com/group/ggplot2

--
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 ggplot2-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe: email ggplot2+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
More options: http://groups.google.com/group/ggplot2


Gmane