different colours in GGPLOT

I have a dataset here.

ID = c(1,2,3,4,5,6,7,8,9,10,11,12,13,
stringsAsFactors = FALSE,
ID = c(1,2,3,4,5,6,7,8,9,10,
Rescan1 = c("No","No","No","No","No",

I'm trying to use ggplot to get two different colours for each bar chart that appears. I enter the following code
ggplot(prostate_cleaned, aes(Rescan1)) + geom_bar()

ID - Patient ID, Rescan1- whether or not patient needed rescanned (a discrete variable)

this gives me two gray bar chats (which I presume is the default). .
I want the 'Yes' to be in Green and 'No' to be in red.

I tried this:
ggplot(prostate_cleaned, aes(Rescan1)) + geom_bar() + scale_fill_manual(values = c("Red", "Green"))

but it doesnt change a think...Can someone help please?

  • Doesn't change a thing

Can you repost that dataframe or provide it in dput() format? It makes no sense at the moment.

Sure thing, I use datapasta:
stringsAsFactors = FALSE,
ID = c(1,2,3,4,5,6,7,8,9,10,
Rescan1 = c("No","No","No","No","No",

To further elucidate my problem...

ggplot(prostate_cleaned, aes(Rescan1)) + geom_bar(fill='green')

fills both graphs in green..but I want one in Red and the other in green. Thanks


dat1 <-   data.frame( ID = c(1,2,3,4,5,6,7,8,9,10,
  Rescan1 = c("No","No","No","No","No",

ggplot(dat1, aes(Rescan1, fill = Rescan1)) + geom_bar()

BTW, that data.frame is still messed up bur easily fixed


wondering if anyone has maanged to fix the problem?

Where is the data in the datapasta coming from? All that is actually missing is the last ) in the data.frame

stringsAsFactors = FALSE,
ID = c(1,2,3,4,5,6,7,8,9,10,
Rescan = c(1,1,1,1,1,1,1,1,1,2,1,
Rescan1 = c("No","No","No","No","No",

I've done and exact copy,paste from my PC using datapasta

That looks fine now. Thanks

in ggplot you map variable to aesthetics.
If you want to apply a fill, a fill aes needs to be provided, like this :

 ggplot(prostate_cleaned, aes(x=Rescan1,fill=Rescan1)) + 
  geom_bar() + 
  scale_fill_manual(values = c("Red", "Green"))

p.s. to format text as code in your forum posts, you use triple backticks to start and stop the formatting. like this

my code

code appears much easier to read when formatted this way.

Many thanks, works...but when I want to display the reuslts in proportion format, I tend lose the colour. please see below

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

Also, not sure where to put the code in for reversibility (i.e. for the 'Yes' to appear first on the graph.

Thanks for all your patience..I've only been learning R for about a week. Problem solving helps me understand this programming more. I have no programming background.

In general I recommend separating out calculations from plotting.
Here is a possible solution for what I believe you are trying to do

(prostate_cleaned_scaled <- 
  prostate_cleaned %>% group_by(Rescan1) %>%
  summarise(n=n()) %>% mutate(prop=n/sum(n)))

           y = prop)) + 
  scale_fill_manual(values = c("Green", "Red")) + 
  geom_text( aes(label=n,
1 Like

Many Thanks.

Worked. ..I've now learnt a bit about this 'piping' function thanks to your help.

I merely added:

scale_x_discrete(limits = c("Yes", "No")) 

to change the order of the barplots at the end the solution you provided

I used this code

data.frame(stringsAsFactors = FALSE,
     ID = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22),
     Rescan1 = c("No","No","No","No","No","No","No","No","No","Yes","No",
    "No","No","No","No","No","No","No","No","No","No","Yes")) %>% 
  ggplot(aes(Rescan1)) + 

... and the result is...

Tell me if that worked for you

Thanks, I'd already found a solution as above.

This topic was automatically closed 7 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.