@Adishwar it is generally recommended to use geom_col() if you work with totals. This avoids you having to set stat
in geom_bar().
Since there is no reproducible example in terms of data, I have grossly simplified your data frame emulating your variable names. (The error you get is because perc
is not known in your case, aka "object not found").
I then use geom_col() and reorder for the ordered barchart.
Your example does not specify where you got the fill colours from.
You can set them manually dependent on your needs. I use the out-of-the-box colours ...
If you do not want a label to show, set it to NULL (which removes it).
# setting up the test data with 2 categories and counts
df <- tribble(
~y_variable, ~fact_variable, ~count
,"Government" , "fact1" , 100
,"Government" , "fact2" , 200
,"Government" , "fact3" , 500
,"Nonprofit" , "fact1" , 75
,"Nonprofit" , "fact2" , 200
,"Nonprofit" , "fact3" , 100
,"Other" , "fact1" , 300
,"Other" , "fact2" , 50
,"Other" , "fact3" , 80
)
# "standard barchart on counts (totals) using geom_col()
ggplot(data = df, aes(x = count, y = y_variable, fill = fact_variable)) +
geom_col() +
# this is to set the title, labels, etc ---------------------------
labs(title = "your title here", fill = "possible fill title"
,x = "my counts or NULL", y = NULL) +
theme_minimal()
For the ordered version, use reorder(variable, ordervariable).
Make sure to assign the right variable names.
ggplot(data = df, aes(x = count, y = reorder(y_variable, count), fill = fact_variable)) +
geom_col() +
labs(title = "your title here", fill = "possible fill title"
,x = "my counts or NULL", y = NULL) +
theme_minimal()
