0

In R, using Tidyverse and exporting a Ggplot2 figure, how should a database be structured for achieving a figure like this

?

It is something like a count the days until reaching a objetive.

Does it need to be time formatted?

How should be my line of thinking? Thank you

divibisan
  • 11,659
  • 11
  • 40
  • 58
Tiago
  • 21
  • 4
  • Does this answer your question? [Horizontal Barplot in ggplot2](https://stackoverflow.com/questions/10941225/horizontal-barplot-in-ggplot2) – divibisan Jul 20 '23 at 16:52
  • 1
    This is a highly stylized figure, but at the core is just a bar chart. `data.frame(co = c("Threads", "ChatGPT"), time_hrs = c(1, 5*24), time_label = c("1 hour", "5 days")) |> ggplot(aes(time_hrs, co, label = time_label)) + geom_col() + geom_text(hjust = -0.1)` is a start. – Jon Spring Jul 20 '23 at 16:52
  • 1
    This is just a simple barplot. Can you explain what you've tried and clarify what your specific problem is? – divibisan Jul 20 '23 at 16:53
  • My obstacle is in the conversion of time... – Tiago Jul 20 '23 at 17:01

1 Answers1

1

Here is a possible example:

library(dplyr)
library(ggplot2)

df <- structure(list(Months = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                              1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                              1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
                     Software = c(rep("Twitter", 10),
                                  rep("Facebook", 10),
                                  rep("Chat GPT", 10)),
                     Users = c(50, 10, 200, 400, 800, 160, 3200, 6400, 1280, 2000,
                                          4000, 800, 1600, 3200, 6400, 128000, 2560, 5120, 102400, 150,
                                          6000, 1200, 2400, 400, 960, 19200, 3800, 7680, 1000, 20)),
                class = "data.frame", row.names = c(NA, -30L),
                .Names = c("Months", "Software", "Users"))



df %>%
  mutate(Cumulative_Users = cumsum(Users)) %>% 
  group_by(Software) %>%
  filter(Cumulative_Users >= 10000) %>%
  summarise(Months_to_10000= min(Months)) %>% 
  ggplot(aes(x = Months_to_10000, y = reorder(Software, Months_to_10000))) +
  geom_col() +
  labs(x = "Months to reach 10000 users", y = "Software") +
  geom_text(aes(label = paste(round(Months_to_10000, 1), "months")), hjust = -0.1)+
  theme_minimal()

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66