0

I want to show statistics of five countries, to each country I have 4 values of each year (2000, 2004, 2008, 2012). The code is fine, but I have one main problem with colors, the color of each field is not fix, for example for one country the color sequence is (blue for 1st filed, red for 2ed filed, ..) but with another country the sequence is different.

another problem with legend, only 2 values out of 5 appear

Here is the code :

par(mfrow=c(3,2),oma=c(5,0,0,0),xpd=NA)
par(mar=c(4,4,2,2))
schoolenrollment <- read.csv(file.choose(), header=T, sep=",")
country <- c("Comoros","Jordan","United Arab Emirates","Egypt"," Qatar")
y=1
z=4
for (x in seq(from=1, to=20, by=4))
{
  barplot(as.matrix(schoolenrollment[x:z]), main=country[y],
          ylab= "Total Number", beside=TRUE, col=rainbow(4))
  y=y+1
  z=z+4  
}
legend(-0.5, 3.5, ncol=2,
       c("School enrollment, preprimary (% gross)",
         "  School enrollment, primary (% gross)",
         "  School enrollment, secondary (% gross)",
         "School enrollment, secondary(% gross)",
         "School enrollment, tertiary (% gross)"),
       cex=0.9,  bty="n", fill=rainbow(4));

output graph

dataset: https://drive.google.com/file/d/0B1NJGKqdrgRta0R2ZFlZemVtRFE/edit?usp=sharing

I tried to use fix colors ,,, but its the same problem:

par(mfrow=c(3,2),oma=c(5,0,0,0),xpd=NA)
par(mar=c(4,4,2,2))
schoolenrollment<- read.csv(file.choose(), header=T, sep=",")
country<- c("Comoros","Jordan","United Arab Emirates","Egypt"," Qatar")
y=1
z=4
for (x in seq(from=1, to=20, by=4))
{
  barplot(as.matrix(schoolenrollment[x:z]), main=country[y], ylab= "Total Number",
          beside=TRUE, col=c("red","blue","green","yellow"))
  y=y+1
z=z+4  
}
legend(-0.5,3.5,ncol=2, c("School enrollment, preprimary (% gross)", "  School enrollment, primary (% gross)"," School enrollment, secondary (% gross)","School enrollment, secondary(% gross)", "School enrollment, tertiary (% gross)"), cex=0.9,  bty="n", fillc("red","blue","green","yellow"));
almegdadi
  • 79
  • 13

2 Answers2

1

The problem is with your sub-setting. This command

as.matrix(schoolenrollment[x:z])

returns a matrix with five rows. The final row only contains NA values, so isn't plotted, but it does change the colours.

So either fix your sub-setting or specify five colours in the plotting command:

barplot(as.matrix(schoolenrollment[x:z]), main=country[y],
      ylab= "Total Number", beside=TRUE, col=rainbow(5))
csgillespie
  • 59,189
  • 14
  • 150
  • 185
  • great, it is working when change the rainbow colors to 5..Thank you do u have suggestion for legend issue ? – almegdadi Mar 17 '14 at 09:25
1

Your data isn't in the best format to achieve what you want.

Please try:

dd <- read.table(file = '\Stackoverflow\\22449135\\schoolenrollment.csv',
                 header = T, dec = '.', sep = ',')

d <- data.frame('Value'=unname(as.matrix(unlist(dd[1:4, ]),ncol = 1,
                                         nrow=80, byrow = F))[,1],
                'Country' =rep(c('Comoros', 'Jordan', 'U A Emirates',
                                 'Egypt', 'Qatar'), each = 16),
                'Year' =rep(c(2000, 2004, 2008, 2012), each = 4, times = 5),
                'Enrollment' = rep(c("Prelimary", "Primary", "Secondary",
                                     "Tertiary"), times = 5))
library(ggplot2)
ggplot(data = d) +
  geom_bar(aes(x=factor(Year), y=Value, fill = Enrollment),
           stat = 'identity', position = 'dodge') +
  facet_wrap(~Country) +
  labs(list(x = 'Year', y = '% gross'))

ggplot1

or

ggplot(data = d) +
  geom_bar(aes(x=factor(Year), y=Value, fill = Enrollment),
           stat = 'identity', position = 'dodge') +
  facet_grid(Country ~.) +
  labs(list(x = 'Year', y = '% gross'))

ggplot2

or with gridExtra::grid.arrange

g1 <- ggplot(data = d[d$Country == 'Comoros', ]) +
  geom_bar(aes(x=factor(Year), y=Value, fill = Enrollment),
           stat = 'identity', position = 'dodge') +
  labs(list(x = 'Year', y = '% gross'))

g2 <- ggplot(data = d[d$Country == 'Jordan', ]) +
  geom_bar(aes(x=factor(Year), y=Value, fill = Enrollment),
           stat = 'identity', position = 'dodge') +
  labs(list(x = 'Year', y = '% gross'))

g3 <- ggplot(data = d[d$Country == 'U A Emirates', ]) +
  geom_bar(aes(x=factor(Year), y=Value, fill = Enrollment),
           stat = 'identity', position = 'dodge') +
  labs(list(x = 'Year', y = '% gross'))
grid.arrange(g1,g2, g3)

arrangeGrid ggplot

or manage legends with grobs.

Paulo E. Cardoso
  • 5,778
  • 32
  • 42
  • wow,, i think this is better visualization method ,, I change the "dd" to schoolenrollment then it is work ... Can I add the year values (2000,2004,2008,2012)for each sub-graph ? – almegdadi Mar 17 '14 at 09:36
  • @almegdadi I don't think you need to. That's the nature of faceting in ggplot when representing a single variable at x axis. You may want to split it into 5 bar plots and then work with ?gridExtra::grid.arrange instead. – Paulo E. Cardoso Mar 17 '14 at 09:43
  • @almegdadi just to give you an idea of how [tricky](http://stackoverflow.com/a/13316126/640783) it may be adjusting x axis labels with faceting. A `grid.arrange`slution would be much easier. Please let me know if you want this option too. – Paulo E. Cardoso Mar 17 '14 at 10:03
  • Thank you,, your answers help me more than i expect :D – almegdadi Mar 17 '14 at 10:57