Calendar plot with geom_pointrange()

  Kiến thức lập trình

I have the following dataset in R. I aim to do a ggplot where the scale goes from 1 to 12 (January, February, …, December) in the x-axis, and the y-axis goes from 1 to 6( num_months variable). Then, I want to use geom_pointrage, with the minimum being start_month_year and the maximum being end_month_year (so they represent the num_months). I want to facet horizontally by the variable year.

My main problem concerns the x-axis; in this dataset, 1 is “Dec-2020”, 2 is “Jan-2021”, and so on until 31, which is “Jun-2023”. Neither start_month_year nor end_month_year starts at 1. But I want each facet (for the year) to start in January and finish in December.

Here’s how the start_month_year and end_month_year are coded. Then, the dataset. Last, my code so far.

  mutate(start_month_year = case_when(
           month_aux == 12 & year_aux == 2020 ~ 1,
           month_aux == 1  & year_aux == 2021 ~ 2,
           month_aux == 2  & year_aux == 2021 ~ 3,
           month_aux == 3  & year_aux == 2021 ~ 4,
           month_aux == 4  & year_aux == 2021 ~ 5,
           month_aux == 5  & year_aux == 2021 ~ 6,
           month_aux == 6  & year_aux == 2021 ~ 7,
           month_aux == 7  & year_aux == 2021 ~ 8,
           month_aux == 8  & year_aux == 2021 ~ 9,
           month_aux == 9  & year_aux == 2021 ~ 10,
           month_aux == 10 & year_aux == 2021 ~ 11,
           month_aux == 11 & year_aux == 2021 ~ 12,
           month_aux == 12 & year_aux == 2021 ~ 13,
           month_aux == 1  & year_aux == 2022 ~ 14,
           month_aux == 2  & year_aux == 2022 ~ 15,
           month_aux == 3  & year_aux == 2022 ~ 16,
           month_aux == 4  & year_aux == 2022 ~ 17,
           month_aux == 5  & year_aux == 2022 ~ 18,
           month_aux == 6  & year_aux == 2022 ~ 19,
           month_aux == 7  & year_aux == 2022 ~ 20,
           month_aux == 8  & year_aux == 2022 ~ 21,
           month_aux == 9  & year_aux == 2022 ~ 22,
           month_aux == 10 & year_aux == 2022 ~ 23,
           month_aux == 11 & year_aux == 2022 ~ 24,
           month_aux == 12 & year_aux == 2022 ~ 25,
           month_aux == 1  & year_aux == 2023 ~ 26,
           month_aux == 2  & year_aux == 2023 ~ 27,
           month_aux == 3  & year_aux == 2023 ~ 28,
           month_aux == 4  & year_aux == 2023 ~ 29,
           month_aux == 5  & year_aux == 2023 ~ 30,
           month_aux == 6  & year_aux == 2023 ~ 31))

data <- read_csv("country,num_months,start_month_year,end_month_year,B1,B1_p,year
2,1,6,6,3.3571016788482666,0.007681768853217363,2021
2,1,8,8,2.548985481262207,0.007373321335762739,2021
2,1,10,10,2.139772415161133,0.03452971577644348,2021
2,1,12,12,2.165775775909424,0.07796278595924377,2021
2,1,13,13,1.9506219625473022,0.09215697646141052,2021
2,1,23,23,2.7839596271514893,0.011407249607145786,2022
2,1,25,25,2.220555543899536,0.06181173026561737,2022
2,2,10,11,1.4871771335601807,0.06038494035601616,2021
2,2,12,13,2.0390524864196777,0.04265233874320984,2021
2,2,13,14,1.417211651802063,0.09348150342702866,2021
2,2,22,23,1.874699354171753,0.018470995128154755,2022
2,2,23,24,1.6757967472076416,0.028078539296984673,2022
2,3,6,8,1.334544062614441,0.05757715925574303,2021
2,3,8,10,1.2151446342468262,0.05723109096288681,2021
2,3,10,12,1.7102551460266113,0.03582938387989998,2021
2,3,11,13,1.6229373216629028,0.07926513999700546,2021
2,3,12,14,1.6802176237106323,0.04918825626373291,2021
2,3,13,15,1.417211651802063,0.09348150342702866,2021
2,3,22,24,1.3854548931121826,0.033461399376392365,2022
2,3,23,25,1.8822635412216187,0.01415294036269188,2022
2,4,8,11,1.1319600343704224,0.06593644618988037,2021
2,4,10,13,1.754726767539978,0.02880094014108181,2021
2,4,11,14,1.4533793926239014,0.08008415251970291,2021
2,4,12,15,1.6802176237106323,0.04918825626373291,2021
2,4,22,25,1.6274609565734863,0.011329303495585918,2022
2,5,6,10,1.0146392583847046,0.08948446065187454,2021
2,5,8,12,1.338212251663208,0.04161843657493591,2021
2,5,9,13,1.2212527990341187,0.0919371098279953,2021
2,5,10,14,1.5834165811538696,0.03494146466255188,2021
2,5,11,15,1.4533793926239014,0.08008415251970291,2021
2,5,12,16,1.4284441471099854,0.0697392150759697,2021
2,5,21,25,1.2466250658035278,0.035289958119392395,2022
2,6,6,11,0.9881601333618164,0.08719704300165176,2021
2,6,8,13,1.438501238822937,0.032221969217061996,2021
2,6,9,14,1.16400945186615,0.09187468141317368,2021
2,6,10,15,1.5834165811538696,0.03494146466255188,2021
2,6,11,16,1.294316291809082,0.09792502969503403,2021
2,6,12,17,1.4204859733581543,0.0546354204416275,2021
2,6,20,25,1.07038414478302,0.0722803920507431,2022
3,1,11,11,2.499434471130371,0.08386286348104477,2021
3,1,12,12,-5.246932506561279,0.06465034931898117,2021
3,1,15,15,2.3472023010253906,0.0787404328584671,2022
3,2,15,16,2.3472023010253906,0.0787404328584671,2022
3,3,15,17,2.195162296295166,0.053296979516744614,2022
3,4,15,18,2.195162296295166,0.053296979516744614,2022
3,5,11,15,1.7339816093444824,0.06338241696357727,2021
3,5,13,17,1.7100595235824585,0.08902979642152786,2021
3,5,15,19,1.9401909112930298,0.07938205450773239,2022
3,6,11,16,1.7339816093444824,0.06338241696357727,2021
3,6,13,18,1.7100595235824585,0.08902979642152786,2021
3,6,15,20,1.9401909112930298,0.07938205450773239,2022")

ggplot(data, aes(x = num_months, ymin = start_month_year, ymax = end_month_year)) +
  geom_pointrange(aes(y = num_months, color = as.factor(num_months)), position = position_dodge(width = 0.5)) +
  scale_x_continuous(breaks = 1:31, limits = c(1, 31)) +
  scale_y_continuous(breaks = 1:6, limits = c(1, 6)) +
  facet_wrap(~ year, nrow = 1) +
  labs(x = "Date", y = "Number of Months", color = "Number of Months") +
  theme_minimal()

My ggplot is not working so far. I also want the geom_pointrange to be the size of B1.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT