creating 3 barplots together

I am trying to create 3 barplots in one plot using ggplot2 . The 3 barplots share the same x axis values and thats how I want to group it, rather than putting the entire plots side by side like this:

Thanks for help!

How are your data structured? Assuming your data are in a data frame named DF, can you post the output of


or if that is too big, try

1 Like
> dput(head(dat))
structure(list(mann = c(1L, 1L, 2L, 2L, 2L, 1L), Alter = c(43L, 
59L, 48L, 35L, 23L, 61L), Bildung = c(6L, 3L, 7L, 5L, 6L, 7L), 
    Klimawandel = c(98L, 100L, 99L, 90L, 100L, 100L), zuk├╝nftige.Generationen.D = c(2L, 
    5L, 4L, 4L, 2L, 5L), zuk├╝nftige.Generationen = c(4L, 5L, 
    4L, 4L, 5L, 5L), Parteiwahl = c(3L, 5L, 5L, 7L, 3L, 3L), 
    Umst├Ąnde.Zukunft = c(4L, 4L, 3L, 4L, 4L, 3L), Opfer.Zukunft = c(3L, 
    3L, 3L, 2L, 3L, 2L), negative.Warnung = c(4L, 4L, 4L, 4L, 
    5L, 3L), einzelner.Beitrag = c(5L, 4L, 4L, 4L, 3L, 3L), Moral.zuk├╝nftige.Generationen = c(4L, 
    5L, 4L, 4L, 5L, 2L), Interessen.zuk├╝nftige.Generationen = c(3L, 
    5L, 4L, 4L, 5L, 3L), Alter.Wahlrecht = c(3L, 1L, 2L, 1L, 
    3L, 3L), privater.Verkehr = c(5L, 1L, 3L, 3L, 4L, 5L), Fl├╝ge = c(5L, 
    5L, 5L, 3L, 2L, 4L), Energieeffizienz = structure(c(4L, 4L, 
    4L, 4L, 4L, 4L), .Label = c("1", "2", "3", "4", "5"), class = "factor"), 
    gr├╝ner.Strom = c(5L, 5L, 5L, 3L, 2L, 3L), Kompensation = c(4L, 
    3L, 2L, 3L, 4L, 3L), gr├╝ne.Parteien = c(4L, 4L, 3L, 4L, 5L, 
    5L), Vegetarier = c(2L, 1L, 3L, 1L, 5L, 3L), Gespr├Ąche = c(3L, 
    3L, 4L, 2L, 5L, 4L), Demonstrationen = structure(c(3L, 1L, 
    1L, 1L, 4L, 4L), .Label = c("1", "2", "3", "4", "5"), class = "factor"), 
    Nachhaltigkeit = c(4L, 4L, 3L, 3L, 4L, 4L), Verhalten.Zukunft = c(3L, 
    3L, 3L, 3L, 4L, 2L), CFC = c(14L, 14L, 13L, 13L, 16L, 10L
    ), Index_individueller.Beitrag = c(25L, 20L, 21L, 19L, 24L, 
    25L), a = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 3L, 1L, 1L, 
    1L, 4L, 4L), .Dim = c(6L, 2L), .Dimnames = list(NULL, c("Energieeffizienz", 
    "Demonstrationen")))), row.names = 2:7, class = "data.frame")

Thank you for posting the data. I thought I could derive your plotting code from the data but the German column names are preventing me from doing that. Could you please post your plotting code or at least say which columns are being plotted?

Sorry for that, this may show it clearer:

> barplot(table(Energieeffizienz),
+      main = "Histogram Energieeffizienz Ger├Ąte", 
+      xlab = "gar nicht - voll und ganz", 
+      ylab = "Frequency",
+      ylim = c(0,500),
+      col = "red"
+       )
> barplot(table(Demonstrationen), 
+      main = "Histogram Teilnahme Demonstrationen", 
+      xlab = "gar nicht - voll und ganz", 
+      ylab = "Frequency",
+      ylim = c(0,500),
+      col = "blue"
+      )

I would like to have this two barplots in only one.

If I understand you correctly, I would do this using the packages ggplot2, tidyr and dplyr. I used the data you posted earlier.

library(dplyr, warn.conflicts = FALSE)
DF <- read.csv("~/R/Play/Dummy.csv", stringsAsFactors = FALSE)
DF %>% select(Energieeffizienz, Demonstrationen) %>% 
  gather(key = "Quantity", value = "Value") %>% 
  ggplot(aes(x = Value, fill = Quantity)) + geom_bar(position = "dodge")

Created on 2020-07-22 by the reprex package (v0.3.0)

1 Like

a variation on FJCC's solution:

library(dplyr, warn.conflicts = FALSE)
df %>%
  pivot_longer(cols = c(Energieeffizienz, Demonstrationen),
               names_to = "Quantity",
               values_to = "Value") %>%
  ggplot(aes(x = Value, fill = Quantity)) +
  facet_wrap(~Quantity) +
  geom_bar()  +


1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.