0

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:

  • x axis: period 1985_1999,
  • y axis: N° of accidents with a bar of 122 for fatal accidents.

Thank you for helping me!

3
  • maybe geom_bar(stat="identity") ? Commented Oct 7, 2019 at 16:35
  • 3
    @BenBolker Which is what geom_col() is for, just to make it a bit easier. Commented Oct 7, 2019 at 16:37
  • 1
    @MrFlick I tried both using geom_bar(stat="identity") and geom_col() . The plot is similar to the initial situation. Thank you very much to both of you for helping me! Commented Oct 7, 2019 at 16:49

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for your help! If I understand the code well: you added a layer called "fill" to arrange the plot with the categories of "type". What is geom_col(position="dodge" doing? Thanks again!
Great to hear! Technically, ggplot layers are added with + operator. I simply changed aesthetics mapped in aes() and dodge is to set bars besides each other as default is stacked bars: position="stack".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.