daily pastebin goal
32%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ggplot2 - highlight min/max bar
  2. data= data.frame( Name = c("Joe","Jane", "John") , Value = c(3,2,1) )
  3. ggplot(data=data)+geom_bar(aes_string(x="Name",y="Value"), stat="identity" )
  4.    
  5. p <- ggplot(data=data)+
  6.      geom_bar(aes(x=Name, y=Value), stat="identity") +
  7.      geom_bar(data=subset(data, Value==min(Value)), aes(Name, Value),
  8.               fill="red", stat="identity") +
  9.      geom_bar(data=subset(data, Value==max(Value)), aes(Name, Value),
  10.               fill="green", stat="identity")
  11. print(p)
  12.    
  13. ggplot(data, aes(Name, Value)) +
  14.  geom_bar(stat = 'identity') +
  15.  geom_bar(stat = 'identity', aes(fill = factor(Value)),
  16.    subset = .(Value %in% range(Value))) +    
  17.  scale_fill_manual(values = c('red', 'green'))
  18.    
  19. ggplot(data=data) +
  20.   geom_bar(aes_string(x="Name",y="Value", fill='factor(ifelse(Value==max(Value), 3, ifelse(Value==min(Value), 2, 1)))'), stat="identity" ) +
  21.   scale_fill_manual(values=c('gray20', 'red', 'green'), legend=F)
  22.    
  23. ggplot(data, aes(Name, Value, stat = "identity")) +
  24.   geom_bar() +
  25.   geom_bar(data = data[which.min(data$Value),], fill = "red") +
  26.   geom_bar(data = data[which.max(data$Value),], fill = "green")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top