R - barplot分组列

时间:2016-01-12 16:13:43

标签: r bar-chart

我有以下数据,其中包含7种组合(行)和12种方法(列)的数据。

  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))

我想得到一个如下图所示的条形图:

enter image description here

我试过

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)

但是我收到了这个错误:

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

你能找到错误吗?

3 个答案:

答案 0 :(得分:1)

我已在此为您命名数据框SELECT * FROM ( SELECT MAX(d.id) AS max_id FROM ( SELECT df.id FROM ( SELECT xf.from , xf.to , MAX(xf.sent) AS max_date FROM `HELLO-MESSAGES` xf WHERE xf.from = '" . $_SESSION['ID'] . "' GROUP BY xf.from, xf.to ) mf JOIN `HELLO-MESSAGES` df ON df.from = mf.from AND df.to = mf.to AND df.sent = mf.max_date UNION ALL SELECT dt.id FROM ( SELECT xt.from , xt.to , MAX(xt.sent) AS max_date FROM `HELLO-MESSAGES` xt WHERE xt.to = '" . $_SESSION['ID'] . "' GROUP BY xt.from, xt.to ) mt JOIN `HELLO-MESSAGES` dt ON dt.from = mt.from AND dt.to = mt.to AND dt.sent = mt.max_date ) q1 JOIN `HELLO-MESSAGES` d ON d.id = q1.id GROUP BY IF(d.from<d.to,d.from,d.to),IF(d.from<d.to,d.to,d.from) ) q2 JOIN `HELLO-MESSAGES` r ON r.id = q2.max_id ORDER BY r.sent DESC, r.id DESC ,并使用了三个软件包。这不是基础R解决方案。根据您的数据集格式,这是执行此操作的最简单方法(IMO):

df

enter image description here

答案 1 :(得分:1)

如果你想使用基础图形而不是ggplot2

,这应该可以帮到你
df <- as.matrix(dat[,-1])
rownames(df) <- dat[, 1]

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

答案 2 :(得分:0)

使用ggplot2包并确保您的数据整洁有序?

类似ggplot(dataframe,aes(color = some_factor)))+ geom_bar(aes(x = Some_variable,y = Some_other_variable))

关于数据如何与图像匹配的更多明确陈述将非常有用。