Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcats reordering not working for ggplot

Tags:

r

forcats

I have the following code for plotting a simple lollipop chart for my data:

p <-
  data %>%   mutate(Activity_Name = fct_reorder(Activity_Name, count)) %>%
  ggplot(aes(x = Activity_Name, y = count)) +
  geom_segment(aes(
    x = Activity_Name,
    xend = Activity_Name,
    y = 0,
    yend = count
  ),
  color = "skyblue") +
  geom_point(color = "#F2F7F2",
             size = 5,
             alpha = 0.6) +
  theme_light() +
  scale_y_log10() +
  coord_flip()

I would like to order this in descending order of Activity Name's counts. Even though I've included that in the code, the below code produces the following graph:

[![enter image description here][1]][1]

What am I doing wrong and why is this failing? My dput is shared below:

Dput here

structure(list(Day = c("01-01-2021", "01-01-2021", "01-01-2021", 
"01-01-2021", "01-01-2021", "01-01-2021", "01-01-2021", "01-01-2021", 
"01-01-2021", "01-01-2021", "01-01-2021", "01-01-2021", "01-01-2021", 
"01-01-2021", "01-01-2021", "01-01-2021", "01-01-2021", "01-01-2021", 
"01-01-2021", "01-01-2021", "01-01-2021", "01-01-2021", "01-01-2021", 
"01-01-2021", "01-02-2021", "01-02-2021", "01-02-2021", "01-02-2021", 
"01-02-2021", "01-02-2021", "01-02-2021", "01-02-2021", "01-02-2021", 
"01-02-2021", "01-02-2021", "01-02-2021", "01-02-2021", "01-02-2021", 
"01-02-2021", "01-02-2021", "01-02-2021", "01-02-2021", "01-02-2021", 
"01-02-2021", "01-02-2021", "01-02-2021", "01-02-2021", "01-02-2021", 
"01-03-2021", "01-03-2021", "01-03-2021", "01-03-2021", "01-03-2021", 
"01-03-2021", "01-03-2021", "01-03-2021", "01-03-2021", "01-03-2021", 
"01-03-2021", "01-03-2021", "01-03-2021", "01-03-2021", "01-03-2021", 
"01-03-2021", "01-03-2021", "01-03-2021", "01-03-2021", "01-03-2021", 
"01-03-2021", "01-03-2021", "01-03-2021", "01-03-2021", "01-04-2021", 
"01-04-2021", "01-04-2021", "01-04-2021", "01-04-2021", "01-04-2021", 
"01-04-2021", "01-04-2021", "01-04-2021", "01-04-2021", "01-04-2021", 
"01-04-2021", "01-04-2021", "01-04-2021", "01-04-2021", "01-04-2021", 
"01-04-2021", "01-04-2021", "01-04-2021", "01-04-2021", "01-04-2021", 
"01-04-2021", "01-04-2021", "01-04-2021", "01-05-2021", "01-05-2021", 
"01-05-2021", "01-05-2021", "01-05-2021", "01-05-2021", "01-05-2021", 
"01-05-2021", "01-05-2021", "01-05-2021", "01-05-2021", "01-05-2021", 
"01-05-2021", "01-05-2021", "01-05-2021", "01-05-2021", "01-05-2021", 
"01-05-2021", "01-05-2021", "01-05-2021", "01-05-2021", "01-05-2021", 
"01-05-2021", "01-05-2021", "01-06-2021", "01-06-2021", "01-06-2021", 
"01-06-2021", "01-06-2021", "01-06-2021", "01-06-2021", "01-06-2021", 
"01-06-2021", "01-06-2021", "01-06-2021", "01-06-2021", "01-06-2021", 
"01-06-2021", "01-06-2021", "01-06-2021", "01-06-2021", "01-06-2021", 
"01-06-2021", "01-06-2021", "01-06-2021", "01-06-2021", "01-06-2021", 
"01-06-2021", "01-07-2021", "01-07-2021", "01-07-2021", "01-07-2021", 
"01-07-2021", "01-07-2021", "01-07-2021", "01-07-2021", "01-07-2021", 
"01-07-2021", "01-07-2021", "01-07-2021", "01-07-2021", "01-07-2021", 
"01-07-2021", "01-07-2021", "01-07-2021", "01-07-2021", "01-07-2021", 
"01-07-2021", "01-07-2021", "01-07-2021", "01-07-2021", "01-07-2021", 
"01-08-2021", "01-08-2021", "01-08-2021", "01-08-2021", "01-08-2021", 
"01-08-2021", "01-08-2021", "01-08-2021", "01-08-2021", "01-08-2021", 
"01-08-2021", "01-08-2021", "01-08-2021", "01-08-2021", "01-08-2021", 
"01-08-2021", "01-08-2021", "01-08-2021", "01-08-2021", "01-08-2021", 
"01-08-2021", "01-08-2021", "01-08-2021", "01-08-2021", "01-09-2021", 
"01-09-2021", "01-09-2021", "01-09-2021", "01-09-2021", "01-09-2021", 
"01-09-2021", "01-09-2021", "01-09-2021", "01-09-2021", "01-09-2021", 
"01-09-2021", "01-09-2021", "01-09-2021", "01-09-2021", "01-09-2021", 
"01-09-2021", "01-09-2021", "01-09-2021", "01-09-2021", "01-09-2021", 
"01-09-2021", "01-09-2021", "01-09-2021", "01-10-2021", "01-10-2021", 
"01-10-2021", "01-10-2021", "01-10-2021", "01-10-2021", "01-10-2021", 
"01-10-2021", "01-10-2021", "01-10-2021", "01-10-2021", "01-10-2021", 
"01-10-2021", "01-10-2021", "01-10-2021", "01-10-2021", "01-10-2021", 
"01-10-2021", "01-10-2021", "01-10-2021", "01-10-2021", "01-10-2021", 
"01-10-2021", "01-10-2021", "01-11-2021", "01-11-2021", "01-11-2021", 
"01-11-2021", "01-11-2021", "01-11-2021", "01-11-2021", "01-11-2021", 
"01-11-2021", "01-11-2021", "01-11-2021", "01-11-2021", "01-11-2021", 
"01-11-2021", "01-11-2021", "01-11-2021", "01-11-2021", "01-11-2021", 
"01-11-2021", "01-11-2021", "01-11-2021", "01-11-2021", "01-11-2021", 
"01-11-2021", "01-12-2021", "01-12-2021", "01-12-2021", "01-12-2021", 
"01-12-2021", "01-12-2021", "01-12-2021", "01-12-2021", "01-12-2021", 
"01-12-2021", "01-12-2021", "01-12-2021", "01-12-2021", "01-12-2021", 
"01-12-2021", "01-12-2021", "01-12-2021", "01-12-2021", "01-12-2021", 
"01-12-2021", "01-12-2021", "01-12-2021", "01-12-2021", "01-12-2021", 
"01-13-2021", "01-13-2021", "01-13-2021", "01-13-2021", "01-13-2021", 
"01-13-2021", "01-13-2021", "01-13-2021", "01-13-2021", "01-13-2021", 
"01-13-2021", "01-13-2021", "01-13-2021", "01-13-2021", "01-13-2021", 
"01-13-2021", "01-13-2021", "01-13-2021", "01-13-2021", "01-13-2021", 
"01-13-2021", "01-13-2021", "01-13-2021", "01-13-2021", "01-14-2021", 
"01-14-2021", "01-14-2021", "01-14-2021", "01-14-2021", "01-14-2021", 
"01-14-2021", "01-14-2021", "01-14-2021", "01-14-2021", "01-14-2021", 
"01-14-2021", "01-14-2021", "01-14-2021", "01-14-2021", "01-14-2021", 
"01-14-2021", "01-14-2021", "01-14-2021", "01-14-2021", "01-14-2021", 
"01-14-2021", "01-14-2021", "01-14-2021", "01-15-2021", "01-15-2021", 
"01-15-2021", "01-15-2021", "01-15-2021", "01-15-2021", "01-15-2021", 
"01-15-2021", "01-15-2021", "01-15-2021", "01-15-2021", "01-15-2021", 
"01-15-2021", "01-15-2021", "01-15-2021", "01-15-2021", "01-15-2021", 
"01-15-2021", "01-15-2021", "01-15-2021", "01-15-2021", "01-15-2021", 
"01-15-2021", "01-15-2021"), Hour = c("00:00", "01:00", "02:00", 
"03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", 
"10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", 
"17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00", 
"00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", 
"07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", 
"14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", 
"21:00", "22:00", "23:00", "00:00", "01:00", "02:00", "03:00", 
"04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", 
"11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", 
"18:00", "19:00", "20:00", "21:00", "22:00", "23:00", "00:00", 
"01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", 
"08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", 
"15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", 
"22:00", "23:00", "00:00", "01:00", "02:00", "03:00", "04:00", 
"05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", 
"12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", 
"19:00", "20:00", "21:00", "22:00", "23:00", "00:00", "01:00", 
"02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", 
"09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", 
"16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", 
"23:00", "00:00", "01:00", "02:00", "03:00", "04:00", "05:00", 
"06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", 
"13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", 
"20:00", "21:00", "22:00", "23:00", "00:00", "01:00", "02:00", 
"03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", 
"10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", 
"17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00", 
"00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", 
"07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", 
"14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", 
"21:00", "22:00", "23:00", "00:00", "01:00", "02:00", "03:00", 
"04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", 
"11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", 
"18:00", "19:00", "20:00", "21:00", "22:00", "23:00", "00:00", 
"01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", 
"08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", 
"15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", 
"22:00", "23:00", "00:00", "01:00", "02:00", "03:00", "04:00", 
"05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", 
"12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", 
"19:00", "20:00", "21:00", "22:00", "23:00", "00:00", "01:00", 
"02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", 
"09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", 
"16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", 
"23:00", "00:00", "01:00", "02:00", "03:00", "04:00", "05:00", 
"06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", 
"13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", 
"20:00", "21:00", "22:00", "23:00", "00:00", "01:00", "02:00", 
"03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", 
"10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", 
"17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00"
), Activity = c("1", "1", "1", "1", "1", "1", "1", "1", "23", 
"2", "23", "23", "23", "23", "8", "8", "15", "15", "23", "23", 
"23", "2", "23", "23", "1", "1", "1", "1", "1", "1", "1", "10", 
"23", "10", "23", "23", "23", "23", "23", "8", "8", "23", "23", 
"23", "23", "23", "23", "7", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "23", "23", "23", "23", "23", "23", "8", "4", "4", 
"15", "15", "15", "2", "2", "23", "7", "1", "1", "1", "1", "1", 
"1", "15", "15", "15", "23", "23", "23", "23", "23", "23", "7.7", 
"7.7", "7.7", "7.7", "2", "15", "15", "15", "1", "1", "1", "1", 
"1", "1", "1", "15", "15", "15", "23", "23", "23", "23", "23", 
"23", "23", "15", "15", "23", "17.1", "17.1", "23", "23", "23", 
"4", "4", "1", "1", "1", "1", "15", "15", "23", "23", "23", "23", 
"23", "8", "8", "8", "8", "15", "2", "15", "15", "23", "1", "1", 
"1", "1", "1", "1", "1", "1", "15", "15", "14", "14", "14", "14", 
"14", "11", "11", "11", "11", "15", "15", "15", "15", "15", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "7.5", "7.5", "7.5", 
"7.5", "7.5", "7.5", "22", "22", "22", "22", "22", "7.5", "7.5", 
"7.5", "7.5", "1", "1", "1", "1", "1", "1", "1", "1", "1", "22", 
"22", "7.5", "7.5", "7.5", "7.5", "7.5", "16", "8", "8", "15", 
"15", "2", "2", "7.5", "7.5", "1", "1", "1", "1", "1", "1", "1", 
"1", "15", "15", "15", "15", "15", "2", "15", "16", "19", "19", 
"19", "19", "2", "15", "15", "15", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "5", "5", "16", "16", "16", "1", "1", "1", "1", 
"15", "17", "17", "8", "8", "17", "17", "17", "1", "1", "1", 
"1", "1", "1", "2", "10", "10", "5", "5", "5", "5", "15", "15", 
"23", "23", "23", "23", "23", "23", "16", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "5", "5", "5", "5", "2", "5", "5", "22", 
"22", "22", "22", "2", "17", "17", "17", "1", "1", "1", "1", 
"1", "1", "1", "19", "19", "19", "19", "19", "15", "2", "1", 
"1", "1", "7.5", "7.5", "7.5", "2", "23", "23", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "2", "19", "5", "5", "2", "5", 
"5", "5", "5", "16", "16", "7.5", "2", "23", "23", "23"), Activity_Name = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 12L, 10L, 12L, 12L, 12L, 12L, 18L, 
18L, 5L, 5L, 12L, 12L, 12L, 10L, 12L, 12L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 12L, 2L, 12L, 12L, 12L, 12L, 12L, 18L, 18L, 12L, 
12L, 12L, 12L, 12L, 12L, 15L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 12L, 12L, 12L, 12L, 12L, 12L, 18L, 13L, 13L, 5L, 5L, 5L, 
10L, 10L, 12L, 15L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 12L, 
12L, 12L, 12L, 12L, 12L, 17L, 17L, 17L, 17L, 10L, 5L, 5L, 5L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 5L, 5L, 12L, 8L, 8L, 12L, 12L, 12L, 13L, 13L, 1L, 1L, 
1L, 1L, 5L, 5L, 12L, 12L, 12L, 12L, 12L, 18L, 18L, 18L, 18L, 
5L, 10L, 5L, 5L, 12L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 
4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 5L, 5L, 5L, 5L, 5L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 16L, 16L, 16L, 16L, 16L, 16L, 11L, 
11L, 11L, 11L, 11L, 16L, 16L, 16L, 16L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 11L, 11L, 16L, 16L, 16L, 16L, 16L, 6L, 18L, 18L, 
5L, 5L, 10L, 10L, 16L, 16L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 
5L, 5L, 5L, 5L, 10L, 5L, 6L, 9L, 9L, 9L, 9L, 10L, 5L, 5L, 5L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 14L, 14L, 6L, 6L, 6L, 1L, 
1L, 1L, 1L, 5L, 7L, 7L, 18L, 18L, 7L, 7L, 7L, 1L, 1L, 1L, 1L, 
1L, 1L, 10L, 2L, 2L, 14L, 14L, 14L, 14L, 5L, 5L, 12L, 12L, 12L, 
12L, 12L, 12L, 6L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 14L, 14L, 
14L, 14L, 10L, 14L, 14L, 11L, 11L, 11L, 11L, 10L, 7L, 7L, 7L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 9L, 9L, 9L, 9L, 9L, 5L, 10L, 1L, 
1L, 1L, 16L, 16L, 16L, 10L, 12L, 12L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 10L, 9L, 14L, 14L, 10L, 14L, 14L, 14L, 14L, 6L, 6L, 
16L, 10L, 12L, 12L, 12L), .Label = c("Sleeping", "Internet Browsing", 
"IRL Social Time", "Travelling", "Waste/Down Time", "Terrace Time", 
"Other Productive Activities", "TV", "Sports & Exercise", "Eating", 
"Shopping", "Coding", "Online Meetings", "Studying", "Audiobook", 
"Sundaram Playing", "Walking", "Netflix"), class = "factor"), 
    count = c(122L, 122L, 122L, 122L, 122L, 122L, 122L, 122L, 
    63L, 18L, 63L, 63L, 63L, 63L, 13L, 13L, 43L, 43L, 63L, 63L, 
    63L, 18L, 63L, 63L, 122L, 122L, 122L, 122L, 122L, 122L, 122L, 
    4L, 63L, 4L, 63L, 63L, 63L, 63L, 63L, 13L, 13L, 63L, 63L, 
    63L, 63L, 63L, 63L, 2L, 122L, 122L, 122L, 122L, 122L, 122L, 
    122L, 122L, 122L, 63L, 63L, 63L, 63L, 63L, 63L, 13L, 4L, 
    4L, 43L, 43L, 43L, 18L, 18L, 63L, 2L, 122L, 122L, 122L, 122L, 
    122L, 122L, 43L, 43L, 43L, 63L, 63L, 63L, 63L, 63L, 63L, 
    4L, 4L, 4L, 4L, 18L, 43L, 43L, 43L, 122L, 122L, 122L, 122L, 
    122L, 122L, 122L, 43L, 43L, 43L, 63L, 63L, 63L, 63L, 63L, 
    63L, 63L, 43L, 43L, 63L, 2L, 2L, 63L, 63L, 63L, 4L, 4L, 122L, 
    122L, 122L, 122L, 43L, 43L, 63L, 63L, 63L, 63L, 63L, 13L, 
    13L, 13L, 13L, 43L, 18L, 43L, 43L, 63L, 122L, 122L, 122L, 
    122L, 122L, 122L, 122L, 122L, 43L, 43L, 5L, 5L, 5L, 5L, 5L, 
    4L, 4L, 4L, 4L, 43L, 43L, 43L, 43L, 43L, 122L, 122L, 122L, 
    122L, 122L, 122L, 122L, 122L, 122L, 21L, 21L, 21L, 21L, 21L, 
    21L, 11L, 11L, 11L, 11L, 11L, 21L, 21L, 21L, 21L, 122L, 122L, 
    122L, 122L, 122L, 122L, 122L, 122L, 122L, 11L, 11L, 21L, 
    21L, 21L, 21L, 21L, 8L, 13L, 13L, 43L, 43L, 18L, 18L, 21L, 
    21L, 122L, 122L, 122L, 122L, 122L, 122L, 122L, 122L, 43L, 
    43L, 43L, 43L, 43L, 18L, 43L, 8L, 10L, 10L, 10L, 10L, 18L, 
    43L, 43L, 43L, 122L, 122L, 122L, 122L, 122L, 122L, 122L, 
    122L, 122L, 18L, 18L, 8L, 8L, 8L, 122L, 122L, 122L, 122L, 
    43L, 8L, 8L, 13L, 13L, 8L, 8L, 8L, 122L, 122L, 122L, 122L, 
    122L, 122L, 18L, 4L, 4L, 18L, 18L, 18L, 18L, 43L, 43L, 63L, 
    63L, 63L, 63L, 63L, 63L, 8L, 122L, 122L, 122L, 122L, 122L, 
    122L, 122L, 122L, 122L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
    11L, 11L, 11L, 11L, 18L, 8L, 8L, 8L, 122L, 122L, 122L, 122L, 
    122L, 122L, 122L, 10L, 10L, 10L, 10L, 10L, 43L, 18L, 122L, 
    122L, 122L, 21L, 21L, 21L, 18L, 63L, 63L, 122L, 122L, 122L, 
    122L, 122L, 122L, 122L, 122L, 122L, 18L, 10L, 18L, 18L, 18L, 
    18L, 18L, 18L, 18L, 8L, 8L, 21L, 18L, 63L, 63L, 63L)), row.names = c(NA, 
-360L), groups = structure(list(Activity = c("1", "10", "11", 
"14", "15", "16", "17", "17.1", "19", "2", "22", "23", "4", "5", 
"7", "7.5", "7.7", "8"), .rows = structure(list(c(1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 49L, 50L, 
51L, 52L, 53L, 54L, 55L, 56L, 57L, 74L, 75L, 76L, 77L, 78L, 79L, 
97L, 98L, 99L, 100L, 101L, 102L, 103L, 124L, 125L, 126L, 127L, 
144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L, 168L, 169L, 170L, 
171L, 172L, 173L, 174L, 175L, 176L, 192L, 193L, 194L, 195L, 196L, 
197L, 198L, 199L, 200L, 217L, 218L, 219L, 220L, 221L, 222L, 223L, 
224L, 241L, 242L, 243L, 244L, 245L, 246L, 247L, 248L, 249L, 255L, 
256L, 257L, 258L, 267L, 268L, 269L, 270L, 271L, 272L, 289L, 290L, 
291L, 292L, 293L, 294L, 295L, 296L, 297L, 313L, 314L, 315L, 316L, 
317L, 318L, 319L, 327L, 328L, 329L, 336L, 337L, 338L, 339L, 340L, 
341L, 342L, 343L, 344L), c(32L, 34L, 274L, 275L), 159:162, 154:158, 
    c(17L, 18L, 67L, 68L, 69L, 80L, 81L, 82L, 94L, 95L, 96L, 
    104L, 105L, 106L, 114L, 115L, 128L, 129L, 139L, 141L, 142L, 
    152L, 153L, 163L, 164L, 165L, 166L, 167L, 211L, 212L, 225L, 
    226L, 227L, 228L, 229L, 231L, 238L, 239L, 240L, 259L, 280L, 
    281L, 325L), c(208L, 232L, 252L, 253L, 254L, 288L, 354L, 
    355L), c(260L, 261L, 264L, 265L, 266L, 310L, 311L, 312L), 
    117:118, c(233L, 234L, 235L, 236L, 320L, 321L, 322L, 323L, 
    324L, 346L), c(10L, 22L, 70L, 71L, 93L, 140L, 213L, 214L, 
    230L, 237L, 273L, 302L, 309L, 326L, 333L, 345L, 349L, 357L
    ), c(183L, 184L, 185L, 186L, 187L, 201L, 202L, 305L, 306L, 
    307L, 308L), c(9L, 11L, 12L, 13L, 14L, 19L, 20L, 21L, 23L, 
    24L, 33L, 35L, 36L, 37L, 38L, 39L, 42L, 43L, 44L, 45L, 46L, 
    47L, 58L, 59L, 60L, 61L, 62L, 63L, 72L, 83L, 84L, 85L, 86L, 
    87L, 88L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 116L, 
    119L, 120L, 121L, 130L, 131L, 132L, 133L, 134L, 143L, 282L, 
    283L, 284L, 285L, 286L, 287L, 334L, 335L, 358L, 359L, 360L
    ), c(65L, 66L, 122L, 123L), c(250L, 251L, 276L, 277L, 278L, 
    279L, 298L, 299L, 300L, 301L, 303L, 304L, 347L, 348L, 350L, 
    351L, 352L, 353L), c(48L, 73L), c(177L, 178L, 179L, 180L, 
    181L, 182L, 188L, 189L, 190L, 191L, 203L, 204L, 205L, 206L, 
    207L, 215L, 216L, 330L, 331L, 332L, 356L), 89:92, c(15L, 
    16L, 40L, 41L, 64L, 135L, 136L, 137L, 138L, 209L, 210L, 262L, 
    263L)), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", 
"list"))), row.names = c(NA, 18L), class = c("tbl_df", "tbl", 
"data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"))
like image 705
namtar Avatar asked Jan 18 '21 16:01

namtar


2 Answers

Try with this. There is some issue in the data type of the count, so transforming to dataframe can alleviate the issue:

#Data
data <- as.data.frame(data)
#Plot
data %>%
  arrange(count,Activity_Name) %>%
  mutate(Activity_Name=as.character(Activity_Name)) %>%
  mutate(Activity_Name=factor(Activity_Name,levels = unique(Activity_Name),
                              ordered = T)) %>%
  ggplot(aes(x = Activity_Name, y = count)) +
  geom_segment(aes(
    x = Activity_Name,
    xend = Activity_Name,
    y = 0,
    yend = count
  ),
  color = "skyblue") +
  geom_point(color = "blue",
             size = 5,
             alpha = 0.6) +
  theme_light() +
  scale_y_log10() +
  coord_flip()

Output (I changed colors for reproducible plot):

enter image description here

like image 107
Duck Avatar answered Oct 09 '22 06:10

Duck


This is because your data is grouped, so the fct_reorder in mutate is trying to work within groups instead of the whole data frame. Just add an ungroup and it should be the desired output

Row 2 of the output shows that the data is grouped

data
# # A tibble: 360 x 5
# # Groups:   Activity [18]
#    Day        Hour  Activity Activity_Name count
#    <chr>      <chr> <chr>    <fct>         <int>
#  1 01-01-2021 00:00 1        Sleeping        122
#  2 01-01-2021 01:00 1        Sleeping        122
#  3 01-01-2021 02:00 1        Sleeping        122
#  4 01-01-2021 03:00 1        Sleeping        122
#  5 01-01-2021 04:00 1        Sleeping        122
#  6 01-01-2021 05:00 1        Sleeping        122
#  7 01-01-2021 06:00 1        Sleeping        122
#  8 01-01-2021 07:00 1        Sleeping        122
#  9 01-01-2021 08:00 23       Coding           63
# 10 01-01-2021 09:00 2        Eating           18
# # … with 350 more rows

Adding an ungroup:

data %>%   
  ungroup %>% 
  mutate(Activity_Name = fct_reorder(Activity_Name, count)) %>%
  ggplot(aes(x = Activity_Name, y = count)) +
  geom_segment(aes(
    x = Activity_Name,
    xend = Activity_Name,
    y = 0,
    yend = count
  ),
  color = "skyblue") +
  geom_point(color = "#F2F7F2",
             size = 5,
             alpha = 0.6) +
  theme_light() +
  scale_y_log10() +
  coord_flip()

enter image description here

like image 31
IceCreamToucan Avatar answered Oct 09 '22 05:10

IceCreamToucan