Hi,
Happy New Year.
I have created simple line graph from a dataframe of performance scores (actual and planned) in three years.
Ggplot doesn’t work in this type of data, hence I simply used “plot”.
My questions are as follows:
Is there a way to keep the x-axis dates as “years” only, from 2021-2023? I created year through a “sequence” because a vector of 2021-2023 makes it a character and not as date.
Without ggplot, I cannot create two geom_lines to plot the actual and planned scores. How can I work on this?
Your suggestions will be highly appreciated
Thank you.
Respectfully,
Echo
Actual <- c("77", "79", "83")
Planned <- c("54", "56", "58")
ANC <- data.frame(Actual, Planned)
year <- seq( as.Date("2021-01-01"), as.Date("2023-12-31"), by="+1 year")
plot(year, ANC$Actual, type = "o", col = "blue", xlab = "Year", ylab = "Performance")
grid()
legend("topright", legend = "Performance score", col = "blue", cex=0.8)
1 Like
Hello Echo,
I would approach this question as below:
# loading packages
library(tidyverse)
# creating fake data
data <- tribble(
~year, ~actual, ~planned,
"2021-01-01", 77, 54,
"2022-01-01", 79, 56,
"2023-01-01", 83, 58
) |>
# converting year to a date
mutate(year = ymd(year))
# shaping the data
long_data <- data |>
pivot_longer(
cols = c(actual, planned),
names_to = "measure",
values_to = "value"
)
# plotting data
long_data |>
ggplot(aes(x = year, y = value, colour = measure, group = measure)) +
geom_line() +
scale_x_date(date_breaks = "years", date_labels = "%Y") +
scale_y_continuous(breaks = scales::extended_breaks(), labels = scales::comma_format()) +
scale_color_viridis_d() +
labs(
x = "\nYear",
y = "Value\n",
colour = NULL
) +
theme_minimal()
Created on 2024-01-04 with reprex v2.0.2
Session info
sessionInfo()
#> R version 4.3.1 (2023-06-16)
#> Platform: x86_64-apple-darwin20 (64-bit)
#> Running under: macOS Ventura 13.6.3
#>
#> Matrix products: default
#> BLAS: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
#>
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#>
#> time zone: America/Toronto
#> tzcode source: internal
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4
#> [5] purrr_1.0.2 readr_2.1.4 tidyr_1.3.0 tibble_3.2.1
#> [9] ggplot2_3.4.4 tidyverse_2.0.0
#>
#> loaded via a namespace (and not attached):
#> [1] styler_1.10.2 utf8_1.2.4 generics_0.1.3 xml2_1.3.6
#> [5] stringi_1.8.3 hms_1.1.3 digest_0.6.33 magrittr_2.0.3
#> [9] evaluate_0.23 grid_4.3.1 timechange_0.2.0 fastmap_1.1.1
#> [13] R.oo_1.25.0 R.cache_0.16.0 R.utils_2.12.3 fansi_1.0.6
#> [17] viridisLite_0.4.2 scales_1.3.0 cli_3.6.2 rlang_1.1.2
#> [21] R.methodsS3_1.8.2 munsell_0.5.0 reprex_2.0.2 withr_2.5.2
#> [25] yaml_2.3.8 tools_4.3.1 tzdb_0.4.0 colorspace_2.1-0
#> [29] curl_5.2.0 vctrs_0.6.5 R6_2.5.1 lifecycle_1.0.4
#> [33] fs_1.6.3 pkgconfig_2.0.3 pillar_1.9.0 gtable_0.3.4
#> [37] glue_1.6.2 xfun_0.41 tidyselect_1.2.0 highr_0.10
#> [41] rstudioapi_0.15.0 knitr_1.45 farver_2.1.1 htmltools_0.5.7
#> [45] rmarkdown_2.25 labeling_0.4.3 compiler_4.3.1
All the best,
Tim
1 Like
machupovirus:
long_data |>
ggplot(aes(x = year, y = value, colour = measure, group = measure)) +
geom_line() +
scale_x_date(date_breaks = "years", date_labels = "%Y") +
scale_y_continuous(breaks = scales::extended_breaks(), labels = scales::comma_format()) +
scale_color_viridis_d() +
labs(
x = "\nYear",
y = "Value\n",
colour = NULL
) +
theme_minimal()
It worked, thank you so much (always).
Best,
Echo
1 Like