0

I have a large dataset and I am trying to create a stacked bar chart that contains countries and years. Here is what I have right now:

counts <- table(IMDB$country, IMDB$year)
barplot(counts, main="Movies produced in each country by year",
        xlab="Years", col=colorRampPalette(brewer.pal(name="Dark2", n = 8))(15),
        legend = rownames(counts))

How can I make all the countries visible in the legend because in my case not all of them appear, moreover, the colours look too similar so I can't distinguish sometimes between them?

  • 1
    15 colours may be too many for humans to distinguish. The most number provided by the RColorBrewer package is 12 ("Set 3" and "Paired"), but most sets have only 8 or 9. This post may help: https://stackoverflow.com/questions/15282580/how-to-generate-a-number-of-most-distinctive-colors-in-r. And this one (25 colour palette): https://stackoverflow.com/questions/9563711/r-color-palettes-for-many-data-classes/41230685 – Edward Apr 16 '20 at 02:23

1 Answers1

0

You need to set a custom length and width parameter before starting your plot. See ?png() or the related pdf(), svg(), etc.

Your best palette choices are those with a sufficient spread across the color wheel to allow easy discrimination.

Added: Re. your example request, there are many already here and in the examples using "?". However...

png(filename= "rplotpng.png",width=1000,  height=400, units ="px")
plot(1:30,rep(2,30))
dev.off()
# you will find the output in  your working directory.
John Garland
  • 483
  • 3
  • 8