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
|
1 Answer
Reset to default 0The 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
dput()
or using some fake data or using a built-in dataset. – stefan Commented Mar 4 at 11:49geom_label/text
requires anx
,y
andlabel
aesthetic whereas you provided onlyx
. Also, to add a label you have to add ageom_label
for each line and filter the data for the last season, e.g. something likegeom_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 onegeom_line/label
– stefan Commented Mar 4 at 11:58