Mikata-Project / ggthemr

Themes for ggplot2.
893 stars 108 forks source link

Error: Your data requires 16 colours but the swatch provided by ggthemr only has 8 #16

Closed KimJenkins closed 7 years ago

KimJenkins commented 7 years ago

I am trying to plot a bar plot with 16 bins. I am getting an error

Error: Your data requires 16 colours but the swatch provided by ggthemr only has 8. To resolve this you can provide an appropriate vector of colours to your scale or expand the swatch using set_swatch(). Note: if you are using a custom palette, the first swatch colour is separate from the others. See the documentation for define_palette() and/or set_swatch()

This is my code below

ggthemr('fresh', layout = 'scientific', spacing = 2, type = 'inner')
 ggplot(data=c1, aes(x=Var1, y=Freq, fill=Var1)) +
      geom_bar(stat="identity",fill = pal) +
   ylab("Count") + 
   ggtitle("Cryo") +
   theme(axis.text.x=element_text(angle=45,hjust=1,vjust=0.5))+
   geom_text(aes(y=Freq, label = Freq), vjust=1.6, color="black",  size=3.5)

This is my dataset

c1 <- structure(list(Var1 = structure(1:16, .Label = c("(-Inf,0]", 
"(0,100]", "(100,200]", "(200,300]", "(300,400]", "(400,500]", 
"(500,600]", "(600,700]", "(700,800]", "(800,900]", "(900,1e+03]", 
"(1e+03,1.1e+03]", "(1.1e+03,1.2e+03]", "(1.2e+03,1.3e+03]", 
"(1.3e+03,1.4e+03]", "(1.4e+03, Inf]"), class = "factor"), Freq = c(1652L, 
3L, 20L, 25L, 6L, 6L, 2L, 3L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L)), .Names = c("Var1", 
"Freq"), row.names = c(NA, -16L), class = "data.frame")
frahik commented 7 years ago

To make a graph in terms of elegance and simplicity, colors must adequately express the meaning of the graph itself, 8 colors I think are fair to do that work, so you should consider reducing the colors that You think necessary, maybe two colors will be enough, depending on what you want to express in your graphic.

ggthemr('fresh', layout = 'scientific', spacing = 2, type = 'inner')
 ggplot(data=c1, aes(x=Var1, y=Freq)) +
      geom_bar(stat="identity") +
   ylab("Count") + 
   ggtitle("Cryo") +
   theme(axis.text.x=element_text(angle=45,hjust=1,vjust=0.5))+
   geom_text(aes(y=Freq, label = Freq), vjust=1.6, color="black",  size=3.5)
sainathadapa commented 7 years ago

Thanks @frahik. @KimJenkins If you do really need 16 colors in the plot, you need to define your own custom palette, as described here in the README.