tornado diagram with Y axis centered at the middle of X axis

637 views
Skip to first unread message

Ulrike Keim

unread,
Oct 18, 2013, 9:13:51 AM10/18/13
to ggp...@googlegroups.com
Dear all,

I am looking for a solution how to plot a tornado diagram with Y axis centered at the middle of X axis (to craete a population age pyramid)

this is my syntax for the descrbed problem with the y axis at the left of the diagram.

dat <- data.frame(
  variable=c("00-04","05-09","10-14","15-19","20-24","25-29","30-34","35-39","40-44","45-49","50-54","55-59","60-64",
             "65-69","70-74","75-79","80-84","85+","00-04","05-09","10-14","15-19","20-24","25-29","30-34","35-39","40-44"
             ,"45-49","50-54","55-59","60-64","65-69","70-74","75-79","80-84","85+"), 
  Level=c("Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male",
          "Male","Male","Male","Female","Female","Female","Female","Female","Female","Female","Female","Female",
          "Female","Female","Female","Female","Female","Female","Female","Female","Female"),
  value=c(4.3,4.5,5.0,5.2,6.3,6.3,6.2,6.0,8.1,9.1,8.1,6.9,6.0,4.8,5.8,3.7,2.3,1.4,
          - 4.0,- 4.1,- 4.6,- 4.8,- 5.8,- 5.9,- 5.9,- 5.7,- 7.5,- 8.4,- 7.6,- 6.7,- 6.0,- 5.0,- 6.4,- 4.7,- 3.5,- 3.5)
)

library(ggplot2)

ggplot(dat, aes(variable, value, fill=Level)) + geom_bar(position="identity")
p<-ggplot(dat, aes(variable, value, fill=Level)) + geom_bar(position="identity")
p
p+coord_flip()+ ylab("Percentage") + xlab("Age")

Does anyone have an idea to fix the y axis at the middel of the x axis?

Thank you very much, ulrike

Stuart Luppescu

unread,
Oct 18, 2013, 10:50:14 AM10/18/13
to ggp...@googlegroups.com
On Fri, 2013-10-18 at 06:13 -0700, Ulrike Keim wrote:
> library(ggplot2)
>
> ggplot(dat, aes(variable, value, fill=Level)) +
> geom_bar(position="identity")
> p<-ggplot(dat, aes(variable, value, fill=Level)) +
> geom_bar(position="identity")
> p
> p+coord_flip()+ ylab("Percentage") + xlab("Age")
>
> Does anyone have an idea to fix the y axis at the middel of the x
> axis?

This question has come up before several times. On July 4 of this year I
sent this message to the list:

I had a similar problem a week or so ago. I wanted to center bars at a
certain point. The solution is to separate the data set into two and
multiply one subset by -1. Then use two geom_bar() statements. Here is
some of the code I used (I'm sure you can modify it to get it to do what
you want):

ggplot(testratings[testratings$rating>=3,], aes(x=fake.schlid,
y=percent, fill=rating.c)) +
geom_bar(stat="identity") +
scale_y_continuous(name="Percentage", limits=c(-100, 100)) +
geom_bar(data=testratings[testratings$rating<3,], aes(x=fake.schlid,
y=(-1)*percent, fill=rating.c), stat="identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_fill_manual(name="Rating", values=rev(mypal)) +
labs(title="Percentages of Ratings by School", x="School ID")
--
Stuart Luppescu <s...@ccsr.uchicago.edu>
University of Chicago

Reply all
Reply to author
Forward
0 new messages