0
votes

i have a bar chart with fill of two levels the y axis is categorical with 0,1. the bar chart shows the 0 count and 1 count now i want to show the each individual percentage of each bar on each bar so that i can see which bar is highest and then which 1 in each bar is higher. but my count is categorical

i want to show percentage of each individual bar as 100% then divided into groups.

ggplot(stackoverflow,aes(x=stackoverflow$person, fill=stackoverflow$success))+facet_wrap(~stackoverflow$city)+geom_bar()

Like this

   structure(list(data = structure(list(source = structure(c(1L, 
1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("p", 
"q", "r"), class = "factor"), person = structure(c(1L, 1L, 1L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 1L), .Label = c("a", "b", 
"c"), class = "factor"), city = structure(c(1L, 1L, 3L, 3L, 3L, 
2L, 1L, 1L, 1L, 3L, 3L, 3L, 3L), .Label = c("x", "y", "z"), class = "factor"), 
    success = structure(c(1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
    1L, 2L, 2L, 2L), .Label = c("0", "1"), class = "factor")), row.names = c(NA, 
-13L), class = "data.frame"), layers = list(<environment>), scales = <environment>, 
    mapping = structure(list(x = ~stackoverflow$person, fill = ~stackoverflow$success), class = "uneval"), 
    theme = list(), coordinates = <environment>, facet = <environment>, 
    plot_env = <environment>, labels = list(x = "stackoverflow$person", 
        fill = "stackoverflow$success", y = "count", weight = "weight")), class = c("gg", 
"ggplot"))
1
Could you provide some data? I think I know how to do it, but I need some data for a reply.akash87
source person city success p a x 0 p a x 0 q a z 1 q b z 1 q b z 1 q b y 1 i was trying to form graph of person on x axis and then fill= city, count of success on y axis then i want to see in each bar 0 , 1 are there then i want to show that as a percentageraju varasala 1801166
To properly get your data out so I can view it correctly and work with it, use dput function on your dataset. Like dput(df) and then copy and paste the result into the question above.akash87
source person city success( p a x 0 )(p a x 0 )(q a z 1 )(q b z 1 )(q b z 1)( q b y 1)raju varasala 1801166
how to use dputraju varasala 1801166

1 Answers

1
votes

Start with some data aggregation using tidyverse:

dk %>% 
group_by(person, city, success) %>% 
summarise(counts = n()) %>% 
right_join(dk %>% 
           group_by(city, person) %>% 
           summarise(all_counts= n())) %>% 
mutate(percents = paste0(round(counts/all_counts * 100, 2), "%")) %>% 
ggplot(aes(x = person, y = counts, fill = as.factor(success))) + 
geom_bar(stat = "identity") + 
geom_text(aes(label = percents), position = position_stack(vjust = 0.5)) +
facet_wrap(~city) + 
coord_flip()

So in effect, I first find the count of success given person and city and divide that by the total counts (all_counts), which is the total number of people given city. Then we find the percents and then plot it using ggplot. Because these are aggregated, we use geom_bar with (stat = "identity") and use geom_text, which prints the percents (position_stack(vjust = 0.5) centers the label). Finally, we facet it based on city. The coord_flip() line flips the x and y axes.