different colours in GGPLOT

I have a dataset here.

data.frame(
ID = c(1,2,3,4,5,6,7,8,9,10,11,12,13,
14,15,16,17,18,19,20,21,22),
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")

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

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

Maybe

dat1 <-   data.frame( 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(dat1, aes(Rescan1, fill = Rescan1)) + geom_bar()

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

Thanks,

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

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),
Rescan = c(1,1,1,1,1,1,1,1,1,2,1,
1,1,1,1,1,1,1,1,1,1,2),
Rescan1 = c("No","No","No","No","No",
"No","No","No","No","Yes","No","No","No","No",
"No","No","No","No","No","No","No","Yes")
)

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

ggplot(prostate_cleaned_scaled,
       aes(x=Rescan1,
           fill=Rescan1, 
           y = prop)) + 
  geom_col()+
  scale_fill_manual(values = c("Green", "Red")) + 
  geom_text( aes(label=n,
                vjust=1))
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)) + 
     geom_bar(aes(fill=Rescan1))

... and the result is...
plot

Tell me if that worked for you
N

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.