Side by Side Histograms in the Same Graph in R

How to plot multiple histograms without overlapping in R

If you want to have multiple plots in the same screen you can use the command

par(mfrow = c(2,1))

Where c(2,1) means you would like to have 2 rows and 1 column of charts, putting your charts side by side. If you put c(1,3) you would be telling R to put your charts in 1 row and 3 columns, and so on and so forth.

Then just plot your charts one after the other and they will fill the correspondent space.

EDIT: if you want to calculate automatically the row and columns for the par function you can create a function like this (or something more refined and pass it to par)

dimension = function(df){
kk = dim(df)[2];

x = round(sqrt(kk),0);
y = ceiling(kk/x);

return(c(x,y))
}

Being your code

set.seed(3)
Ex <- xts(1:100, Sys.Date()+1:100)
df = data.frame(Ex,matrix(rnorm(100*4,mean=123,sd=3), nrow=100))
df<-df[,-1]

par(mfrow = dimension(df))

for(i in names(df)){
hist(df[[i]] ,main="Histogram",xlab="x",col="green",label=TRUE,plot = TRUE)
}

R Side by Side Histograms

the par(mfrow = c(2,1) should be par(mfrow = c(2,1))

set.seed(123)
x <- rnorm(1000, 0, 2)
set.seed(123)
y <- rnorm(1000, 0, 1)
cbind(x,y)
sd1 <- sd(x)
sd2 <- sd(y)
hist(x, col =c('red'))
hist(y, col =c('green'))
par(mfrow = c(2,1))
xlim=c(-6,6)

How to plot two histograms together in R?

That image you linked to was for density curves, not histograms.

If you've been reading on ggplot then maybe the only thing you're missing is combining your two data frames into one long one.

So, let's start with something like what you have, two separate sets of data and combine them.

carrots <- data.frame(length = rnorm(100000, 6, 2))
cukes <- data.frame(length = rnorm(50000, 7, 2.5))

# Now, combine your two dataframes into one.
# First make a new column in each that will be
# a variable to identify where they came from later.
carrots$veg <- 'carrot'
cukes$veg <- 'cuke'

# and combine into your new data frame vegLengths
vegLengths <- rbind(carrots, cukes)

After that, which is unnecessary if your data is in long format already, you only need one line to make your plot.

ggplot(vegLengths, aes(length, fill = veg)) + geom_density(alpha = 0.2)

Sample Image

Now, if you really did want histograms the following will work. Note that you must change position from the default "stack" argument. You might miss that if you don't really have an idea of what your data should look like. A higher alpha looks better there. Also note that I made it density histograms. It's easy to remove the y = ..density.. to get it back to counts.

ggplot(vegLengths, aes(length, fill = veg)) + 
geom_histogram(alpha = 0.5, aes(y = ..density..), position = 'identity')

Sample Image

Create multigroup histograms side by side in R

If I have understood your question this may be what you are after

ggplot(data = df3, 
aes(interaction(menutype,Belief), #get combination of groups
prop , fill = Belief) +
geom_bar(stat = "identity", position = "dodge")+
scale_x_discrete(labels = levels(df3$menutype)) # adds clean label to x

Datasummary - Plot histograms side-by-side

I solved my problem thanks to @Vincent.

tmp_list <- lapply(normal_df, na.omit)
tmp_list <- lapply(tmp_list, scale)

tmp_list2 <- lapply(outlier_df, na.omit)
tmp_list2 <- lapply(tmp_list2, scale)

emptycol = function(x) " "

kbl1 <- datasummary(All(normal_df) ~ Mean + SD + Heading("Boxplot") * emptycol + Heading("Histogram") * emptycol, data = normal_df) %>%
column_spec(column = 4, image = spec_boxplot(tmp_list, same_lim = FALSE)) %>%
column_spec(column = 5, image = spec_hist(tmp_list, same_lim = FALSE)) %>%
kable_styling(full_width = FALSE, position = "float_left", font_size = 12) %>%
add_header_above(c("", "Normal" = 4))

kbl2 <- datasummary(All(outlier_df) ~ Mean + SD + Heading("Boxplot") * emptycol +
Heading("Histogram") * emptycol, data = outlier_df) %>%
column_spec(column = 4, image = spec_boxplot(tmp_list2, same_lim = FALSE)) %>%
column_spec(column = 5, image = spec_hist(tmp_list2, same_lim = FALSE)) %>%
kable_styling(full_width = FALSE, position = "left", font_size = 12) %>%
add_header_above(c("", "Outlier" = 4))

knitr::kables(list(kbl1,kbl2)) %>% kable_styling()


Related Topics



Leave a reply



Submit