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'))
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"))