r - Add label at the end of lines - Stack Overflow

I am trying to add the name of the column at the end of lines in ggplot.Neither geom_text or geom_labe

I am trying to add the name of the column at the end of lines in ggplot. Neither geom_text or geom_label worked.

df %>% 
  filter(played < 39) %>% #filtering dataset for the season that was played 38 games
  filter(place==1) %>% # analysing the champions
  #create graph to show the champions 
  
  ggplot()+
  aes(x=season) +
  geom_line(aes(y=points), color = "blue") + 
  geom_line(aes(y=goals), color="red") +
  geom_line(aes(y=won), color = "purple") +
  geom_line(aes(y=loss), color = "orange") +
  geom_line(aes(y=draw)) +
  
  geom_label() +
    
  labs( title = "Brasileirão", 
        subtitle = "2006 - 2024"
    
  ) +
  theme(
    
  )

I am trying to add the name of the column at the end of lines in ggplot. Neither geom_text or geom_label worked.

df %>% 
  filter(played < 39) %>% #filtering dataset for the season that was played 38 games
  filter(place==1) %>% # analysing the champions
  #create graph to show the champions 
  
  ggplot()+
  aes(x=season) +
  geom_line(aes(y=points), color = "blue") + 
  geom_line(aes(y=goals), color="red") +
  geom_line(aes(y=won), color = "purple") +
  geom_line(aes(y=loss), color = "orange") +
  geom_line(aes(y=draw)) +
  
  geom_label() +
    
  labs( title = "Brasileirão", 
        subtitle = "2006 - 2024"
    
  ) +
  theme(
    
  )

Share asked Mar 4 at 11:37 Lucas BatistaLucas Batista 1431 gold badge1 silver badge7 bronze badges 2
  • 2 It would be easier to help you if you provide a minimal reproducible example including a snippet of your input data shared using dput() or using some fake data or using a built-in dataset. – stefan Commented Mar 4 at 11:49
  • 2 This said: A geom_label/text requires an x, y and label aesthetic whereas you provided only x. Also, to add a label you have to add a geom_label for each line and filter the data for the last season, e.g. something like geom_label( data = ~subset(.x, season == max(season)), aes(y = points, label = "points"), hjust = 0 ). For other options see Plot labels at ends of lines But overall a better approach would be to reshape your data to long and add the lines/labels with just one geom_line/label – stefan Commented Mar 4 at 11:58
Add a comment  | 

1 Answer 1

Reset to default 0

The dupe @stefan mentioned is perhaps adequate for the specific question, but I agree you'd be better served by first reshaping your data long to work more fluidly with ggplot2. Here's an example where I take some wide data and reshape it long. Let's look at one storm from dplyr::storms, grab a bunch of disparate numeric columns, pivot them longer, and just keep the non-NAs:

library(tidyverse)
my_storms <- storms |>
  filter(name == "Martin") |>
  select(-status) |>
  pivot_longer(lat:hurricane_force_diameter, names_to = "stat") |>
  mutate(ts = ymd_h(paste(year, month, day, hour))) |> 
  filter(!is.na(value))

Now we can plot, where we make a single geom_line to plot the various series, and a single geom_label which is filtered to just see the last entry per series (accommodating situations where series end at different times).

You can look at the ggrepel package if you'd like the labels to automatically shift to avoid overlaps.

my_storms |>
  ggplot(aes(ts, value, label = value, color = stat, group = stat)) +
  geom_line() +
  geom_label(data = ~filter(., ts == max(ts), .by = stat), hjust = 0) +
  scale_y_continuous(trans = scales::pseudo_log_trans()) +
  coord_cartesian(clip = "off")

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745046087a4608105.html

相关推荐

  • r - Add label at the end of lines - Stack Overflow

    I am trying to add the name of the column at the end of lines in ggplot.Neither geom_text or geom_labe

    8小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信