如何仅显示与 r 折线图中前 4 个最高值相对应的日期

how to show only the dates that correspond to the top 4 highest values in a line chart in r

我有这个 df 并且我已经创建了一个线图。我试图只绘制具有 4 个最高值的日期。

dput(df)
structure(list(Date = c("2021-06-22", "2021-06-23", "2021-06-24", 
"2021-06-25", "2021-06-26", "2021-06-27", "2021-06-28", "2021-06-29", 
"2021-06-30", "2021-07-01", "2021-07-02", "2021-07-03", "2021-07-04", 
"2021-07-05", "2021-07-06", "2021-07-07", "2021-07-08", "2021-07-09", 
"2021-07-10", "2021-07-11", "2021-07-12", "2021-07-13", "2021-07-14", 
"2021-07-15", "2021-07-16", "2021-07-17", "2021-07-18", "2021-07-19", 
"2021-07-20", "2021-07-21"), Volume = c(30L, 30L, 30L, 30L, 30L, 
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
27L, 27L, 27L, 27L, 27L, 27L, 27L, 30L, 30L, 30L, 31L, 30L), 
    Percentage = c(6.67, 3.33, 10, 10, 3.33, 3.33, 6.67, 10, 
    3.33, 6.67, 10, 13.33, 13.33, 10, 6.67, 6.67, 10, 10, 3.7, 
    3.7, 3.7, 3.7, 11.11, 7.41, 11.11, 10, 13.33, 13.33, 12.9, 
    10)), row.names = c(NA, -30L), class = c("tbl_df", "tbl", 
"data.frame"))

这是折线图

ggplot(data = df, aes(x = Date, y = Percentage,group=1)) +       
  geom_line() + geom_point()

percentage 值相互关联,因此我选择了 4 个随机值,但从概念上讲,您可以使用 scale_x_continuous 指定日期 labelsbreaks 来执行此操作您要显示的内容。

library(ggplot2)
library(dplyr)

data <- summary_final_by_date %>%
  mutate(label = as.Date(ifelse(dense_rank(-Percentage) %in% 1:4, Date, '')), 
         Date  = as.Date(Date))

unique_dates <- unique(na.omit(data$label))[c(1, 4, 10, 16)]

ggplot(data, aes(x = as.numeric(Date), y = Percentage)) +       
  geom_line() + geom_point() + 
  scale_x_continuous(breaks = as.numeric(unique_dates), 
                     labels = format(unique_dates, format = "%d %b")) + 
  xlab('Date')