为什么 geom_bar 不拆分分类变量
Why is geom_bar not splitting categorical variables
对 R 编程和尝试创建条形图非常陌生,但是我的分类 x 变量仍然分组在一起,我得到一个没有任何可用信息的条形图。子样本如下:
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
我使用的代码是
ggplot(ngames, aes(x= 'New Name', y= 'Ld P Min')) + geom_bar(stat = "summary")
这给了我这个不正确的图表
bad graph。
我已经能够使用 iris 中的股票数据创建我正在寻找的示例图,但它似乎不适用于我的数据,尽管所有数据格式都与 iris 中的相同。 example of expected graph。该图的代码是
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
geom_bar(stat = "identity")
感谢您的帮助。
我不确定这就是全部,因为没有可重现的示例,但我马上看到的是:
1) ggplot 不需要在引号中包含变量名。如果变量名中有空格,请改用刻度线。此外,最好指定要定义的参数。所以将第一行更改为
ggplot(games, aes(x=`Player Name`, y=`Player Load Per Minute`));
2) 如上面的评论所述,您的 geom_bar
语句使用了不需要的参数。您收到的错误字面意思是 "ggplot does not know what stat = "summary"
or fun.y = 'mean'
means." 我建议您不要使用 YouTube 视频作为指南,而是使用 GGplot Reference.
假设你的 data.frame 是这样的(名字有空格是一场噩梦):
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")
最好将 "New Name" 转换为因子:
df[["New Name"]] = factor(df[["New Name"]])
然后,您可以将变量放在反引号内,例如 variable
:
ggplot(df,aes(x=`New Name`,y=`Ld P Min`)) + stat_summary(fun="mean",geom="bar")
对 R 编程和尝试创建条形图非常陌生,但是我的分类 x 变量仍然分组在一起,我得到一个没有任何可用信息的条形图。子样本如下:
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
我使用的代码是
ggplot(ngames, aes(x= 'New Name', y= 'Ld P Min')) + geom_bar(stat = "summary")
这给了我这个不正确的图表
bad graph。
我已经能够使用 iris 中的股票数据创建我正在寻找的示例图,但它似乎不适用于我的数据,尽管所有数据格式都与 iris 中的相同。 example of expected graph。该图的代码是
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
geom_bar(stat = "identity")
感谢您的帮助。
我不确定这就是全部,因为没有可重现的示例,但我马上看到的是:
1) ggplot 不需要在引号中包含变量名。如果变量名中有空格,请改用刻度线。此外,最好指定要定义的参数。所以将第一行更改为
ggplot(games, aes(x=`Player Name`, y=`Player Load Per Minute`));
2) 如上面的评论所述,您的 geom_bar
语句使用了不需要的参数。您收到的错误字面意思是 "ggplot does not know what stat = "summary"
or fun.y = 'mean'
means." 我建议您不要使用 YouTube 视频作为指南,而是使用 GGplot Reference.
假设你的 data.frame 是这样的(名字有空格是一场噩梦):
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")
最好将 "New Name" 转换为因子:
df[["New Name"]] = factor(df[["New Name"]])
然后,您可以将变量放在反引号内,例如 variable
:
ggplot(df,aes(x=`New Name`,y=`Ld P Min`)) + stat_summary(fun="mean",geom="bar")