2

Maybe I have missed this in the stackexchange literature, as I'm surprised to find many solutions for adding floating axis labels and adjustments to axes (e.g. add "floating" axis labels in facet_wrap plot), but nothing to solve my issue of overlapping x-axis labels with a facet_wrap and scales = "free". This one is close, but for an older ggplot version: overlapping y-scales in facet (scale="free"). This one may be the answer, to write a function to do such an operation, but I couldn't make it work for me: Is there a way of manipulating ggplot scale breaks and labels?

Here is a reproducible example:

v<-runif(1000,min=1000000,max=10000000)
x<-runif(100,min=0.1,max=1)
y<-runif(100000,min=0,max=20000)
z<-runif(100000,min=10000,max=2000000)
df<-data.frame(v,x,y,z)
colnames(df)<-c("Order V","Order X","Order Y","Order z")

library(reshape2)
melt.df<-melt(df[,c(1:4)])

library(ggplot2)
ggplot(melt.df, aes(x = value)) +
  geom_histogram(bins=50,na.rm=TRUE) +
  facet_wrap(~variable, scales="free") +
  theme_bw()

The resulting figure is this:

Ex

I have a similar setup, which produces this:

Histogram Any help on a cmd to do this, or a function that could set up these x-axis label breaks would be awesome!

Community
  • 1
  • 1
LauraR
  • 181
  • 1
  • 2
  • 12
  • First step: make a reproducible example that represents your real use. It doesn't have to be your data, just simulate something. Otherwise every person who wants to work on your problem has to come up with their own... But the `diamond` example has 2 digits in every x-axis label and the other plot has between 4 and 7 digits for each label. It seems like the `diamond` example isn't a good one. – Gregor Thomas Nov 18 '16 at 20:48
  • 1
    @Gregor I'm not sure I deserve a -1 for this, though now I'll be curious to see how the solutions for the two different cases are unique. – LauraR Nov 18 '16 at 22:37
  • Downvote gladly retracted now that there's a good example! For the diamonds example a small font size adjustment would have worked pretty well. For your case that's definitely not true, and it's nice to be able to test on something resembling the real use case. Otherwise somebody answers and you just end up commenting "thanks, it works for the simple example but not for my real data". – Gregor Thomas Nov 18 '16 at 22:38
  • 1
    @Gregor you make good points. And I appreciate the feedback. – LauraR Nov 18 '16 at 22:44
  • Couple more questions: how are you using the output? Is it being saved? Do you know the dimensions of the save? And are you open to number format-related fixes (i.e., more scientific notation)? What about rotating the text? – Gregor Thomas Nov 18 '16 at 22:49
  • I am open to rotating the text or adding legible scientific notation to accommodate. I am saving the output and using it as part of a regression model. Are you referring to the dimensions of the df or the plot. Assuming the df, the dim of each set of values is shown with "n=" at the top of each subplot. – LauraR Nov 19 '16 at 00:10
  • No, I mean dimensions of the saved plot. We can switch to scientific notation fairly easily, but at the end of the day we need to specify a font size, and how big that font size is relative to the plot will depend on the dimensions of the saved image. A final check/adjustment by hand is the only way to guarantee good results. – Gregor Thomas Nov 19 '16 at 18:12
  • I'm using height = 5, width = 7, saving as a png. I plan to have this image quality for a publication. – LauraR Nov 21 '16 at 05:47

2 Answers2

2

I figured it out - at least a hacked answer - and will post my solution in case others can use it. Basically I adjusted the font size of the axis text, and used the scales pkg to keep the notation consistent (i.e. get rid of scientific). My altered code is:

ggplot(melt.df, aes(x = value)) +
geom_histogram(bins=50,na.rm=TRUE) +
facet_wrap(~variable, scales="free") +
theme_bw()+
theme(axis.text=element_text(size=10),text=element_text(size=16))+
scale_x_continuous(labels = scales::comma)
LauraR
  • 181
  • 1
  • 2
  • 12
0

If you don't want the commas in the labels, you can set something like

options(scipen = 10)

before plotting. This makes the threshold for using scientific notation higher so ordinary labels will be used in this case.

Joe
  • 8,073
  • 1
  • 52
  • 58