r - 如何在 geom_smooth 之后而不是在 geom_line 之后显示直接标签?

标签 r ggplot2 direct-labels

我正在使用直接标签来注释我的情节。正如你在这张图片中看到的,标签在 geom_line 之后,但我想要在 geom_smooth 之后。这是否受直接标签支持?或任何其他想法如何实现这一目标?提前致谢!

enter image description here

这是我的代码:

library(ggplot2)
library(directlabels)

set.seed(124234345)

# Generate data
df.2 <- data.frame("n_gram" = c("word1"),
                   "year" = rep(100:199),
                   "match_count" = runif(100 ,min = 1000 , max = 2000))

df.2 <- rbind(df.2, data.frame("n_gram" = c("word2"),
                      "year" = rep(100:199),
                      "match_count" = runif(100 ,min = 1000 , max = 2000)) )

# plot
ggplot(df.2, aes(year, match_count, group=n_gram, color=n_gram)) +
  geom_line(alpha = I(7/10), color="grey", show_guide=F) +
  stat_smooth(size=2, span=0.3, se=F, show_guide=F) +
  geom_dl(aes(label=n_gram), method = "last.bumpup", show_guide=F) +
  xlim(c(100,220))

最佳答案

这个答案采用了@celt-Ail 答案的基本概念,而不是函数、基础 R 和直接标签,而是尝试一种 tidyverse 方法,从 here 窃取一些代码为多个 loess楷模。

很高兴听到建议的改进。

set.seed(124234345)

# Generate data
df.2 <- data.frame("n_gram" = c("word1"),
                   "year" = rep(100:199),
                   "match_count" = runif(100 ,min = 1000 , max = 2000))

df.2 <- rbind(df.2, data.frame("n_gram" = c("word2"),
                               "year" = rep(100:199),
                               "match_count" = runif(100 ,min = 1000 , max = 2000)) )

#example of loess for multiple models
#https://stackoverflow.com/a/55127487/4927395
library(dplyr)
library(tidyr)
library(purrr)
library(ggplot2)

models <- df.2 %>%
  tidyr::nest(-n_gram) %>%
  dplyr::mutate(
    # Perform loess calculation on each CpG group
    m = purrr::map(data, loess,
                   formula = match_count ~ year, span = .3),
    # Retrieve the fitted values from each model
    fitted = purrr::map(m, `[[`, "fitted")
  )

# Apply fitted y's as a new column
results <- models %>%
  dplyr::select(-m) %>%
  tidyr::unnest()

#find final x values for each group
my_last_points <- results %>% group_by(n_gram) %>% summarise(year = max(year, na.rm=TRUE))

#Join dataframe of predictions to group labels
my_last_points$pred_y <- left_join(my_last_points, results)

# Plot with loess line for each group
ggplot(results, aes(x = year, y = match_count, group = n_gram, colour = n_gram)) +
  geom_line(alpha = I(7/10), color="grey", show.legend=F) +
  #stat_smooth(size=2, span=0.3, se=F, show_guide=F)
  geom_point() +
  geom_line(aes(y = fitted))+  
  geom_text(data = my_last_points, aes(x=year+5, y=pred_y$fitted, label = n_gram))

direct_label

关于r - 如何在 geom_smooth 之后而不是在 geom_line 之后显示直接标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10065196/

相关文章:

python - 寻根函数中不一致的参数错误

javascript - 如何将自定义悬停功能添加到 plotOutput 以便它可以用于许多绘图

r - 图例 ggplot 图不显示均值、误差线和彩色矩形

r - 使用 ggplot 或 R 中的任何其他方法根据计数绘制线宽(大小)

r - 在交替索引上拆分字符串

用一个因素替换另一个因素(在数据框内)

r - ggplot 主题格式可以保存为对象吗?

r - 如何向 ggplot geom_text 标签添加百分比和分数?

r - 增加 geom_line 图的标签间距

r - Directlabels包装-标签不适合绘图区域