简体   繁体   中英

bar chart within 2 group variables using ggplot

The idea was to develop a chart where

1- It is the mix between the two charts here:

在此处输入图片说明

The chart is supposed to have for "Branco00" variable group the data showed as in the first chart. For the "Year" variable group, the data is supposed to be showed up as on the second chart, ie, would be like a "fill" by "Branco00" onto "Year".

I tried:

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

However, it gets close what I am looking for, however it did not separate by year on different bars but on the same ones, such as here: 在此处输入图片说明

Since it seems you intend to unstack the numeric bars and then organize by Year . Consider geom_bar(...) , specifiying dodge position, instead of geom_col() and then run grouping variable, Year , in facet_wrap() .

To demonstrate below data uses the populations of Brazil's states (as your data seems to include), pulled from Wikipedia's List of Brazilian states by population for 2000, 2010, and 2014. I include a Branco00 variable equal to each other for each state.

Data

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

Original Output (reproducing OP's similar structure)

library(ggplot2)

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

原始图输出

Adjusted Output (with scales package to adjustment of Y axis and rotating X labels)

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)

调整图输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM