Skip to content

Instantly share code, notes, and snippets.

@rtanglao
Created May 6, 2015 09:46
Show Gist options
  • Save rtanglao/a0bf92971d158c9876fd to your computer and use it in GitHub Desktop.
Save rtanglao/a0bf92971d158c9876fd to your computer and use it in GitHub Desktop.
correct bar graph using ggplot
ggplot(sorted3,aes(x=colorname,y=count,fill=as.character(sorted3$colorname)))+
stat_summary(fun.y=identity,geom="histogram")+
scale_fill_manual(values=c(as.character(sorted3$colorname)),+
name= "My name", guide = guide_legend(reverse = TRUE))+
scale_x_discrete(limits = sorted3$colorname)
@rtanglao
Copy link
Author

rtanglao commented May 6, 2015

the colours are wrong! everything else is correct!

@rtanglao
Copy link
Author

rtanglao commented May 6, 2015

so colours for labelling needs to be in hex not colour names!

c3 <- c("#1F78B4", "#F03B20", "#31A354")

ggplot(sorted3,aes(x=colorname,y=count,fill=c3))+ stat_summary(fun.y=identity,geom="histogram")+scale_x_discrete(limits = sorted3$colorname)+scale_fill_manual("Result", values = c3)

@rtanglao
Copy link
Author

rtanglao commented May 6, 2015

  1. sapply(d, identity) to make named vector
  2. https://gist.github.com/mmparker/6315074 - how to make named colors in ggplot2

@rtanglao
Copy link
Author

rtanglao commented May 6, 2015

getnumericColour <- function(colorname) {

  • colour_matrix=col2rgb(colorname)
  • return(as.numeric(colour_matrix[1,1]) * 65536 +
  • as.numeric(colour_matrix[2,1]) * 256 +
  • as.numeric(colour_matrix[3,1]))
  • }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment