Cannot use ylimit in ggplot when using "scale_y_continuous (trans =" ​​reverse ")"

As a question, I am trying to create a graph using the following code:

chart.demo.sex.age <- ggplot(psf10[!is.na(psf10[,"age_gp"]),c("sex","age_gp")], aes(x=age_gp))
chart.demo.sex.age.f <- chart.demo.sex.age + geom_bar(subset = .(sex =="F"), fill="red")
chart.demo.sex.age.f <- chart.demo.sex.age.f + scale_x_discrete(expand=c(0.05,0))
chart.demo.sex.age.f <- chart.demo.sex.age.f + scale_y_continuous(limits=c(0,1500), expand=c(0.05,0))
chart.demo.sex.age.f <- chart.demo.sex.age.f + opts(axis.title.x = theme_blank(),
                                                    axis.title.y = theme_blank(),
                                                    axis.text.y  = theme_blank(),
                                                    axis.ticks = theme_blank(),
                                                    panel.border = theme_rect(colour="black"),
                                                    plot.margin = unit(c(1,0,1,1),"lines"))
chart.demo.sex.age.f <- chart.demo.sex.age.f + scale_y_continuous(trans = 'reverse') + coord_flip()

Since scale_y_continuous(trans = 'reverse')I can not use ylimit(i.e., I do not see the settings in ylimit), I do not know why. Any suggestions? Thank.

+3
source share
1 answer

As Hadley suggested: use the scale_continuous limits parameter. See: http://had.co.nz/ggplot2/scale_continuous.html

Eg .: scale_y_continuous(trans = 'reverse', limits=c(0, 1500))

In your example, you cannot see the setting because you are overwriting your last parameter with the scale_y_continuouslimits defined earlier.

Good luck

+4
source

Source: https://habr.com/ru/post/1782876/


All Articles