CharlesD
CharlesD

Reputation: 13

How to plot a bar plot with specific values of my dataset?

I'm stuck with a R case study. I need to plot a graph using specific values in my dataset. I have trouble finding the right code. How should I write it?

The dataset is:

data <- read.csv(file = "https://raw.githubusercontent.com/ScPoEcon/ScPoEconometrics/master/inst/datasets/airline-safety.csv")

The question is:

Propose a vizualization showing the evolution of the number of fatal accidents between the two periods

I want to plot a bar plot with my two periods of time in x axis, number of accidents in y axis.

Here is how far I managed to code:

graph_1 <- summarise(group_by(data, type, period), sum_1 =sum(value) )
ggplot((data = graph_1),
aes(x=period, y=type))
geom_bar()

It outputs a graph with: the two periods on the x axis accidents type on the y axis

However, it doesn't use the number of accidents per accident type.

I would expect to have:

Thank you for helping me!

Upvotes: 0

Views: 161

Answers (1)

Parfait
Parfait

Reputation: 107587

Simply add a fill to aesthetic. Usually, you want to include the numeric value in y column and categorical values like type in fill or color.

# SUMMARY DATA
graph_1
# # A tibble: 6 x 3
# # Groups:   type [3]
#   type            period    sum_1
#   <fct>           <fct>     <int>
# 1 fatal_accidents 1985_1999   122
# 2 fatal_accidents 2000_2014    37
# 3 fatalities      1985_1999  6295
# 4 fatalities      2000_2014  3109
# 5 incidents       1985_1999   402
# 6 incidents       2000_2014   231

# PLOT OUTPUT
ggplot((data = graph_1), aes(x=period, y=sum_1, fill=type)) +
  geom_col(position="dodge")  # OR geom_bar(stat="identity", position="dodge")

Bar Plot Output

Upvotes: 1

Related Questions