使用ggplot

时间:2018-04-11 20:14:33

标签: r ggplot2 bar-chart

想法是开发一个图表

1-这是两个图表之间的混合:

enter image description here

该图表应该用于" Branco00"变量组数据显示在第一个图表中。对于"年"变量组,数据应该显示在第二个图表上,即,就像一个" fill" by" Branco00"到"年"。

我试过了:

g <- ggplot(tabDummy19, aes(X, Y, group = Year, fill = Branco00))
g + geom_col()

然而,它接近我正在寻找的东西,但它并没有按年份在不同的栏上分开,而是在相同的栏上,例如: enter image description here

1 个答案:

答案 0 :(得分:0)

因为您似乎打算取消堆叠数字栏,然后按进行整理。考虑geom_bar(...),指定道奇位置,而不是geom_col(),然后在facet_wrap()中运行分组变量 Year

为了证明以下数据,使用巴西各州的人口(如您的数据似乎包含),从2000年,2010年和2014年的Wikipedia's List of Brazilian states by population拉出。我包含 Branco00 变量等于每个州相互对立。

数据

txt = 'UF,Year,Population
SãoPaulo,2014,44035304
MinasGerais,2014,20734097
RiodeJaneiro,2014,16461173
Bahia,2014,15126371
RioGrandedoSul,2014,11207274
Paraná,2014,11081692
Pernambuco,2014,9277727
Ceará,2014,8842791
Pará,2014,8073924
Maranhão,2014,6850884
SantaCatarina,2014,6727148
Goiás,2014,6523222
Paraíba,2014,3943885
EspíritoSanto,2014,3885049
Amazonas,2014,3873743
RioGrandedoNorte,2014,3408510
Alagoas,2014,3321730
MatoGrosso,2014,3224357
Piauí,2014,3194718
DistritoFederal,2014,2852372
MatoGrossodoSul,2014,2619657
Sergipe,2014,2219574
Rondônia,2014,1748531
Tocantins,2014,1496880
Acre,2014,790101
Amapá,2014,750912
Roraima,2014,496936
SãoPaulo,2010,41262199
MinasGerais,2010,19597330
RiodeJaneiro,2010,15989929
Bahia,2010,14016906
RioGrandedoSul,2010,10693929
Paraná,2010,10444526
Pernambuco,2010,8796448
Ceará,2010,8452381
Pará,2010,7581051
Maranhão,2010,6574789
SantaCatarina,2010,6248436
Goiás,2010,6003788
Paraíba,2010,3766528
EspíritoSanto,2010,3512672
Amazonas,2010,3483985
RioGrandedoNorte,2010,3168027
Alagoas,2010,3120494
MatoGrosso,2010,3035122
Piauí,2010,3118360
DistritoFederal,2010,2570160
MatoGrossodoSul,2010,2449024
Sergipe,2010,2068017
Rondônia,2010,1562409
Tocantins,2010,1383445
Acre,2010,733559
Amapá,2010,669526
Roraima,2010,450479
SãoPaulo,2000,37032403
MinasGerais,2000,17891494
RiodeJaneiro,2000,14391282
Bahia,2000,13070250
RioGrandedoSul,2000,10187798
Paraná,2000,9569458
Pernambuco,2000,7918344
Ceará,2000,7430661
Pará,2000,6192307
Maranhão,2000,5651475
SantaCatarina,2000,5356360
Goiás,2000,5003228
Paraíba,2000,3443825
EspíritoSanto,2000,3097232
Amazonas,2000,2812557
RioGrandedoNorte,2000,2776782
Alagoas,2000,2822621
MatoGrosso,2000,2504353
Piauí,2000,2843278
DistritoFederal,2000,2051146
MatoGrossodoSul,2000,2078001
Sergipe,2000,1784475
Rondônia,2000,1379787
Tocantins,2000,1157098
Acre,2000,557526
Amapá,2000,477032
Roraima,2000,324397'

# STACK EQUAL-LENGTH BRANCO AND NEGRO DFS
brazil_pop_df <- rbind(transform(read.csv(text=txt, header=TRUE), Branco00 = "Branco"),
                       transform(read.csv(text=txt, header=TRUE), Branco00 = "Negro"))

原始输出 (再现OP的类似结构)

library(ggplot2)

ggplot(brazil_pop_df, aes(UF, Population, group = Year, fill = Branco00)) +
  geom_col()

Original Plot Output

调整后的输出 (使用比例包调整Y轴并旋转X标签)

library(ggplot2)
library(scales)

ggplot(brazil_pop_df, aes(UF, Population, fill = Branco00)) +
  geom_bar(stat="identity", position="dodge") + 
  scale_y_continuous(labels = comma, expand = c(0, 0), limits = c(0, 50000000)) + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  facet_grid(. ~ Year)

Adjusted Plot Output