Luis Loza
Luis Loza

Reputation: 11

Trying to Plot Each Column of Dataframe as its Own Histogram

What I'm currently stuck on is trying to plot each column of my dataframe as its own histogram in ggplot. I attached a screenshot below:

Ideally I would be able to compare the values in every 'Esteem' column side-by-side by plotting multiple histograms.

I tried using the melt() function to reshape my dataframe, and then feed into ggplot() but somewhere along the way I'm going wrong...

Upvotes: 1

Views: 220

Answers (2)

zephryl
zephryl

Reputation: 17069

You could pivot to long, then facet by column:

library(tidyr)
library(ggplot2)

esteem81_long <- esteem81 %>% 
  pivot_longer(
    Esteem81_1:Esteem81_10, 
    names_to = "Column", 
    values_to = "Value"
  )

ggplot(esteem81_long, aes(Value)) +
  geom_bar() +
  facet_wrap(vars(Column))

Or for a list of separate plots, just loop over the column names:

plots <- list()
for (col in names(esteem81)[-1]) {
  plots[[col]] <- ggplot(esteem81) +
    geom_bar(aes(.data[[col]]))
}

plots[["Esteem81_4"]]

Example data:

set.seed(13)

esteem81 <- data.frame(Subject = c(2,6,7,8,9))
for (i in 1:10) {
  esteem81[[paste0("Esteem81_", i)]] <- sample(1:4, 5, replace = TRUE)
}

Upvotes: 1

PGSA
PGSA

Reputation: 3071

esteem_long <- esteem81 %>% pivot_longer(cols = -c(Subject))

plot <- ggplot(esteem_long, aes(x = value)) +
  geom_histogram(binwidth = 1) +
  facet_wrap(vars(name))
plot

I'm using pivot_longer() from tidyr and ggplot2 for the plotting.

The line pivot_longer(cols = -c(Subject)) reads as "apart from the "Subject" column, all the others should be pivoted into long form data." I've left the default new column names ("name" and "value") - if you rename them then be sure to change the downstream code.

geom_histogram automates the binning and tallying of the data into histogram format - change the binwidth parameter to suit your desired outcome.

facet_wrap() allows you to specify a grouping variable (here name) and will replicate the plot for each group.

Upvotes: 0

Related Questions