Barplot in R: Help with the arguments to create a more complete graph

Asked

Viewed 44 times

-1

Hello. I need to create the two charts below in this exact way. Certain adjustments, for example:

  • the creation of a value of 7 in the x-axis, which has been omitted;
  • the horizontal axis with traces of each value;
  • the rotated label of each bar in the second chart;
  • the grid lines of the chart background, relative to each value of the y-axis and arrows of the axes;

Chart 1:

Gráfico 1

Graph 2: Gráfico 2

What I’ve done so far:

Acidentes_ATA <- read_excel("~/R/PROVA MAURI/Acidentes ATA.xlsx")
View(Acidentes_ATA)
dput(Acidentes_ATA)
structure(list(Acidentes = c(3, 5, 4, 0, 4, 4, 4, 1, 4, 2, 4, 
3, 8, 6, 4, 4, 1, 4, 1, 3, 3, 1, 2, 2, 6, 0, 2, 1, 3, 2), Ano = c(1977, 
1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 
1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 
2000, 2001, 2002, 2003, 2004, 2005, 2006)), row.names = c(NA, 
-30L), class = c("tbl_df", "tbl", "data.frame"))

frequencia_acidentes <- table(Acidentes_ATA$Acidentes)
barplot(frequencia_acidentes, ylab="Frequency", xlab="Number of Accidents", ylim=c(0, 10), col="skyblue1", space=0, main="Fatal Commercial Airplane Accidents per Year (1977-2006)")
barplot(dbinom(0:12, 11000000, 0.0000004), ylab="Probability", xlab="Number of Accidents", xlim=c(0,13), ylim=c(0,0.25), col="skyblue1", space=0)

Meu Gráfico 1 Meu Gráfico 2

  • Welcome to Stackoverflow! Unfortunately, this question cannot be reproduced by anyone trying to answer it. Please take a look at this link (mainly in the use of function dput) and see how to ask a reproducible question in R. So, people who wish to help you will be able to do this in the best possible way.

  • Edited. Many thanks and pardon the stupidity.

2 answers

1

I got some parameters. Others are missing.

barplot(frequencia_acidentes, ylab="Frequency", xlab="Number of Accidents", ylim=c(0, 10), col="skyblue1", space=0, main="Fatal Commercial Airplane Accidents per Year (1977-2006)")
grid(nx=NA, ny=NULL, lty=1)
barplot(frequencia_acidentes, ylab="Frequency", xlab="Number of Accidents", ylim=c(0, 10), col="skyblue1", space=0, main="Fatal Commercial Airplane Accidents per Year (1977-2006)", add=TRUE)
axis(2, at=seq(0,9,1), labels=seq(0,9))
abline(h=0)
distribuição_1 <- dbinom(0:12, 11000000, 0.0000004)
barplot(distribuição_1, ylab="Probability", xlab="Number of Accidents", xlim=c(0,13), ylim=c(0,0.25), col="skyblue1", space=0)
axis(side=1, at=seq(1,12), labels=seq(1,12), cex.axis=1)
grid(nx=NA, ny=NULL, lty=1)
barplot(distribuição_1, ylab="Probability", xlab="Number of Accidents", xlim=c(0,13), ylim=c(0,0.25), col="skyblue1", space=0, add=TRUE)
abline(h=0)

Gráfico 1 In the above missing include the bar of the number 7 (which has frequency 0 and the R omitted), and trace the lower lines.

Gráfico 2 In this absence center the values of the x-axis (center of the bars), plot the labels of each bar (probabilities).

  • 1

    To have bar 7, try table(factor(Acidentes_ATA$Acidentes, levels = 0:8)). This includes the value 7 with zero frequency in case count (table).

  • It worked. Thank you

0

I got the text bars that way, through trial and error:

text(x=c(0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5), y=c(0.0125, 0.054, 0.08, 0.135, 0.152, 0.13, 0.085, 0.078, 0.043, 0.021, 0.009, 0.004, 0.001), labels=c("0.012", "0.054", "0.119", "0.174", "0.192", "0.169", "0.124", "0.078", "0.043", "0.021", "0.009", "0.004", "0.001"), srt=90, adj=0)

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.