-3

I am trying to make a graph in R with data like:

sex ethnicity x2015 x2016 x2017
-------------------------------

male    dutch      112    117   116   
female  dutch      114    118   120
female  german     102    101   99
etc     

I want make a bart chart from each data row...

Thanks for your help!!

pogibas
  • 27,303
  • 19
  • 84
  • 117
Egbert
  • 11
  • 1
  • 2
    Welcome to stackoverflow, the aforementioned isn't a codewriting service, let us know what you've tried first. see: [mcve] – Joel Sep 11 '18 at 09:21
  • 1
    Possible duplicate of [Grouping Barplot in R](https://stackoverflow.com/questions/30269695/grouping-barplot-in-r) – pogibas Sep 11 '18 at 09:59

1 Answers1

0

You can use ggplot2 package using stat = identity and facet_wrap. Additionally you can use melt function from reshape2 package to transform data convinient for use in ggplot2 - from wide to narrow dataset format. Please see the code below:

library(ggplot2)
library(reshape2)

data <- structure(list(sex = structure(c(2L, 1L, 1L), .Label = c("female", 
"male"), class = "factor"), ethnicity = structure(c(1L, 1L, 2L
), .Label = c("dutch", "german"), class = "factor"), x2015 = c(112L, 
114L, 102L), x2016 = c(117L, 118L, 101L), x2017 = c(116L, 120L, 
99L)), class = "data.frame", row.names = c(NA, -3L))

df <- melt(data)

ggplot(df, aes(x = ethnicity, y = value, fill = variable)) +
  geom_bar(stat = "identity") +
  facet_wrap(~ sex)

Output: enter image description here

Artem
  • 3,304
  • 3
  • 18
  • 41