-1

I am an R neewbie, so apologies if my question is too basic or if I've violated a forum rule.

I have data in 2-columns. The second column contains the sex (Female-1 and male-2). Column 'a' (first column) contains answer responses to a question which range from (0,1,2,3,4).

How can I get a bar plot of the count of answers, grouped by sex. I am able to do this in excel but I haven't been able to do so in R. In the first instance, I'm unable to transform the data to be usable for plotting. I tried dplyr, etc.

a   Sex
1   1
0   2
4   1
2   2
0   1
1   1
2   1
3   1
3   1
2   1
4   2
4   1
2   1
1   2
0   1
2   2
3   1
0   1
4   1
2   1
0   1
1   1
2   2
2   1
1   2
1   2
1   1
0   1
1   1
3   
0   1
3   1
4   1
2   2
0   1
1   1
1   2
0   1
3   1
3   1
2   1
1   1
1   1
0   1
0   2
4   1
0   2
0   2
0   2
2   1
2   1
0   2
1   1
4   1
0   1
2   1
0   2
1   2
2   2
0   1
0   1
1   1
1   1
2   2
1   1
0   1
0   2
1   2
0   1
0   1
3   1
0   1
0   2
1   2
0   2
0   1
2   
4   2
0   1
1   1
2   2
1   1
4   2
1   1
4   2
0   1
4   2
0   1
2   2
2   1
4   1
2   2
1   1
1   1
2   1
2   1
1   1
3   2
1   1
1   1
2   1
0   2
3   2
2   1
0   1
1   2
2   2
0   2
3   2
2   1
0   1
1   1
1   2
1   1
1   1
0   2
1   
0   1
2   1
3   1
0   2
2   1
2   1
0   2
2   1
0   1
0   1
4   1
2   1
2   1
2   1
3   2
2   2
3   1
1   1
2   2
1   1
0   2
2   1
1   2
1   2
0   1
1   2
0   1
2   1
2   1
1   1
2   2
1   1
0   2
2   1
1   1
2   1
2   1
4   2
1   1
0   1
0   1
3   1
3   1
2   1
0   1
1   1
1   1
1   1
2   1
1   1
2   1
2   1
2   1
1   2
2   1
2   1
3   1
0   1
1   2
2   2
1   1
2   1
0   2
0   1
2   1
0   1
0   1
0   1
3   2
2   2
1   2
1   1
1   1
4   1
3   2
2   1
3   1
0   1
1   2
0   1
3   
2   2
1   1
3   2
1   2
1   1
2   2
2   2
1   2
0   1
2   2
1   1
2   2
0   2
2   2
0   1
0   1
3   2
3   2
1   3
2   1
0   1
1   2
2   1
2   2
4   2
2   2
2   1
0   2
1   2
1   1
0   2
3   1
3   1
2   1
2   1
2   2
1   1
0   1
2   1
0   1
4   1
0   2
0   1
3   1
1   2
0   1
0   1
0   2
2   1
2   2
1   2
2   1
4   1
2   2
2   1
2   1
1   2
3   1
0   1
1   1
1   1
4   1
0   2
3   
0   
0   2
2   1
3   2
1   2
1   2
1   2
0   1
1   2
0   1
3   1
3   2
3   1
0   1
0   1
2   1
1   1
1   1
1   1
2   1
3   1
3   1
2   2
3   1
1   2
1   1

Output I'm trying to achieve:

plot

AK16
  • 111
  • 5
  • Look at the first answer [here](https://stackoverflow.com/questions/18158461/grouped-bar-plot-in-ggplot) – Adamm Feb 08 '19 at 12:16

1 Answers1

1

Should get you started:

library(tidyverse)

df %>%
  count(Sex = ifelse(Sex == 1, "Female", "Male"), a) %>%
  ggplot(aes(x = a, y = n, fill = Sex)) +
  geom_bar(stat = 'identity', position = "dodge") +
  coord_flip()
arg0naut91
  • 14,574
  • 2
  • 17
  • 38
  • Thank you! the code works perfectly. However, when I want to plot other columns, say column 'b' (i.e. Same as above bar plot of the count of answers in column 'b', grouped by sex) and modify the code to read column 'b' (wherever there is now 'a') the output gets skewed (i.e. all bars plot to the maximum). What am I doing wrong, please? – AK16 Feb 08 '19 at 13:05
  • I changed y=nn in ggplot(aes(x = a, y = nn, fill = Sex)) and the command works for all columns – AK16 Feb 08 '19 at 14:38
  • Can you post an example of your `df` with other columns? `count` by definition summarises the data, so you may be losing observations with that, am not sure - there could be other approaches – arg0naut91 Feb 08 '19 at 14:39
  • Thank you for your response. The df is available at: https://www.dropbox.com/s/p3cd43dehlmz9d7/Analysis_Share.csv?dl=1 – AK16 Feb 08 '19 at 16:30
  • Get an error when accessing, can you copy paste just first few rows and all columns in your original post? – arg0naut91 Feb 08 '19 at 16:33
  • a b c d e f g h i j k l m n o p Sex\\ 1 3 1 4 1 1 1 1 1 1 4 1 3 3 4 2 1\\ 1 2 4 4 4 3 1 2 1 1 1 1 1 1 4 1 1 1 1\\ – AK16 Feb 08 '19 at 18:26