matbess
matbess

Reputation: 1

ggplot2 - geom_histogram / scale_fill_manual

I am working the following dataframe (df):

df$GP<-c(0,0,0,1,1,2,3,3,3,3,4,4,9,15,18,18,19,19,20,20,21,22,22,23)

df$colour<-c("g","g","g","g","g","g","g","g","g","g","g","g","t","t","g","g","g","g","g","g","g","g","g","g")

I want the histogram below, but showing a different fill for colour=="g" and colour=="t". plot However, running the following code, the bars labelled colour=="t", go out of scale (up to 1 - plot2) whereas should be at 0.25 (plot1).

ggplot(data=df,aes(x=GP,y=..ndensity..))+geom_histogram(bins=25,aes(fill=colour))+scale_fill_manual(values=c("black","grey"))

plot2

Do you have any idea of how this could be achieved?

Thank you very much for your help with this one!

Upvotes: 0

Views: 1223

Answers (1)

Yusuf al-Imrani
Yusuf al-Imrani

Reputation: 65

I used a tibble as the data type for dataset, with different tibble variable names.

the result is just as you want.

  tb <- tibble(
  tbx = c(0, 0, 0, 1, 1, 2, 3, 3, 3, 3, 4, 4, 9, 15, 18, 18, 19, 19, 20, 20, 21, 22, 22, 23),
  tby = c("g","g","g","g","g","g","g","g","g","g","g","g","t","t","g","g","g","g","g","g","g","g","g","g")
)


ggplot(tb, aes(tbx, tby = ..ndensity..)) +
  geom_histogram(bins = 25, aes(fill = tby)) +
  scale_fill_manual(values = c("red", "grey"))

and this is the output plot: Output Plot

I hope this addresses your question

Upvotes: 1

Related Questions