-1

I have data with only two columns:

Name         Id
Joy           5
Alo           6
Aho           4
Joy           3
Alo           2
Aho           1

If I want to plot a line chart with error bar grouping them with the names:

I use this:

data_summary <- function(data, varname, groupnames){
  require(plyr)
  summary_func <- function(x, col){
    c(mean = mean(x[[col]], na.rm=TRUE),
      #sd = sd(x[[col]], na.rm=TRUE))
  }
  data_sum<-ddply(data, groupnames, .fun=summary_func,
                  varname)
  data_sum <- rename(data_sum, c("mean" = varname))
  return(data_sum)
}

but it gives me this error

Error: All arguments must be named Call rlang::last_error() to see a backtrace Called from: abort("All arguments must be named")

df2 <- data_summary(df, varname="Id", 
                    groupnames=c("Name")

ggplot(df2, aes(x=Name, y=Id, group=keyword)) + 
  geom_line()+
  geom_pointrange(aes(ymin=Id-sd, ymax=Ido+sd))

please if anyone could help to draw the error bar for this data

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
SFS
  • 31
  • 6
  • 1
    Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Nov 29 '18 at 18:46

1 Answers1

0

As Tung commented, in the future please include your data in a format so others can copy/paste to make a reproducible example.

df <- data.frame(Name = c("Joy", "Alo", "Aho", "Joy", "Alo", "Aho"), Id = c(5,6,4,3,2,1))

library(plyr)
library(ggplot2)
data_summary <- function(data, varname, groupnames){
  summary_func <- function(x, col){
    c(mean = mean(x[[col]], na.rm=TRUE),
      sd = sd(x[[col]], na.rm=TRUE))
  }
  data_sum<-ddply(data, groupnames, .fun=summary_func,
                  varname)
  data_sum <- rename(data_sum, c("mean" = varname))
  return(data_sum)
}

df2 <- data_summary(df, varname="Id", 
                    groupnames=c("Name"))

ggplot(df2, aes(x=Name, y=Id)) + 
                      geom_line()+
                      geom_pointrange(aes(ymin=Id-sd, ymax=Id + sd))

I think this is what you're after. Alternate way using dplyr. There were some errors in your code (you commented out the sd calculation, and had a call to a non-existent keyword), but I didn't get the one you posted.

library(dplyr)
library(ggplot2)
df %>% group_by(Name) %>% summarise(mean = mean(Id), sd = sd(Id)) %>% 
  mutate(group = 1) %>% 
  ggplot(aes(x=Name, y=mean)) + 
  geom_pointrange(aes(ymin=mean-sd, ymax=mean + sd)) + geom_line(aes(group = group))

Anonymous coward
  • 2,061
  • 1
  • 16
  • 29