R颜色条形图

时间:2014-03-17 07:49:36

标签: r charts bar-chart

我想显示五个国家的统计数据,每个国家每年有4个值(2000年,2004年,2008年,2012年)。 代码很好,但我有一个颜色的主要问题,每个字段的颜色不固定,例如对于一个国家的颜色序列是(蓝色的第一个字段,红色的2ed字段,..)但与另一个国家序列不同。

图例的另一个问题是,只有2个值出现在

以下是代码:

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

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

我尝试使用修复颜色,,,但同样的问题:

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"));

2 个答案:

答案 0 :(得分:0)

问题在于您的子设置。这个命令

as.matrix(schoolenrollment[x:z])

返回五行行的矩阵。最后一行只包含NA个值,因此不会被绘制,但它确实会改变颜色。

因此,要么修复子设置,要么在绘图命令中指定五种颜色:

barplot(as.matrix(schoolenrollment[x:z]), main=country[y],
      ylab= "Total Number", beside=TRUE, col=rainbow(5))

答案 1 :(得分:0)

您的数据不是达到您想要的最佳格式。

请尝试:

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

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

或使用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

或用grobs管理传说。

相关问题