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)")
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
.
Try the sciplot package, available in Cran.
– Mauricio Camargo