Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

Overlaying two quick plot all variables from several data frames

$
0
0

I have uploaded a datafame and done a quick plot of all variables using:

df %>%
  keep(is.numeric) %>% 
  gather() %>% 
  ggplot(aes(value)) +
  facet_wrap(~ key, scales = "free") +
  geom_histogram() 

Reference: https://drsimonj.svbtle.com/quick-plot-of-all-variables

I have split this data frame into two data frames based on a binary variable (in my case, Smoker/Non-smoker) in one of the columns. I would like to perform the same quick plot of all variables but have overlayed, different coloured histograms for each of the new data frames (to see if they differ significantly).

I found the following:

Overlaying two ggplot facet_wrap histograms

But it only does the facet_wrap over a single variable. Is there a way to do this by filtering the gathered data frame by the binary value something like:

df %>%
  keep(is.numeric) %>% 
  gather() %>% 
  ggplot(aes(value)) +
  facet_wrap(~ key, scales = "free") +
  geom_histogram(subset(df,Smoker==1), fill = "Red", alpha=0.3) +
  geom_histogram(subset(df,Smoker==2), fill = "Blue", alpha=0.3) 

Idea would be to overlay the following:

df_s %>%
  keep(is.numeric) %>% 
  gather() %>% 
  ggplot(aes(value)) +
  facet_wrap(~ key, scales = "free") +
  geom_histogram(fill = "Red", alpha=0.3) 

df_ns %>%
  keep(is.numeric) %>% 
  gather() %>% 
  ggplot(aes(value)) +
  facet_wrap(~ key, scales = "free") +
  geom_histogram(fill = "Blue", alpha=0.3)

I could do this will a loop but would like to do it with the df key-value pairs if possible.


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>