Stacked Bar Chart

data <- read.csv('./data/bar_chart.csv')
head(data, 6)
##      x   group         n
## 1 2015  female 337984365
## 2 2015    male 906952243
## 3 2015 unknown 356064463
## 4 2014  female 387202244
## 5 2014    male 625050611
## 6 2014 unknown 149353878
nswidgets::create_bar(
  data = data,
  labels = list( female = 'Female', male = 'Male', unknown = 'Unknown' ),
  axis_labels = list(x = 'Year', y = 'Population'),

  # When you have fewer bars, it may look better with more padding
  padding = 0.5,

  # These are the default values, so you don't need to pass these if the dataframe uses these variable names
  x = 'x',
  y = 'n',
  group = 'group',

  # Optionally specify min and max values
  yValueDomain = list(0, 2400000000)
)

Grouped Bar Chart

head(data, 6)
##      x   group         n
## 1 2015  female 337984365
## 2 2015    male 906952243
## 3 2015 unknown 356064463
## 4 2014  female 387202244
## 5 2014    male 625050611
## 6 2014 unknown 149353878
nswidgets::create_bar(
  data = data,
  axis_labels = list(x = 'Year', y = 'Population'),
  group_mode = 'grouped'
  # Note: without labels, the key values will be used
)

100 Bar Chart

data2 <- read.csv('./data/bar_chart2.csv')
head(data2, 5)
##   x  n
## 1 1 21
## 2 2 11
## 3 3  6
## 4 4 53
## 5 5 27
nswidgets::create_bar(
  data = data2,
  labels = list( n = 'Random Number' ),
  axis_labels = list(x = 'Sequence', y = 'Random Number'),
  x_tick_values = list(1, 25, 50, 75, 100),

  # When you have lots of bars, it may look better with less padding
  padding = 0.3
)