Reputation: 181
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:
I have a similar setup, which produces this:
Any help on a cmd to do this, or a function that could set up these x-axis label breaks would be awesome!
Upvotes: 2
Views: 2842
Reputation: 8631
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.
Upvotes: 0
Reputation: 181
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)
Upvotes: 2