1

I have a simplified dataframe

library(ggplot2)
df <- data.frame(wins=c(1,1,3,1,1,2,1,2,1,1,1,3))
ggplot(df,aes(x=wins))+geom_histogram(binwidth=0.5,fill="red")

I would like to get the final value in the sequence,3, shown with either a different fill or alpha. One way to identify its value is

tail(df,1)$wins

In addition, I would like to have the histogram bars shifted so that they are centered over the number. I tried unsuccesfully subtracting from the wins value

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
pssguy
  • 3,455
  • 7
  • 38
  • 68

2 Answers2

2

1) To draw bins in different colors you can use geom_histogram() for subsets.

2) To center bars along numbers on the x axis you can invoke scale_x_continuous(breaks=..., labels=...)

So, this code

library(ggplot2)
df <- data.frame(wins=c(1,1,3,1,1,2,11,2,11,15,1,1,3))
cond <- df$wins == tail(df,1)$wins

ggplot(df, aes(x=wins)) +
  geom_histogram(data=subset(df,cond==FALSE), binwidth=0.5, fill="red") +
  geom_histogram(data=subset(df,cond==TRUE), binwidth=0.5, fill="blue") +
  scale_x_continuous(breaks=df$wins+0.25, labels=df$wins)

produces the plot:

enter image description here

redmode
  • 4,821
  • 1
  • 25
  • 30
  • Thanks for that. How would I cater for instances where, say, one of the values was 15. I would want it to be to the far right of graph - at the appropriate separation from the others but it actually would appear between the 1 and 2 values as it is no longer numeric? – pssguy Nov 15 '12 at 03:17
  • You can just remove `as.factor()`. In this case bins wouldn't be centered against numbers on x axis. – redmode Nov 15 '12 at 12:39
  • Tx. I have accepted your answer but posted an edit which satisfies all of my needs. Hopefully it will be accepted for viewing – pssguy Nov 15 '12 at 15:29
  • @pssguy I think the usual SO practice is to provide such update in your question itself. I reviewed but didn't approve your edit, someone else might but if I were you I would add that to my question – Miserable Variable Nov 15 '12 at 15:35
  • @Miserable Variable. Thanks never had to do this before – pssguy Nov 15 '12 at 16:31
  • I updated my solution, now it solves both problems with color and centering. – redmode Nov 15 '12 at 18:11
  • 2
    It could be done with a single `geom_histogram` if you made `cond` a column `df$cond <- df$wins == tail(df,1)$wins` and used it inside `aes`, `ggplot(df, aes(x=wins, fill = cond))`. That's the usual way to control colours in ggplot2. – nacnudus Sep 11 '17 at 14:34
  • @nacnudus but how do you set the color of that specific bar you want to change?? – Seymour Apr 10 '18 at 14:55
  • @Seymour see my answer. – nacnudus Apr 10 '18 at 20:18
2

You can do this with a single geom_histogram() by using aes(fill = cond).

To choose different colours, use one of the scale_fill_*() functions, e.g. scale_fill_manual(values = c("red", "blue").

library(ggplot2)
df <- data.frame(wins=c(1,1,3,1,1,2,11,2,11,15,1,1,3))
df$cond <- df$wins == tail(df,1)$wins
ggplot(df, aes(x=wins, fill = cond)) +
  geom_histogram() +
  scale_x_continuous(breaks=df$wins+0.25, labels=df$wins) +
  scale_fill_manual(values = c("red", "blue"))

enter image description here

nacnudus
  • 6,328
  • 5
  • 33
  • 47