2

I have the following data which contains data from 7 combinations (rows) and 12 methods (columns).

  structure(list(Beams = structure(c(1L, 3L, 4L, 5L, 6L, 7L, 2L
), .Label = c("1 – 2", "1 – 2 – 3 – 4", "1 – 3", "1 – 4", "2 – 3", 
"2 – 4", "3 – 4"), class = "factor"), Slope...No.weight = c(75L, 
65L, 45L, 30L, 95L, 70L, 75L), Slope...W1 = c(85L, 70L, 65L, 
55L, 90L, 85L, 75L), Slope...W2 = c(80L, 65L, 65L, 50L, 90L, 
90L, 75L), Slope...W3 = c(80L, 75L, 75L, 65L, 90L, 95L, 80L), 
Average.Time...No.Weight = c(75L, 65L, 45L, 30L, 95L, 70L, 
70L), Average.Time...W1 = c(70L, 60L, 75L, 60L, 75L, 75L, 
80L), Average.Time...W2 = c(65L, 40L, 65L, 50L, 75L, 85L, 
70L), Average.Time...W3 = c(65L, 40L, 80L, 75L, 65L, 85L, 
80L), Momentum...No.weight = c(80L, 60L, 45L, 30L, 95L, 70L, 
75L), Momentum...W1 = c(85L, 75L, 60L, 55L, 95L, 90L, 80L
), Momentum...W2 = c(80L, 65L, 70L, 50L, 90L, 90L, 85L), 
Momentum...W3 = c(85L, 75L, 75L, 55L, 90L, 95L, 80L)), .Names =   c("Beams", 
"Slope...No.weight", "Slope...W1", "Slope...W2", "Slope...W3", 
"Average.Time...No.Weight", "Average.Time...W1", "Average.Time...W2", 
"Average.Time...W3", "Momentum...No.weight", "Momentum...W1", 
"Momentum...W2", "Momentum...W3"), class = "data.frame", row.names = c(NA, 
-7L))

I would like to get a barplot like the one below:

enter image description here

I've tried with

library(RColorBrewer)
dat<-read.csv("phaser-p13-30dBm-100ms.csv")

names <- c("1-2","1-3","1-4","2-3","2-4","3-4","1-2-3-4")

barx <- 

 barplot(as.integer(dat2[,2:13]),
      beside=TRUE,
      col=brewer.pal(12,"Set3"),
      names.arg=names,          
      ylim=c(0,100),
      xlab='Combination of beams',
      ylab='Correct detection [%]')



box()
par(xpd=TRUE)
legend("top", c("Slope - No weight","Slope - W1","Slope - W2","Slope - W3","Average Time - No weight","Average Time - W1","Average Time - W2","Average Time - W3","Momentum - No weight","Momentum - W1","Momentum - W2","Momentum - W3"), fill = brewer.pal(12,"Set3"),horiz = T)

but I got this error:

Error in barplot.default(as.integer(dat2[, 2:13]), beside = TRUE, col = brewer.pal(12,  : 
  incorrect number of names

Could you find the error?

3
  • 1
    It would be much easier to help you, if you could provide some data (and thus a working example) which is easily read into R. You can e.g. used dput(dat) (if it's not too big) and copy-paste the output in your question. Commented Jan 12, 2016 at 16:18
  • Can't edit question but the dataframe should be named as df Commented Jan 12, 2016 at 16:37
  • Your error is in the line barplot(t(as.integer(dat2[,2:13])),, this should be changed to barplot(t(as.matrix(dat2[,2:13])),. This makes dat2 a matrix and transposes it. Commented Jan 12, 2016 at 16:50

3 Answers 3

1

I've named you dataframe df here and made use of three packages. This is not a base R solution. Given your dataset format, this is the easiest way (IMO) to do this:

library(dplyr)
library(tidyr)
library(ggplot2)

df %>% # dataframe
  gather(variable, value, -Beams) %>% # convert to long format excluding beams column
  ggplot(aes(x=Beams, y=value, fill=variable)) + # plot the bar plot
  geom_bar(stat='identity', position='dodge')

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

This should get you started, if you wish to use base graphics and not ggplot2:

df <- as.matrix(dat[,-1])
rownames(df) <- dat[, 1]

barplot(df, beside = TRUE, las = 2)

Comments

0

Use ggplot2 package and make sure that your data is neat and ordered?

something like ggplot(dataframe, aes(colour = some_factor))) + geom_bar(aes(x=Some_variable, y=Some_other_variable))

More explict statement as to how your data matches the image would be useful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.