【问题标题】:How to add customized labels to ggplot? [duplicate]如何将自定义标签添加到 ggplot? [复制]
【发布时间】:2021-09-19 00:45:50
【问题描述】:

我正在尝试将另一列(年份)的标签添加到我的 ggplot 图中,但它给了我以下错误:

Error: geom_text_repel requires the following missing aesthetics: label

我的数据:thirdgraph2

year      pdor         juni
2016      1991-06-26   13305.0
2019      1991-06-16   13598.0
2017      1991-06-17   13944.5
2018      1991-06-17   15653.5
2015      1991-07-08   17143.0

这是我使用的代码:

  ggplot(thirdgraph2, aes(juni, pdor, label=rownames(thirdgraph2$year))) + 
         geom_point() + 
         theme_bw() + 
         labs(y="Calculated date of reproduction", 
         x="Accumulated GDD until 17th June") + 
         geom_text_repel()

所以我喜欢标签是年份而不是默认的 1-5。有人知道方法吗?

【问题讨论】:

    标签: r ggplot2 label geom-text


    【解决方案1】:

    这行得通吗?:

    library(tibble)
    library(ggplot2)
    library(ggrepel)
    library(dplyr)
    
    
    ggplot(thirdgraph2, aes(juni, pdor, label=year)) + 
      geom_point() + 
      theme_bw() + 
      labs(y="Calculated date of reproduction", 
           x="Accumulated GDD until 17th June") + 
      geom_text_repel()
    

    reprex package (v2.0.0) 于 2021-07-08 创建

    数据

    thirdgraph2 <- tribble(
      ~"year",      ~"pdor",      ~"juni",
    2016,      "1991-06-26", 13305.0,
    2019,      "1991-06-16", 13598.0,
    2017,      "1991-06-17", 13944.5,
    2018,      "1991-06-17", 15653.5,
    2015,      "1991-07-08", 17143.0)
    
    #assuming you want the dates as dates: 
    
    thirdgraph2 <- 
      thirdgraph2 %>% 
      mutate(pdor = as.Date(pdor, format = "%Y-%m-%d"))
    
    

    【讨论】:

    • 这很有帮助!出于某种原因,我被困在使用“行名”,但这个简单的解决方案修复了它。非常感谢
    • 我很高兴,如果这回答了您的问题,习惯上接受并投票赞成答案;假设这是可能的。
    • 不幸的是我还没有足够的积分来接受和投票:(
    • 我觉得你总是可以接受的,你可能需要点赞!
    猜你喜欢
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    相关资源
    最近更新 更多