2

Possible Duplicate:
Using ggplot2, can I insert a break in the axis?

I'm using the following ggplot2 code to generate a faced_grid barplots:

ggplot(plotobj, aes(as.factor(gm) , peaks, fill=rvalue)) +
  geom_bar(stat="identity") +
  facet_grid(rvalue ~ .,scales="free") + 
  opts(legend.position = "none")

Which gives the following plot (screenshot of the first facet): ggplot2 barplot axis issue

As you can see the y-axis get stretched to quite a high value because of 1 outlier. What I'd like to do is create a more sensible scaling by having more ticks until 2e+05 and then just have 1 tick that goes directly towards 5e+05. This way the scaling would not be linear anymore but it would allow to show that there is a massive peak for 1 of the categories.

Is there anyway of doing this simple with ggplot2? Is there a R trick for doing this? If possible I'd not like to use things like ylim to just not show the top anymore.

Community
  • 1
  • 1
Sander
  • 801
  • 2
  • 7
  • 13
  • A while back I asked a similar question on crossvalidated. http://stats.stackexchange.com/questions/1764/what-are-alternatives-to-broken-axes – Roman Luštrik Jun 12 '12 at 11:56

1 Answers1

2

You could use a transformation on the y-axis. Untested since you did not provide a reproducible example.

ggplot(plotobj, aes(as.factor(gm) , peaks, fill=rvalue)) +
    geom_bar(stat="identity") + facet_grid(rvalue ~ .,scales="free") + 
    opts(legend.position = "none") + scale_y_log10()
ggplot(plotobj, aes(as.factor(gm) , peaks, fill=rvalue)) +
    geom_bar(stat="identity") + facet_grid(rvalue ~ .,scales="free") + 
    opts(legend.position = "none") + scale_y_sqrt()
ggplot(plotobj, aes(as.factor(gm) , peaks, fill=rvalue)) +
    geom_bar(stat="identity") + facet_grid(rvalue ~ .,scales="free") + 
    opts(legend.position = "none") + coord_trans(y = "log10")
ggplot(plotobj, aes(as.factor(gm) , peaks, fill=rvalue)) +
    geom_bar(stat="identity") + facet_grid(rvalue ~ .,scales="free") + 
    opts(legend.position = "none") + coord_trans(y = "sqrt")
Thierry
  • 18,049
  • 5
  • 48
  • 66
  • Sorry for not giving a proper example. I could sample some data and put it online. Will do so! Though it looks like you more or less change the values to logs or sqrt and not really create a "broken axis" as described in the comment above. Will give them a try though to see how it looks! – Sander Jun 12 '12 at 13:31
  • 1
    a "broken axis" is not possible in ggplot2. And it is not a good idea. In your current plot you see one bar which is 5 times bigger than the other bars. If you 'break' the axis, you will lose that kind of interpretation. But people still will try to do that kind of interpretation, only now size of the bar will no longer be 5 times larger but much smaller (e.g. 1.5 times larger). – Thierry Jun 12 '12 at 14:23
  • You right, broken axis is actually an awful invention. I'll just fiddle around with the type of y-axis scaling and otherwise just make an additional zoomed in plot with the ylim feature! Thanks a bunch – Sander Jun 13 '12 at 09:52
  • You're right, broken axes are sometimes misleading, and can be used deceptively. However, they are sometimes useful. I, for instance, have a dataset where 20 values are int he range of 0-2000, and one value is 200,000. I either have to draw two graphs to show all this (waste of space), do a transformation of my data (which puts it into odd units, and also ruins the proportions the charts want to show), or have an axis break which indicates that this one column is really really tall. By breaking the axis my data is compactly displayed and I can display all of it. – ccoffman Dec 18 '15 at 05:19