2
votes

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!

2
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
@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
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
@Gregor you make good points. And I appreciate the feedback.LauraR
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

2 Answers

2
votes

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)
0
votes

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.