My data looks like this:
freight_rate
date LR2_115000dwt LR1_74000dwt MR_50000dwt
1 2020-04-03 21125 17000 16875
2 2021-04-02 21125 16125 15625
3 2022-04-01 21625 16125 16500
4 2019-08-02 19125 15375 16750
5 2020-08-07 24750 16500 16875
6 2021-08-06 21625 16125 16375
7 2022-08-05 23500 16625 20500
8 2019-12-06 21500 17250 17375
9 2020-12-04 21750 16125 16250
10 2021-12-03 21625 16125 16375
11 2020-02-07 21125 17250 16875
12 2021-02-05 21125 16125 15375
13 2022-02-04 21625 16125 16375
14 2020-01-03 21500 17250 17375
15 2021-01-01 21500 16125 15625
16 2022-01-07 21625 16125 16375
17 2019-07-05 19125 15375 16750
18 2020-07-03 24750 16500 16875
My code:
freight_rate %>%
ggplot(aes(x = freight_rate$date))
geom_line(aes(y = freight_rate$LR2_115000dwt), color = "darkred")
geom_line(aes(y = freight_rate$LR1_74000dwt), color = "steelblue")
geom_line(aes(y = freight_rate$MR_50000dwt), color = "green")
I want to have values on all of my line plots like this (the values on the line):
How can I do this with the given code? I am sorry for bad way of posting because I am new.
CodePudding user response:
Rather than doing one layer for each series you wish to plot, it would be far easier to pivot your data into long format and plot your layers altogether.
Whatever way to do it, you are going to end up with a very messy plot, since you have three series on a single panel, each with several points, and each point with a long number attached to it. This can be mitigated a bit by using geom_text_repel
library(tidyverse)
library(ggrepel)
freight_rate %>%
pivot_longer(-date) %>%
ggplot(aes(date, value, color = name))
geom_line(alpha = 0.5)
geom_point()
geom_text_repel(aes(label = scales::comma(value)), size = 3)
scale_color_manual(values = c('steelblue', 'red4', 'green4'), name = NULL)
scale_y_continuous(labels = scales::dollar, name = '$/day')
labs(x = 'Date')
You can make this a bit clearer by using facets to separate the series:
freight_rate %>%
pivot_longer(-date) %>%
ggplot(aes(date, value, color = name))
geom_line(alpha = 0.5)
geom_point()
geom_text_repel(aes(label = scales::comma(value)), size = 3)
scale_color_manual(values = c('steelblue', 'red4', 'green4'), name = NULL)
scale_y_continuous(labels = scales::dollar, name = '$/day')
labs(x = 'Date')
theme_minimal()
facet_grid(name ~ ., scales = 'free_y')
theme(panel.border = element_rect(fill = NA),
strip.text = element_blank(),
panel.spacing.y = unit(5, 'mm'))
Personally, I think the labels are just distracting here and the plot looks better without; I would trust my viewers to be able to read the data from the axes. Otherwise, why not just show a table?
freight_rate %>%
pivot_longer(-date) %>%
ggplot(aes(date, value, color = name))
geom_line(alpha = 0.7, size = 1)
geom_point(size = 3)
scale_color_manual(values = c('steelblue', 'red4', 'green4'), name = NULL)
scale_y_continuous(labels = scales::dollar, name = '$/day')
labs(x = 'Date')
theme_minimal(base_size = 16)