geom_text удаление других геометрий (geom_point и geom_line) - PullRequest
0 голосов
/ 06 августа 2020

У меня странное поведение с набором данных, с которым я работаю. Я рисую слой точек, а затем слой линий, а затем ищу подмножество меток. Но метки удаляют слои линий и точек. Я не могу воспроизвести проблему в другом наборе данных и не могу разобраться в том, что происходит.

Вот код, который вызывает проблему:

#Have a look at the data:
>head(df)
# A tibble: 6 x 5
  p.start p.end  year  value change
    <dbl> <dbl> <int>  <dbl>  <dbl>
1       0     1  2017 -4928.  1144.
2       1     2  2017     0   -798.
3       2     3  2017   687. -3400.
4       3     4  2017  3438. -3063.
5       4     5  2017  5978. -2400.
6       5     6  2017  7988. -2023.

>tail(df)
# A tibble: 6 x 5
  p.start p.end  year    value  change
    <dbl> <dbl> <int>    <dbl>   <dbl>
1    96      97  2017  208759.  23221.
2    97      98  2017  248919.  28333.
3    98      99  2017  334171.  36818.
4    99     100  2017 1104671. 113244.
5    99.5   100  2017 1709530. 174812.
6    99.9   100  2017 4847340. 504441 

#Create the relevant plot
library('ggplot2')
ggplot(df, aes(x = p.start, y = change)) +
  geom_point() + #plot the points and lines
  geom_line() + 
  geom_text(data = subset(df, p.start >= 99), #plot a subset labels
            aes(x = p.start, y = value, label = p.start))

enter image description here

You can see the geom_text() removing the geom_point() and geom_line(). I tried 'nudge_x' but that didn't make a difference. I also tried plotting the layers in a different order by that didn't make a difference.

I try and do this with a different dataset and it works:

library('tibble')
#set rownames to use as labels
df2 <- mtcars %>% 
  rownames_to_column('name')

#plot
ggplot(df2, aes(x = mpg, y = hp)) + 
  geom_point() + #plot the points and lines
  geom_line() + 
  geom_text(data = subset(df, cyl > 4), #plot a subset of labels
            aes(x = mpg, y = hp, label = name))

введите описание изображения здесь

Все работает нормально: над точками и линиями появляются метки.

Что не так с моим первым графиком / данными, которые вызывают эту проблему?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...