我有两个 ggplots。第一个看起来像这样:
ggplot(nurse, aes(x = nurse$z2.bk, y = nurse$z1.bk, color = nurse$phoneme)) +
geom_point() +
scale_x_reverse() + scale_y_reverse() +
scale_color_discrete() +
theme_classic()
然后我创建了一个子集,用于计算每个音素类别的 z1.bk 平均值和 z2.bk。mean_F1 = z1.bk 平均值,mean_F2 = z1.bk 平均值。
vowel mean_F1 mean_F2
<fct> <dbl> <dbl>
1 Er 0.00830 0.612
2 Ir -0.0433 0.0456
3 Vr 0.0365 -0.576
然后我为这些值创建了另一个 ggplot(如下)并根据nurse$phoneme 值标记它们。我只是在此处将它们重命名为元音,以使所有内容都更简洁。
ggplot(means, aes(x = mean_F2, y = mean_F1, label = vowel)) +
geom_label() +
scale_x_reverse() + scale_y_reverse() +
theme_classic()
我现在想覆盖它们,以便标签显示在相应颜色的其他点上方,即红色的 Er.... 我尝试了以下操作,但收到一条错误消息。
ggplot(nurse, aes(x = nurse$z2.bk, y = nurse$z1.bk, color = nurse$phoneme, label = means$vowel)) +
geom_point() +
geom_label(data = means, aes(x = mean_F2, y = mean_F1)) +
scale_x_reverse() + scale_y_reverse() +
theme_classic()
Error: Aesthetics must be either length 1 or the same as the data (563): label
If I change 'label = means$vowel' to just 'vowel', I get another error message saying the object can't be found. If I change it to nurse$phoneme, I get this error message Error: Aesthetics must be either length 1 or the same as the data (3): colour, label
.
How do I combine them properly? If I need to supply you with more data, just let me know. And thanks in advance!
First, it's a bit of bad form to use the $
convention to call columns in ggplot2
, where you should simply give the name of the column in the dataset: thus nurse$z2.bk
becomes simply z2.bk
in the aes()
call. With that being said, you can use it and it should still work... it's just frowned upon. :)
现在,对于您收到的错误消息 - 这是因为label=
在您的ggplot()
调用中指示了美学为means$vowel
,但在数据集中nurse
,有 563 个观察值。由于您有两个数据集分别应用于您的点和标签几何体,我会在aes()
每个几何体中说明它们。
没有你的完整数据集,我无法确认,但这应该在下面工作。另请注意,我为颜色图例指定了一个标签,因为使用不同名称调用单独数据集中的两列很可能会拆分图例。将图例的名称设置为相同(并在每个图例中具有相同的标签)应该将两个颜色图例保持在一起。
ggplot(nurse, aes(x = z2.bk, y = z1.bk, color = phoneme)) +
geom_point() +
geom_label(data = means, aes(x = mean_F2, y = mean_F1, label=vowel, color=vowel)) +
scale_x_reverse() + scale_y_reverse() +
labs(color='The colors') +
theme_classic()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句