简体   繁体   中英

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" . 情节 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"))

情节2

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

Thank you very much for your help with this one!

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: 输出图

I hope this addresses your question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM