r - How to change negative x axis breaks (labels) of ggplot2 bar graph into positive ones? -
i have dataframe looks one:
df <- structure(list(gender = c("male", "male", "male", "female", "female", "female", "male", "male", "male", "male", "male", "female", "female", "female"), agegroup = c("-24", "25-34", "45-54", "-24", "25-34", "35-44", "-24", "25-34", "35-44", "65-", "unknown", "-24", "25-34", "35-44"), n = c(-2, -4, -1, 3, 4, 1, -3, -14, -1, -1, -2, 3, 3, 1), n2 = c(2, 4, 1, 3, 4, 1, 3, 14, 1, 1, 2, 3, 3, 1), location = c("here", "here", "here", "here", "here", "here", "there", "there", "there", "there", "there", "there", "there", "there")), .names = c("gender", "agegroup", "n", "n2", "location"), row.names = c(na, 14l), class = "data.frame")
i needed make of n's negatives because wanted make "pyramid". this:
ggplot(biofile2, aes(x = agegroup, y = n , fill = gender),color=gender) + geom_bar(stat="identity", size=.3, position="identity")+ facet_wrap(~ location,ncol=2)+ coord_flip()
it looks intended. left change -10 , -5 positives. there way without changing appearance of plot?
it seems want make axis labels positive, i.e. take absolute value. can using scale_*_continuous
breaks
, labels
arguments. note in case need transform y-axis, since have refer axis before coord_flip
. pretty
function can used conveniently generate pretty axis breaks:
add plot:
scale_y_continuous(breaks = pretty(df$n), labels = abs(pretty(df$n))
Comments
Post a Comment