Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Why doesn't it work when I use the same variable for 'x' and 'fill' aesthetic in geom_bar?

$
0
0

Riddle me this: When I use the same variable as x and fill aesthetic for a geom_bar (I know that this is redundant coding), the fill mapping is not used.

library(ggplot2)
df <- data.frame(Points=c(1, 3, rep(5,20), rep(20,80), rep(40,35), rep(70,22), rep(100,18)))
ggplot(df, aes(fill = Points, x = Points)) +
  geom_bar(width=.1) +
  scale_x_log10(breaks=c(1,3,5,10,20,40,70,100))

enter image description here

What's happening?!? I suspect it must have to do something with the inner workings of geom_bar because it's calculating the ..count.. variable for the y-axis. But that shouldn't affect the variable Points, no?

One more thing: It does work, when I wrap an as.character() around Points for the fill aesthetic. But then I don't get a nice continuous color scale but a discrete color scale (which makes sense for a character variable).

Thanks for your help!


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>