0

Sorts different categories (stores) A-H into black shirts sold and non-black shirts sold:

1 (for A-H)
0.8600394
0.5191401
0.5200601
0.1275694
0.4371994
0.4352312
0.4994585
0.08854391



0 (for A-H)
0.6368297
0.4958072
0.4068714
0.3631703
0.3498011
0.2870286
0.4980114
0.05199099

In R: I would like to plot a graph, in which I show all categories on the x-axis, and for each I show 2 bars, one for black shirts mean, and one for non-black shirts mean. Anybody can help on how to do that please? Thank you!

To plot it from the step below for my data would work (this is from a different question answered)

dat <- data.frame(country=c('USA','Brazil','Ghana','England','Australia'), Stabbing=c(15,10,9,6,7), Accidents=c(20,25,21,28,15), Suicide=c(3,10,7,8,6))

dat.m <- melt(dat, id.vars='country').

I thought it might be more elegant to plot directly from the table.

Wizhi
  • 6,424
  • 4
  • 25
  • 47
Max J.
  • 3
  • 4
  • If you decide to plot using [`ggplot2`](https://ggplot2.tidyverse.org/reference/geom_bar.html) you will need to reflow the data into long format first. It is pretty straight forward to draw a barplot based on certain variables from then on. What have you tried so far? Can you provide your data in an easy-to-paste format (see [hints here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example))? – Roman Luštrik Oct 20 '18 at 10:37
  • I tried to do it without putting the data into long format first. I saw those versions dat <- data.frame(country=c('USA','Brazil','Ghana','England','Australia'), Stabbing=c(15,10,9,6,7), Accidents=c(20,25,21,28,15), Suicide=c(3,10,7,8,6)) dat.m <- melt(dat, id.vars='country'). To plot it from this step for my data would work (this is from a different question answered) – Max J. Oct 20 '18 at 10:50
  • Good start. Please edit that code into your original question. – Roman Luštrik Oct 20 '18 at 10:52
  • It does not let me post the picture anymore. But thank you for your help! – Max J. Oct 20 '18 at 10:59

1 Answers1

0

As the others have said, you should put your data in an easy to past format.

Anyway, here:

library(tidyverse)
dat <- data.frame(country=c('USA','Brazil','Ghana','England','Australia'),
Stabbing=c(15,10,9,6,7), Accidents=c(20,25,21,28,15), Suicide=c(3,10,7,8,6)) 
dat

Gather is used to get it in to a long format:

dat.m <- dat %>%   gather(crime, number, 2:4)
dat.m

Basic ggplot:

ggplot (dat.m, aes(x = country, y = number, fill = crime)) + geom_bar(stat ="identity")

In your question, you have mentioned means, but with this data, you don't have enough data to do it by both country and mean, but here is the code by country:

dat.m <- dat.m %>% 
  group_by(country) %>% 
  summarise(mean = mean(number))
dat.m

Then just change the variable in the ggplot.

william3031
  • 1,653
  • 1
  • 18
  • 39
  • Okay, then I will do it that way! Thank you for your help! Just FYI regarding the means - the numbers are the mean I conducted before from my data. – Max J. Oct 20 '18 at 12:43
  • If is what you are after, can you mark it as answered? Thanks. – william3031 Oct 20 '18 at 21:16
  • Sorry, forgot to do that in the rush yesterday. Just marked it, thank you for reminding me! – Max J. Oct 21 '18 at 11:43