散点图在中间画点
Scatterplot draws points in the middle
我对 ggplot2 中的散点图有疑问。我不知道为什么所有的点都画在中心,告诉我我做错了什么。
ggplot(TD, aes(x="Goals total", y="Assists", group=Position)) +
geom_point(aes(shape=Position, color=Position))
> TD
# A tibble: 9 x 3
Position `Goals total` Assists
<fctr> <dbl> <dbl>
1 RCB 0 0
2 RCB 0 1
3 RCB 3 1
4 RB 0 0
5 RB 2 1
6 RB 0 0
7 CF 0 0
8 CF 1 0
9 CF 6 0
aes
不接受字符串。因此,当您将 x = "Goals total"
传递给 aes
时,它会将 x
的值全部视为 "Goals total"
(这就是为什么字符串 "Goals total"
实际上是一个绘图中 x-axis
上的刻度线而不是轴名称)。所以如果你想继续使用它,你可以这样做:
ggplot(TD, aes(x=`Goals total`, y=Assists, group=Position)) +
geom_point(aes(shape=Position, color=Position))
或者,您可以将所有字符串与 aes_string
:
一起使用
ggplot(TD, aes_string(x="`Goals total`", y="Assists", group="Position")) +
geom_point(aes(shape=Position, color=Position))
另外一个要考虑的想法是不要在变量名中使用空格。对于它们,您必须使用 ` 将它们作为表达式访问。
我对 ggplot2 中的散点图有疑问。我不知道为什么所有的点都画在中心,告诉我我做错了什么。
ggplot(TD, aes(x="Goals total", y="Assists", group=Position)) +
geom_point(aes(shape=Position, color=Position))
> TD
# A tibble: 9 x 3
Position `Goals total` Assists
<fctr> <dbl> <dbl>
1 RCB 0 0
2 RCB 0 1
3 RCB 3 1
4 RB 0 0
5 RB 2 1
6 RB 0 0
7 CF 0 0
8 CF 1 0
9 CF 6 0
aes
不接受字符串。因此,当您将 x = "Goals total"
传递给 aes
时,它会将 x
的值全部视为 "Goals total"
(这就是为什么字符串 "Goals total"
实际上是一个绘图中 x-axis
上的刻度线而不是轴名称)。所以如果你想继续使用它,你可以这样做:
ggplot(TD, aes(x=`Goals total`, y=Assists, group=Position)) +
geom_point(aes(shape=Position, color=Position))
或者,您可以将所有字符串与 aes_string
:
ggplot(TD, aes_string(x="`Goals total`", y="Assists", group="Position")) +
geom_point(aes(shape=Position, color=Position))
另外一个要考虑的想法是不要在变量名中使用空格。对于它们,您必须使用 ` 将它们作为表达式访问。