0

I have two ggplots. The first 1 looks like this:

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()

enter image description here

I then created a subset which calculates the z1.bk averages and z2.bk for each of the phoneme categories. mean_F1 = the z1.bk average and mean_F2 = the z1.bk average.

  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 

I then created another ggplot (below) for these values and labelled them according to the nurse$phoneme values. I just renamed them here to vowels to keep everything a bit cleaner.

ggplot(means, aes(x = mean_F2, y = mean_F1, label = vowel)) +
  geom_label() + 
  scale_x_reverse() + scale_y_reverse() + 
  theme_classic()

enter image description here

I now wanted to overlay them, so that the labels are displayed above the other points in the corresponding colour, i.e. Er in red.... I tried the following but got an error message.

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!

2
  • 2
    You could try ggplot2::annotate() to avoid a second plot. Commented Jun 25, 2020 at 20:43
  • 1
    This is an answer to a question similar in spirit: stackoverflow.com/questions/62531891/… Commented Jun 25, 2020 at 20:52

1 Answer 1

1

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

Now, for the error message you are receiving - this is because the aesthetic for label= is indicated in your ggplot() call to be means$vowel, but in the dataset nurse, there are 563 observations. Since you have two datasets being applied separately to your point and label geoms, I would state them within the aes() for each geom.

Without your full dataset, I can't confirm, but this should work below. Note also that I'm indicating a label for the legend for color, because it is likely that calling the two columns in the separate datasets with different names could split the legend. Setting the name of the legend to be the same (and having the same labels in each) should keep the two color legends together.

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for the answer and the tips :)!!! It worked just find. However, if you could help me with another minor problem. I wanted to change the symbol of the points. I achieved that in my original plot by adding 'shape = phoneme' to the first aes() function. If I try to do that with the code you gave, it says object not found. Do you know what I have to change?
It should work to have shape=phoneme within the aes(). I would place it in the geom_point() call, since it's only used there (and it avoids "confusion" for ggplot among your two datasets): geom_point(aes(shape=phoneme)). However, that's definitely going to split your legend. You'll need to add shape='The colors' as a second argument to labs() and that should bring them together again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.