I recommend using the ggplot2
and Rmisc
packages to make this chart. The first packet makes the graph itself, while the second packet prepares the data for analysis. Below I will explain the step by step how I built the desired graphic.
First, I use the summarySE
function to get the default averages and errors of the desired dataset. You just have to enter the name of the data frame, the response variable, and the collation variable to get what we want.
library(ggplot2)
library(Rmisc)
malaria.plot <- summarySE(malaria, measurevar="peso", groupvars="grupo", na.rm=TRUE)
Then, for the chart to get the x-axis labels with the correct names, without the use of numbers, I converted the grupo
column to factor. Below is 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 make the chart. I used the ggplot
function together with geom_errorbar
, considering the calculations in malaria.plot
. Notice that I'm not plotting the original data set, but the transformation I made to the data. I also use geom_line
and geom_point
to make the points and the 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 do not want the gray background in the image, add + theme_bw()
to the above command. Other graph details can be adjusted by looking for ggplot2
help.