简体   繁体   English

不同长度线系列的标签

[英]Labels for line series of different lengths

I do have a problem with one of my charts, where there are four data sets, with three of the same length and one dataset that is a month longer;我的一张图表确实有问题,其中有四个数据集,三个长度相同,一个数据集长一个月; only the longest data set shows the appropriate label at the end of that particular line.只有最长的数据集在该特定行的末尾显示适当的 label。

I'm trying to get all four labels related to each line series to shown on the chart, but I can only get the label for the longest series.我试图让与每个线系列相关的所有四个标签都显示在图表上,但我只能获得最长系列的 label。 Please any thoughts and ideas would be greatly appreciated!请任何想法和想法将不胜感激!

I show the code below and the chart output我显示下面的代码和图表 output

library(GetBCBData)
library(ggplot2)
library(dplyr)
library(ggrepel)
# set ids
id.series <- c(ICC_sprd_total = 27443,
               ICC_sprd_corps = 27444,
               ICC_sprd_indivs = 27445,
               SELIC = 4189)
first.date = '2013-01-01'

# get series from bcb
df_cred <- gbcbd_get_series(id = id.series,
                            first.date = first.date,
                            last.date = Sys.Date(), 
                            use.memoise = FALSE)
glimpse(df_cred)

p <- ggplot(df_cred, aes(x =ref.date, y = value, colour = series.name)) +
  geom_line() + 
  geom_label_repel(data = df_cred %>% 
                     slice(which.max(ref.date)),
                   aes(label = value),
                   nudge_x = 0.05,
                   show.legend = FALSE,
                   size = 4.5) +
  scale_y_continuous(limits = c(0,NA), expand = c(0,0)) +
  geom_hline(yintercept=0)

print(p)

在此处输入图像描述

The original code is identifying points whose ref.date match the latest ref.date in the data;原始代码是识别ref.date与数据中最新的ref.date匹配的点; what you want is the latest ref.date within each series , which you can get by grouping first.您想要的是每个系列中最新的ref.date ,您可以通过先分组获得。

...  
geom_label_repel(data = df_cred %>% 
                     group_by(series.name) %>%  # ADD THIS
                     slice(which.max(ref.date)),
...

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM