1

I have a dataframe that I am trying to plot in a bar graph and arrange the labels according to values in a different column. I understand that I must create factors and order the levels (related post), but what is the best way to create a factor when the labels are not unique and you are using the fill parameter.

This is how I am plotting:

cat_all %>% 
  ggplot(aes(fill=device, y=t_by_p, x=domain)) +
  geom_bar(position="stack", stat="identity", colour="black") +
  geom_text(aes(label=round(t_by_p, 2)), 
            size = 3,
            position = position_stack(vjust = .5)) +
  coord_flip() +
  labs(y = "total time spent/distinct people",
       x = sprintf("Top 10 %s Domains", 'X'),
       title = sprintf("Cluster 2 %s Domain Engagement", 'X'))

enter image description here

The idea is to order by the total value (desktop and phone).

data:

cat_all  <- structure(list(domain = c("businessinsider.com|News/Research", 
"chase.com|Banking", "paypal.com|Personal Finance", "forbes.com|News/Research", 
"bloomberg.com|News/Research", "cnbc.com|News/Research", "bankofamerica.com|Banking", 
"wellsfargo.com|Banking", "wsj.com|News/Research", "fidelity.com|Online Trading", 
"businessinsider.com|News/Research", "paypal.com|Personal Finance", 
"forbes.com|News/Research", "cnbc.com|News/Research", "reuters.com|News/Research", 
"bloomberg.com|News/Research", "chase.com|Banking", "bankofamerica.com|Banking", 
"wellsfargo.com|Banking", "wsj.com|News/Research"), device = c("desktop", 
"desktop", "desktop", "desktop", "desktop", "desktop", "desktop", 
"desktop", "desktop", "desktop", "phone", "phone", "phone", "phone", 
"phone", "phone", "phone", "phone", "phone", "phone"), t_by_p = c(3.40721337398374, 
8.60096034164358, 6.23387870632672, 3.78531992009132, 12.9647524904215, 
6.04311842447917, 10.1131791503268, 9.58312816091954, 6.69483134556575, 
20.556119009009, 4.0323962962963, 6.47267734375, 2.11255132275132, 
3.36567561728395, 5.78803899371069, 3.78916862745098, 6.08099117647059, 
7.82377898550725, 9.81572870370371, 3.73643333333333)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("domain", 
"device", "t_by_p"))
MrFlick
  • 195,160
  • 17
  • 277
  • 295
Patrick Nieto
  • 582
  • 4
  • 4
  • near-duplicate: https://stackoverflow.com/questions/4000670/how-to-order-breaks-with-ggplot-geom-bar?rq=1 – C8H10N4O2 Oct 03 '18 at 12:53

2 Answers2

3

One way is to just calculate the order first. Here's a way using dplur and forcats

library(dplyr)
library(forcats)
lvls <- cat_all %>% 
  group_by(domain) %>% 
  summarize(total=sum(t_by_p)) %>% 
  mutate(domain=fct_reorder(domain, total)) %>% 
  pull(domain) %>% levels()

Then you can use the lvls vairable for plotting

cat_all %>% 
  mutate(domain=factor(domain, levels=lvls)) %>% 
  ggplot(aes(fill=device, y=t_by_p, x=domain)) +
  geom_bar(position="stack", stat="identity", colour="black") +
  geom_text(aes(label=round(t_by_p, 2)), 
            size = 3,
            position = position_stack(vjust = .5)) +
  coord_flip() +
  labs(y = "total time spent/distinct people",
       x = sprintf("Top 10 %s Domains", 'X'),
       title = sprintf("Cluster 2 %s Domain Engagement", 'X'))

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
2

Staying in the tidyverse, you could do:

library(dplyr)
cat_all %>% 
  group_by(domain) %>% 
  summarize(total_time = sum(t_by_p)) %>%
  arrange(total_time) %>%
  select(domain) %>% unlist -> domain_breaks

cat_all %<>% mutate(domain=factor(domain, levels = domain_breaks))

library(ggplot2)
cat_all %>% 
  ggplot(aes(fill=device, y=t_by_p, x=domain)) +
  geom_bar(position="stack", stat="identity", colour="black") +
  geom_text(aes(label=round(t_by_p, 2)), 
            size = 3,
            position = position_stack(vjust = .5)) +
  coord_flip() +
  labs(y = "total time spent/distinct people",
       x = sprintf("Top 10 %s Domains", 'X'),
       title = sprintf("Cluster 2 %s Domain Engagement", 'X'))

enter image description here

C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134