Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a geom_line plot where as the numeric value on x-axis increased, the y-axis shows a cumulative count

I have the following data set:

df <- data.frame(
  Passwords = c("123er", "234f4", "234r4", "234f6", "234f8","274f4","294f4",
                "204f4","23yf4","2l4f4","2u4f4","23of4","734f4","834f4",
                "934f4","234w4","267f4"),
  Days_Elapsed = c(1,1,1,4,4,5,5,5,7,7,7,7,8,8,8,9,9))

The objective is to create a geom-line plot that shows an ever upwards line corresponding to the number of passwords registered as the Days-Elapsed variable increases. For example, when Days_Elapsed equals 1, there are three passwords, meaning that, in the plot, on the x-axis value for 1, there would be a y-value of 3. This value of 3 would remain consistent for Days_Elapsed 2 and 3, as no passwords were registered on these days (meaning the line would be perfectly horizontal). This line would then increase to 5 as the Days-Elapsed reach the value of 4 (because two more passwords were registered on day 4). On day 5 there are three more passwords, so the line in Y would increase to the value 8, and so on.

I reckon this is fairly simple but I just cannot find a way to get it done. Any help would be appreciated. Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

it seems the function you were looking for is geom_step() in ggplot2. But to use it you must transform your data a bit:

  1. To get the count of passwords by days elapsed
  2. To get the cumulative number of days
library(dplyr)
library(ggplot2)
df <- data.frame(
  Passwords = c("123er", "234f4", "234r4", "234f6", "234f8","274f4","294f4",
                "204f4","23yf4","2l4f4","2u4f4","23of4","734f4","834f4",
                "934f4","234w4","267f4"),
  Days_Elapsed = c(1,1,1,4,4,5,5,5,7,7,7,7,8,8,8,9,9))

df |> count(Days_Elapsed) |> # for 1.
  mutate(
    n_cumul = cumsum(n) # for 2.
  ) |> 
  ggplot(aes(x = Days_Elapsed, y = n_cumul )) +
  geom_step()

plot from the code above

Hope that helps ; )

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading