2

I have a dataframe similar to this one:

myTable <- "ID Data Group
        1     -50     5.0
        2     -44     5.0
        3     -48     5.0
        4     -50     4.9
        5     -44     4.9
        6     -48     4.9
        7     -48     4.9
        8     -44     4.8
        9     -49     4.8
       10     -48     4.8
       11     -60     4.8
       10     -50     4.8
       11     -80     4.7"
Data <- read.table(text=myTable, header = TRUE)

The data is sorted by ID and Group. The groups do not all have the same size but have at least one member. The data is always negative. What I plan to do, is to make a line graph with the "Group" as x-axis and "Data" as y-axis and it should also show the standard deviation of each group.

I am a R beginner. So my knowledge is very limited, I only have a bit experience with the ggplot2 library. I tried to use the function geom_errorbar but without success. What I tried looked something like this:

require("ggplot2")
pplot <- ggplot(data=data, aes(x=group, y=data))
pplot + geom_errorbar(aes(ymax = <Max of each group>, ymin= <Min of each group> ), width= 0.1)
pplot + geom_line();

So the result should have a form like this graph: enter image description here

schande
  • 576
  • 12
  • 27
  • Do you want max and min of each group? or standard deviations of all observations? Are you basically treating group as a categorical variable for determining error? Using `...` in your sample data ins't helpful. It's easier to provide a solution if you provide a proper [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Feb 14 '18 at 15:35
  • Standard deviation, but it would be interesting to know how to do it with min and max also. Yes the "Group" is like a category for me, they base on measurements, for example group 5.0 is an is an applied voltage of 5 but if I measure it, it don't get exactly 5.0 but more like 5.00254 or 4.99987. So I made this groups to find the standard deviation of the data in each group – schande Feb 14 '18 at 15:43

1 Answers1

2
df <- data.frame(
  ID=1:7,
  Data=c(-50,-44,-48,-50,-44,-48,-48),
  Group=c(5,5,5,4.9,4.9,4.9,4.9)
)

library("ggplot2")

( pplot <- ggplot(data=df, aes(x=df$Group, y=df$Data)) + 
    stat_summary(fun.data = mean_sdl, fun.args = list(mult = 1), geom = "errorbar") +
    stat_summary(fun.y = mean, geom = "line") +
    geom_point() )

Outputs the following plot:

plot

I added geom_point() to show single observations. You can modify the errorbar width with the argument aes(width=0.4).

nx0
  • 56
  • 3
  • 2
    Use `stat_summary(fun.data = mean_sdl, fun.args = list(mult = 1))` to show standard deviations, as requested. – Axeman Feb 14 '18 at 15:56
  • 2
    You need the whole thing, otherwise it will plot roughly two standard deviations.. – Axeman Feb 14 '18 at 16:00
  • 1
    @Axeman I did not know that and just checked, you are absolutely correct. Thank you. – nx0 Feb 14 '18 at 16:06
  • Thanks for all your help, you guys are amazing! I tried it but sadly get the following error: Warning message: Computation failed in `stat_summary()`: Hmisc package required for this function – schande Feb 14 '18 at 16:45
  • If I try to install the package "Hmisc via console with > install.packages("Hmisc") or through Tools/InstallPackages I get following error: Error in install.packages : error reading from connection – schande Feb 14 '18 at 16:56
  • Restarting Rstudio solved the downloading problem. But the first error stays, I've added the line library("Hmisc") – schande Feb 14 '18 at 17:03
  • Updated everything, and it worked like a charm, thank you. – schande Feb 16 '18 at 12:27