CroatiaHR
CroatiaHR

Reputation: 625

geom_histogram() to hist() in R

I have the following code:

mapping <- aes(
  x = values
  , color = factor(par_a)
)

plot <- (ggplot(data=data, mapping=mapping)
         + geom_histogram(binwidth = 5, na.rm = TRUE)
         + facet_grid(par_b ~ par_c ~ par_d, scales = "free")
)

Since I am asked to use instead hist() because of the possibility to use plot=FALSE, now I want to adjust the code.

mapping <- aes(
      x = values
      , color = factor(par_a)
    )

plot2 <- hist(values, breaks = seq(min(values), max(values)+5, by = 5))
         + facet_grid(par_b ~ par_c ~ par_d, scales = "free")

However, I have no idea how to implement the 'color = factor(par_a)' or the whole line 'facet_grid(par_b ~ par_c ~ par_d, scales = "free")'. I guess these functions are not explicitly supported for 'hist()', but I would really appreciate it if someone could tell me what the alternatives for them would be?

Upvotes: 0

Views: 61

Answers (1)

cookesd
cookesd

Reputation: 1336

the base plotting can use the function par(mfrow=c(num_rows,num_cols)) in order to build subplots. The next plot, hist, etc. calls that you make will fill the desired subplots. and to color your bars within the plots you can make a variable as described here to pass to the color parameter of hist.

Upvotes: 1

Related Questions