GGplot2 facet_wrap() with scaling for each variable
I have a dataframe data
with 9 predictor variables and one indicator variable ind
with values from 0 to 3. I need to make histograms for all of the variables given a value of ind
. I've already wrote some code; having no better idea I just divided my dataset into 4 disjoint subsets.
data_no0 <- data[data$ind == 0, -1]
data_no1 <- data[data$ind == 1, -1]
data_no2 <- data[data$ind == 2, -1]
data_no3 <- data[data$ind == 3, -1]
ggplot(gather(data_no1), aes(value)) +
geom_histogram(bins = 6) +
facet_wrap(~key, scales = 'free_x')
What's more I want to be able to compare histograms for different values of ind
so I would like to fix min and max values of x to be (i.e for the first variable) min(data$avg_f0env_sma0)
and max(data$avg_f0env_sma)
, respectively.
I've tried to do it in that way:
scales_x <- list(
'avg_jitterlocal_sma'
= scale_y_continuous(limits = c(min(data$avg_jitterlocal_sma), max(data$avg_jitterlocal_sma))),
'avg_jitterddp_sma'
= scale_y_continuous(limits = c(min(data$avg_jitterddp_sma), max(data$avg_jitterddp_sma))),
'avg_shimmerlocal_sma'
= scale_y_continuous(limits = c(min(data$avg_shimmerlocal_sma), max(data$avg_shimmerlocal_sma))),
'avgx_pcm_fftmag_spectralflux_sma'
= scale_y_continuous(limits = c(min(data$avg_pcm_fftmag_spectralflux_sma), max(data$avg_pcm_fftmag_spectralflux_sma))),
'avg_pcm_fftmag_spectralcentroid_sma'
= scale_y_continuous(limits = c(min(data$avg_pcm_fftmag_spectralcentroid_sma), max(data$avg_pcm_fftmag_spectralcentroid_sma))),
'avg_pcm_fftmag_spectralharmonicity_sma_compare'
= scale_y_continuous(limits = c(min(data$avg_pcm_fftmag_spectralharmonicity_sma_compare), max(data$avg_pcm_fftmag_spectralharmonicity_sma_compare))),
'avg_f0final_sma'
= scale_y_continuous(limits = c(min(data$avg_f0final_sma), max(data$avg_f0final_sma))),
'avg_f0env_sma'
= scale_y_continuous(limits = c(min(data$avg_f0env_sma), max(data$avg_f0env_sma))),
'avg_loudness_sma3'
= scale_y_continuous(limits = c(min(data$avg_loudness_sma3), max(data$avg_loudness_sma3)))
)
ggplot(gather(data_no0), aes(value)) +
geom_histogram(bins = 6) +
facet_wrap(~key, scales = list(x = scales_x))
But it doesn't work. I've also read about facet_grid_sc()
, but there's also a problem with it.
Looks like your dataset could use some tidying, but data wrangling principles aside, it seems that you are looking to control the scales individually for each facet - am I correct?
The lemon
package contains some useful functions including facet_rep_wrap()
and facet_rep_grid()
. One useful feature of these functions is to allow for each facet to have a differently scaled y or x axis. Here's an example with the diamonds
dataset.
Histograms of price according to cut with facet_wrap()
:
library(ggplot2)
library(lemon)
ggplot(diamonds, aes(x=price, fill=cut)) +
geom_histogram() +
facet_wrap(~cut)
And here's the same plot using lemon::facet_rep_wrap()
with a free y scale:
ggplot(diamonds, aes(x=price, fill=cut)) +
geom_histogram() +
facet_rep_wrap(~cut, scales="free")