Masi - 1 year ago 92

R Question

I think R designed tool for the taks is

`ggplot2 stat_summary`

`barplot`

The problem here is the declaration of R table structure with column headers

`ECG 1`

`ECG 2`

`M.1.sum`

`M.2.sum`

I try to do it with

`means.long <- melt(M.1.sum, M.2.sum)`

Each item,

`M.1.sum`

`M.2.sum`

`ids`

My proposal for its table column and row declarations is with

`aes(x=ids, y=value)`

`value`

`ggplot`

Code

`library('ggplot2')`

library('reshape2')

M <- structure(c(-0.21, -0.205, -0.225, -0.49, -0.485, -0.49,

-0.295, -0.295, -0.295, -0.56, -0.575, -0.56, -0.69, -0.67,

-0.67, -0.08, -0.095, -0.095), .Dim = c(3L, 6L))

M2 <- structure(c(-0.121, -0.1205, -0.1225, -0.149, -0.485, -0.49,

-0.295, -0.295, -0.295, -0.56, -0.1575, -0.56, -0.69, -0.67,

-0.117, -0.08, -0.1095, -0.1095), .Dim = c(3L, 6L))

ids <- seq(1,6)

M.1.sum <- colSums(M)

M.2.sum <- colSums(M2)

# http://stackoverflow.com/q/22305023/54964

means.long <- melt(M.1.sum, M.2.sum)

ggplot(means.long, aes(x=ids, y=value ))+ # ,fill=factor(ids))) +

stat_summary(fun.y=mean, geom="bar",position=position_dodge(1)) +

scale_fill_discrete(name="ECG",

breaks=c(1, 2),

labels=c("1", "2"))+

stat_summary(fun.ymin=min,fun.ymax=max,geom="errorbar",

color="grey80",position=position_dodge(1), width=.2) +

xlab("ID")+ylab("Sum potential")

#depreciated because stat_summary designed for the case

#barplot(M.1.sum, ids)

#barplot(M.2.sum, ids)

Output does not look right

Expected output: 6x two columns side by side with legend of two items

Not sure how to use this one

`fill=factor(ids)))`

How can you better make the table?

Pseudocode

`y.sd = sd(each col M) # TODO which data structure suitable for ggplot here?`

# http://www.ats.ucla.edu/stat/r/pages/ggplot2_errorbar.htm

ggplot(means, aes(x = col, y = mean, fill = source)) +

geom_bar(stat = 'identity', position = 'dodge') +

geom_errorbar(data = means,

mapping = aes(x = col, y = means,

ymin = means - y.sd,

ymax = means + y.sd),

size=1,

color="red", width=.4)

I put

`ids`

`= c(1, 777, 2, 4, 5, 6)`

Output wrong now

R: 3.3.1

OS: Debian 8.5

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

With `ggplot`

, it is essential to have a single data frame with everything in it (at least for a single plotting layer, e.g., all the bars in a plot). You create a data frame of the column sums, and then try to use external vectors for the id and the grouping, which makes things difficult.

This is how I would do it:

```
means = rbind(
data.frame(mean = colSums(M), source = "M", col = 1:ncol(M)),
data.frame(mean = colSums(M2), source = "M2", col = 1:ncol(M2))
)
means$col = factor(means$col)
## one nice data frame with everything needed for the plot
means
# mean source col
# 1 -0.6400 M 1
# 2 -1.4650 M 2
# 3 -0.8850 M 3
# 4 -1.6950 M 4
# 5 -2.0300 M 5
# 6 -0.2700 M 6
# 7 -0.3640 M2 1
# 8 -1.1240 M2 2
# 9 -0.8850 M2 3
# 10 -1.2775 M2 4
# 11 -1.4770 M2 5
# 12 -0.2990 M2 6
ggplot(means, aes(x = col, y = mean, fill = source)) +
geom_bar(stat = 'identity', position = 'dodge')
```

You seem to want error bars too. I have no idea what would define those error bars - if you look at `geom_errorbar`

it expects aesthetics `ymin`

and `ymax`

. If you calculate whatever values you want and add them as column to the data frame above, adding the error bar to the plot should be easy.