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
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.
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")