0

Very new to R programming and trying to create a bar plot, however my categorical x variable remains grouped together and I get a single bar without any usable information. A subsample is as follows:

     New Name Tot Pl L Ld P Min
 1:        1   376.62     5.91
 2:        4   689.07     7.26
 3:        5   202.14     4.67
 4:        6   516.28     6.01
 5:        7   698.02     7.41
 6:        8   180.50     3.69
 7:        9   163.12     4.24
 8:       10   176.66     4.59
 9:       11   665.34     7.97
10:        2   584.28     6.18
11:        3   155.67     4.04
12:        1   137.28     3.65
13:        4   181.87     4.84
14:        5   152.95     4.07
15:        6   150.07     3.99
16:        7   156.41     4.16
17:        8   108.19     2.88
18:        9   162.76     4.33
19:       10   175.88     4.68
20:       11   160.88     4.28
21:        2   148.53     3.95
22:        3   155.28     4.13
23:        1    62.84    10.49
24:        4   145.73    10.85
25:        5    12.00     8.08
26:        6    68.25     8.92
27:        7   143.88    11.83
28:        8     0.12     0.56
29:        9     0.22     1.04
30:       10     0.39     1.80
31:       11   162.22    12.08
32:        2   115.77     8.62
33:        3     0.11     0.51

The code I am using is

ggplot(ngames, aes(x= 'New Name', y= 'Ld P Min')) + geom_bar(stat = "summary")

which gives me this incorrect graph

bad graph.

I have been able to create an example graph of what I'm looking for using stock data from iris, but it doesn't seem to work with my data despite all of data formats being identical to those in iris. example of expected graph. The code for this graph is

ggplot(iris, aes(x = Species, y = Sepal.Length)) +
  geom_bar(stat = "identity")

Thanks for the help.

Cra538
  • 19
  • 5
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 02 '20 at 15:04
  • Please take a look at [How to make a great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and [How to ask](https://stackoverflow.com/help/how-to-ask). We need an example of your data and a description of your desired output. – Martin Gal Jun 02 '20 at 15:06
  • Based on your code I suggest replacing `geom_bar(stat = "summary", fun.y = "mean")` with `geom_bar(stat="identity")`. – Martin Gal Jun 02 '20 at 15:07
  • Updated question with example data – Cra538 Jun 02 '20 at 21:36

2 Answers2

1

I'm not sure this is everything, because there is no reproducible example, but what I see right away is:

1) ggplot does not need to have variable names in quotes. If you have variable names with spaces in them, use tick marks instead. Also, it's better if you specify which parameters you are defining. So change your first line to

ggplot(games, aes(x=`Player Name`, y=`Player Load Per Minute`));

2) As mentioned in the comments above, your geom_bar statement is using parameters that don't need to be there. The error you are getting literally says "ggplot does not know what stat = "summary" or fun.y = 'mean' means." I would suggest, instead of using YouTube videos as your guide, use the GGplot Reference.

mmyoung77
  • 1,343
  • 3
  • 14
  • 22
  • Thanks for the feedback. I've updated the question with example data. Still running into the same problems even after your suggestions. – Cra538 Jun 02 '20 at 21:36
1

Let's say your data.frame is like this (it's a nightmare to have spaces for names):

structure(list(`New Name` = structure(c(1L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 2L, 3L, 1L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 2L, 
3L, 1L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 2L, 3L), .Label = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11"), class = "factor"), 
    `Tot Pl L` = c(376.62, 689.07, 202.14, 516.28, 698.02, 180.5, 
    163.12, 176.66, 665.34, 584.28, 155.67, 137.28, 181.87, 152.95, 
    150.07, 156.41, 108.19, 162.76, 175.88, 160.88, 148.53, 155.28, 
    62.84, 145.73, 12, 68.25, 143.88, 0.12, 0.22, 0.39, 162.22, 
    115.77, 0.11), `Ld P Min` = c(5.91, 7.26, 4.67, 6.01, 7.41, 
    3.69, 4.24, 4.59, 7.97, 6.18, 4.04, 3.65, 4.84, 4.07, 3.99, 
    4.16, 2.88, 4.33, 4.68, 4.28, 3.95, 4.13, 10.49, 10.85, 8.08, 
    8.92, 11.83, 0.56, 1.04, 1.8, 12.08, 8.62, 0.51)), row.names = c(NA, 
33L), class = "data.frame")

Best to convert the "New Name" to factor:

df[["New Name"]] = factor(df[["New Name"]])

Then, you can use put your variable inside the back tick, like variable :

ggplot(df,aes(x=`New Name`,y=`Ld P Min`)) + stat_summary(fun="mean",geom="bar")

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • Thank you for the feedback. In my original data set, I have converted the character strings to factors. It appears the back tick is the solution along with stat_summary. – Cra538 Jun 03 '20 at 11:17