force axis to extend

6 messages
Open this post in threaded view
|
Report Content as Inappropriate

force axis to extend

 Hi,  I'm creating a couple of mirrored bar plots.  Below is data and code for one. My problem is that I need the axis to go from -35 to 35 by 5.  I can't get that to happen with the code below.  I need it so all my plots are on the same scale. How can I do that using barplot? For reasons, I can't use ggplot or lattice. Thanks, Jen df <- data.frame(matrix(c( '18-29',        'Females',      23.221039, '30-44',        'Females',      16.665565, '45-59',        'Females',      7.173238, '60+',  'Females',      4.275979, '18-29',        'Males',        -22.008875, '30-44',        'Males',        -15.592936, '45-59',        'Males',        -7.312195, '60+',  'Males',        -3.750173), nrow=8, ncol=3, byrow=T, dimnames=list(NULL, c("Age", "Sex", "Percent")))) df\$Percent <- as.numeric(as.character(df\$Percent)) midf <- barplot(height = df\$Percent[df\$Sex == "Females"]) # distribution of men and women with solid fill plot(c(0,5),range(df\$Percent),type = "n", axes=FALSE, ann=F) barplot(height = df\$Percent[df\$Sex == "Females"], add = TRUE,axes = FALSE, col="#b498ec", ylab="") barplot(height = df\$Percent[df\$Sex == "Males"], add = TRUE, axes = F, col="#f8bb85", ylab="",         names.arg=c("18-29", "30-44", "45-59", "60+")) axis(side=2, at = seq(-35,35,by=5),      labels=format(abs(seq(-35,35,by=5)), scientific=F),      cex.axis=0.7)         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: force axis to extend

 Hi Jen, It seems way too simple, but does this work? axis(side=2,at=seq(-35,35,by=5),cex.axis=0.7) You may want to consider using a pyramid plot for this. Jim On Thu, Mar 16, 2017 at 11:45 AM, Jen <[hidden email]> wrote: > Hi,  I'm creating a couple of mirrored bar plots.  Below is data and code > for one. > > My problem is that I need the axis to go from -35 to 35 by 5.  I can't get > that to happen with the code below.  I need it so all my plots are on the > same scale. > > How can I do that using barplot? For reasons, I can't use ggplot or > lattice. > > Thanks, > > Jen > > > > df <- data.frame(matrix(c( > '18-29',        'Females',      23.221039, > '30-44',        'Females',      16.665565, > '45-59',        'Females',      7.173238, > '60+',  'Females',      4.275979, > '18-29',        'Males',        -22.008875, > '30-44',        'Males',        -15.592936, > '45-59',        'Males',        -7.312195, > '60+',  'Males',        -3.750173), > nrow=8, ncol=3, byrow=T, > dimnames=list(NULL, c("Age", "Sex", "Percent")))) > > df\$Percent <- as.numeric(as.character(df\$Percent)) > > midf <- barplot(height = df\$Percent[df\$Sex == "Females"]) > > # distribution of men and women with solid fill > > plot(c(0,5),range(df\$Percent),type = "n", axes=FALSE, ann=F) > > barplot(height = df\$Percent[df\$Sex == "Females"], add = TRUE,axes = FALSE, > col="#b498ec", ylab="") > > barplot(height = df\$Percent[df\$Sex == "Males"], add = TRUE, axes = F, > col="#f8bb85", ylab="", >         names.arg=c("18-29", "30-44", "45-59", "60+")) > > axis(side=2, at = seq(-35,35,by=5), >      labels=format(abs(seq(-35,35,by=5)), scientific=F), >      cex.axis=0.7) > >         [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: force axis to extend

 Hi Jim, Thanks for replying. Unfortunately, that doesn't work.   The axis automatically scales to (-30, 25, by 5). Jen On Wed, Mar 15, 2017, 10:09 PM Jim Lemon <[hidden email]> wrote: > Hi Jen, > It seems way too simple, but does this work? > > axis(side=2,at=seq(-35,35,by=5),cex.axis=0.7) > > You may want to consider using a pyramid plot for this. > > Jim > > > On Thu, Mar 16, 2017 at 11:45 AM, Jen <[hidden email]> > wrote: > > Hi,  I'm creating a couple of mirrored bar plots.  Below is data and code > > for one. > > > > My problem is that I need the axis to go from -35 to 35 by 5.  I can't > get > > that to happen with the code below.  I need it so all my plots are on the > > same scale. > > > > How can I do that using barplot? For reasons, I can't use ggplot or > > lattice. > > > > Thanks, > > > > Jen > > > > > > > > df <- data.frame(matrix(c( > > '18-29',        'Females',      23.221039, > > '30-44',        'Females',      16.665565, > > '45-59',        'Females',      7.173238, > > '60+',  'Females',      4.275979, > > '18-29',        'Males',        -22.008875, > > '30-44',        'Males',        -15.592936, > > '45-59',        'Males',        -7.312195, > > '60+',  'Males',        -3.750173), > > nrow=8, ncol=3, byrow=T, > > dimnames=list(NULL, c("Age", "Sex", "Percent")))) > > > > df\$Percent <- as.numeric(as.character(df\$Percent)) > > > > midf <- barplot(height = df\$Percent[df\$Sex == "Females"]) > > > > # distribution of men and women with solid fill > > > > plot(c(0,5),range(df\$Percent),type = "n", axes=FALSE, ann=F) > > > > barplot(height = df\$Percent[df\$Sex == "Females"], add = TRUE,axes = > FALSE, > > col="#b498ec", ylab="") > > > > barplot(height = df\$Percent[df\$Sex == "Males"], add = TRUE, axes = F, > > col="#f8bb85", ylab="", > >         names.arg=c("18-29", "30-44", "45-59", "60+")) > > > > axis(side=2, at = seq(-35,35,by=5), > >      labels=format(abs(seq(-35,35,by=5)), scientific=F), > >      cex.axis=0.7) > > > >         [[alternative HTML version deleted]] > > > > ______________________________________________ > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: force axis to extend

 In reply to this post by Jim Lemon-4 Use plot(NA, xlim=c(0, 5), ylim=c(-35, 35), type="n", axes=FALSE, ann=FALSE) to set up the length of the y axis instead of your first plot command. ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:[hidden email]] On Behalf Of Jim Lemon Sent: Wednesday, March 15, 2017 9:09 PM To: Jen <[hidden email]> Cc: r-help mailing list <[hidden email]> Subject: Re: [R] force axis to extend Hi Jen, It seems way too simple, but does this work? axis(side=2,at=seq(-35,35,by=5),cex.axis=0.7) You may want to consider using a pyramid plot for this. Jim On Thu, Mar 16, 2017 at 11:45 AM, Jen <[hidden email]> wrote: > Hi,  I'm creating a couple of mirrored bar plots.  Below is data and code > for one. > > My problem is that I need the axis to go from -35 to 35 by 5.  I can't get > that to happen with the code below.  I need it so all my plots are on the > same scale. > > How can I do that using barplot? For reasons, I can't use ggplot or > lattice. > > Thanks, > > Jen > > > > df <- data.frame(matrix(c( > '18-29',        'Females',      23.221039, > '30-44',        'Females',      16.665565, > '45-59',        'Females',      7.173238, > '60+',  'Females',      4.275979, > '18-29',        'Males',        -22.008875, > '30-44',        'Males',        -15.592936, > '45-59',        'Males',        -7.312195, > '60+',  'Males',        -3.750173), > nrow=8, ncol=3, byrow=T, > dimnames=list(NULL, c("Age", "Sex", "Percent")))) > > df\$Percent <- as.numeric(as.character(df\$Percent)) > > midf <- barplot(height = df\$Percent[df\$Sex == "Females"]) > > # distribution of men and women with solid fill > > plot(c(0,5),range(df\$Percent),type = "n", axes=FALSE, ann=F) > > barplot(height = df\$Percent[df\$Sex == "Females"], add = TRUE,axes = FALSE, > col="#b498ec", ylab="") > > barplot(height = df\$Percent[df\$Sex == "Males"], add = TRUE, axes = F, > col="#f8bb85", ylab="", >         names.arg=c("18-29", "30-44", "45-59", "60+")) > > axis(side=2, at = seq(-35,35,by=5), >      labels=format(abs(seq(-35,35,by=5)), scientific=F), >      cex.axis=0.7) > >         [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: force axis to extend

 In reply to this post by sabbyATL > Unfortunately, that doesn't work.   The axis automatically scales to (-30, > 25, by 5). Your data do not extend to ±35 so the axis limits you have asked axis() for are outside the plot region. it is plot() that is (correctly) defaulting to the data range. if you want to override that to get a larger plot range, specify the y limits for the plot as part of the plot command: plot(c(0,5),range(df\$Percent),type = "n", axes=FALSE, ann=F, ylim=c(-35, 35)) barplot(height = df\$Percent[df\$Sex == "Females"], add = TRUE,axes = FALSE, col="#b498ec", ylab="") barplot(height = df\$Percent[df\$Sex == "Males"], add = TRUE, axes = F, col="#f8bb85", ylab="",         names.arg=c("18-29", "30-44", "45-59", "60+")) axis(side=2, at = seq(-35,35,by=5),      labels=format(abs(seq(-35,35,by=5)), scientific=F),      cex.axis=0.7)         #Axes now run to ±35 S Ellison > > Jen > > > > On Wed, Mar 15, 2017, 10:09 PM Jim Lemon <[hidden email]> > wrote: > > > Hi Jen, > > It seems way too simple, but does this work? > > > > axis(side=2,at=seq(-35,35,by=5),cex.axis=0.7) > > > > You may want to consider using a pyramid plot for this. > > > > Jim > > > > > > On Thu, Mar 16, 2017 at 11:45 AM, Jen <[hidden email]> > > wrote: > > > Hi,  I'm creating a couple of mirrored bar plots.  Below is data and > > > code for one. > > > > > > My problem is that I need the axis to go from -35 to 35 by 5.  I > > > can't > > get > > > that to happen with the code below.  I need it so all my plots are > > > on the same scale. > > > > > > How can I do that using barplot? For reasons, I can't use ggplot or > > > lattice. > > > > > > Thanks, > > > > > > Jen > > > > > > > > > > > > df <- data.frame(matrix(c( > > > '18-29',        'Females',      23.221039, > > > '30-44',        'Females',      16.665565, > > > '45-59',        'Females',      7.173238, > > > '60+',  'Females',      4.275979, > > > '18-29',        'Males',        -22.008875, > > > '30-44',        'Males',        -15.592936, > > > '45-59',        'Males',        -7.312195, > > > '60+',  'Males',        -3.750173), > > > nrow=8, ncol=3, byrow=T, > > > dimnames=list(NULL, c("Age", "Sex", "Percent")))) > > > > > > df\$Percent <- as.numeric(as.character(df\$Percent)) > > > > > > midf <- barplot(height = df\$Percent[df\$Sex == "Females"]) > > > > > > # distribution of men and women with solid fill > > > > > > plot(c(0,5),range(df\$Percent),type = "n", axes=FALSE, ann=F) > > > > > > barplot(height = df\$Percent[df\$Sex == "Females"], add = TRUE,axes = > > FALSE, > > > col="#b498ec", ylab="") > > > > > > barplot(height = df\$Percent[df\$Sex == "Males"], add = TRUE, axes = > > > F, col="#f8bb85", ylab="", > > >         names.arg=c("18-29", "30-44", "45-59", "60+")) > > > > > > axis(side=2, at = seq(-35,35,by=5), > > >      labels=format(abs(seq(-35,35,by=5)), scientific=F), > > >      cex.axis=0.7) > > > > > >         [[alternative HTML version deleted]] > > > > > > ______________________________________________ > > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > > > https://stat.ethz.ch/mailman/listinfo/r-help> > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html> > > and provide commented, minimal, self-contained, reproducible code. > > > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-> guide.html > and provide commented, minimal, self-contained, reproducible code. ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}} ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.