Tomikichi
Tomikichi

Reputation: 47

R; ggplot2: Overlaying 1 plot with another

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!

Upvotes: 0

Views: 76

Answers (1)

chemdork123
chemdork123

Reputation: 13793

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

Upvotes: 1

Related Questions