change the order of bar charts to ascending, change to proportion on Y axis without losing color

dataset is as follows:
data.frame(
stringsAsFactors = FALSE,
ID = c(10,11,12,13,14,15,16,17,
18,19,20,21,22,23,24,25,26,27,28,29),
Rescan1 = c("Yes","No","No","No","No",
"No","No","No","No","No","No","No","Yes","No",
"No","No","Yes","No","No","No")
)

I have created two barcharts using thiscode

ggplot(prostate_cleaned, aes(x=Rescan1, fill=Rescan1)) + geom_bar()+scale_fill_manual(values = c("green", "red")) + geom_text(stat= 'count', aes(label=..count.., vjust=1))  

I want to do the following:

  1. Add in proprtions on the Y axis rather than counts. entering the code below made me lose the colors - turned back to gray
ggplot(prostate_cleaned, aes(x=Rescan1, fill=Rescan1, y = ..prop.., group = 1)) + geom_bar()+scale_fill_manual(values = c("green", "red")) + geom_text(stat= 'count', aes(label=..count.., vjust=1))  

  1. Also, I wish to change the order of the bar plots - i.e. I want the 'Yes' (count = 3) Bar chart (, to be displayed first then the 'No' Bar Chart.

I'm challenging myself to find solutions to this..but clearly struggling. Merci.

Hi @saurabh,
I can fix your column ordering question but couldn't get the "proportion" bar chart to work without first making an intermediate data.frame. Maybe someone else has a solution?

suppressPackageStartupMessages(library(tidyverse))

prostate_cleaned <- data.frame(
  stringsAsFactors = FALSE,
  ID = c(10,11,12,13,14,15,16,17,
  18,19,20,21,22,23,24,25,26,27,28,29),
  Rescan1 = c("Yes","No","No","No","No",
  "No","No","No","No","No","No","No","Yes","No",
  "No","No","Yes","No","No","No"))

# Your original graph with the bar position specified, and
# legend removed.
ggplot(prostate_cleaned, aes(x=Rescan1, fill=Rescan1)) + 
  geom_bar() +
  scale_fill_manual(values = c("green", "red")) + 
  geom_text(stat= 'count', aes(label=..count.., vjust=1)) +
  scale_x_discrete(limits = c("Yes", "No")) +
  theme(legend.position = "none")

prostate_cleaned %>%
  mutate(total = nrow(.)) %>% 
  group_by(Rescan1) %>% 
  summarise(tots = n(),
            totals = mean(total),
            prop = tots/sum(totals)) -> summary.df

ggplot(summary.df) +
  aes(x=Rescan1, y=prop, fill=Rescan1) + 
  geom_col() +
  scale_fill_manual(values = c("green", "red")) + 
  geom_text(aes(x=Rescan1, label=tots, vjust=1)) +
  scale_x_discrete(limits = c("Yes", "No")) +
  theme(legend.position = "none") 

Created on 2021-07-01 by the reprex package (v2.0.0)

#--- BUT this doesn't work?? ------
ggplot(prostate_cleaned, aes(x=Rescan1, fill=Rescan1, y = ..prop.., group=factor(Rescan1))) + 
  geom_bar() + 
  scale_fill_manual(values = c("green", "red")) + 
  geom_text(stat= 'count', aes(label=..count.., vjust=1)) +
  scale_x_discrete(limits = c("Yes", "No"))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.