Chart of average profiles (including error bars)

Asked

Viewed 3,756 times

5

I have a dataset that I titled Amazonia, which can be downloaded in this link. I need to construct a chart of average profiles (including error bars) of the newborn weight variable ('weight' column), according to parasite type group ('group' column where 0 = control, 1 = Vivax, 2 = falciparum, 3 = mixed). Code I used to separate the weight of the babies by each group:

controle = malaria$peso[malaria['grupo']==0] vivax = malaria$peso[malaria['grupo']==1] falciparum = malaria$peso[malaria['grupo']==2] mista = malaria$peso[malaria['grupo']==3]

I need to plot the average of these four groups, interconnected by a line and with bars, around this point of mean, which inform the standard deviation of each group. It would be a graph like the figure below:

inserir a descrição da imagem aqui

  • Try the sciplot package, available in Cran.

2 answers

5


I recommend using the packages ggplot2 and Rmisc to make this graph. The first package makes the graph itself, while the second prepares the data for analysis. Below I will explain step by step how I built the desired chart.

First of all, I use the function summarySE to obtain the means and standard errors of the desired data set. Note that it was enough to enter the name of the data frame, the response variable and the grouping variable to get what we want.

library(ggplot2)
library(Rmisc)

malaria.plot <- summarySE(malaria, measurevar="peso", groupvars="grupo", na.rm=TRUE)

Then, for the graph to stay with the x-axis Abels with the correct names, without the use of numbers, I converted the column grupo factor. Below I show the final result of this data preparation.

malaria.plot$grupo <- factor(c("Controle", "Vivax", "Falciparum", "Mista"), 
  levels=c("Controle", "Vivax", "Falciparum", "Mista"))
malaria.plot
       grupo   N     peso       sd       se        ci
1   Controle 206 3225.830 510.6585 35.57927  70.14821
2      Vivax 173 3134.098 508.4377 38.65580  76.30084
3 Falciparum 100 3122.550 512.6327 51.26327 101.71744
4      Mista  56 3144.696 489.7987 65.45211 131.16896

With the data ready, just do the graph. I used the function ggplot along with geom_errorbar, taking into account the calculations in malaria.plot. Note that I am not filling the original dataset, but rather the transformation I did in the data. I also use geom_line and geom_point to make the dots and lines by joining them. Finally, labs adds the names to the axes.

ggplot(malaria.plot, aes(x=grupo, y=peso, group=1)) + 
  geom_errorbar(aes(ymin=peso-se, ymax=peso+se), width=.1) +
  geom_line() +
  geom_point() +
  labs(x="Grupo", y="Peso (kg)")

inserir a descrição da imagem aqui

If you don’t want the gray background in the image, add + theme_bw() to the above command. Other chart details can be adjusted by looking for ggplot2.

3

It is possible to use the function stat_summary(), ggplot2, to summarize the observations. Using ggplot2, you do not need to separate the groups into several vectors, simply indicate the group=static parameter in each of the instructions.

First, I previously converted the groups to a given factor type, assigning the appropriate levels as you indicate in the separation of the groups:

malaria$grupo = as.factor(malaria$grupo)
levels(malaria$grupo) = c("controle","vivax","falciparum","mista")

Then I plotted the graph from the averages as dots, used the mean_cl_normal() function to generate the confidence intervals and plot them as error bars, and connected the averages using a dashed line - each of these operations using only stat_summary():

ggplot(malaria,aes(group=grupo,y=peso,x=grupo)) + 
 stat_summary(fun.y=mean,geom="point") + 
 stat_summary(fun.data=mean_cl_normal, geom="errorbar") + 
 stat_summary(fun.y=mean,geom="line",aes(group=""),linetype="dashed")

Gráfico gerado para comparar peso ao nascer e grupos de Malária.

More information on stat_summary() can be found in the ggplot2 documentation.

Browser other questions tagged

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