R中数据集的Sum Barplot

时间:2014-12-01 23:00:26

标签: r bar-chart

假设我有一个包含100列和200行的csv数据集。

每行都是观察,列是从1-5的李克特量表(分类)测量的问题。

现在我想制作一个条形图,其中y轴为频率,x轴为1-5类。所以最后我只想清楚地了解哪些类别被最多或最少使用而没有任何进一步的细节。

对不起,这一定非常简单,但如果我搜索解决方案,我大多数都会看到超级精彩的多个或堆积的条形图。

所以基本上,如果*被定义为所有列,我需要像:

barplot(dataset$*)  

其他评论:

当我回答这些问题时,上面的代码适用于1列f.e。

barplot(dataset$column1)

然后我玩了一下,突然频率现在在x轴上,它看起来很奇怪,但它的代码相同。这怎么可能呢? enter image description here

如果我将代码更改为:

barplot(xtabs(~dataset$column1))

我会得到与以前相同的结果: enter image description here

1 个答案:

答案 0 :(得分:2)

两个并排的条形图

x <- c(0.0001, 0.0059, 0.0855, 0.4082) 
y <- c(0.54, 0.813, 0.379, 0.35) 

# create a two row matrix with x and y 
height <- rbind(x, y) 

# Use height and set 'beside = TRUE' to get pairs 
# save the bar midpoints in 'mp' 
# Set the bar pair labels to A:D 
mp <- barplot(height, beside = TRUE,  
              ylim = c(0, 1.1),  
              main="Species 8472 damage taken by Cube and Battle", 
              names.arg = c("Battle 1", "Battle 2", "Battle 3", "Battle 4"),  
              col = c("#999999", "#56B4E9")) 

legend("topleft", 
       legend = c("Cube 17", "Cube 19"), 
       fill = c("#999999", "#56B4E9")) 

# Draw the bar values above the bars 
text(mp, height, labels = format(height, 5), pos = 3, cex = .75) 

R barplot of two vectors broken out by 2 categories

两套的条形图,堆叠

x <- c(0.0001, 0.0059, 0.0855, 0.4082) 
y <- c(0.54, 0.813, 0.379, 0.35) 

height <- rbind(x, y) 
thesum = colSums(rbind(x, y)) 

mp <- barplot(height, beside = FALSE,  
              main="Species 8472 cumulative damage taken, by Cube and Battle", 
              ylim = c(0, 1.1),  
              names.arg = c("Battle 1", "Battle 2", "Battle 3", "Battle 4"),  
              col = c("#999999", "#56B4E9")) 

legend("topleft", 
       legend = c("Cube 17", "Cube 19"), 
       fill = c("#999999", "#56B4E9")) 
text(mp, thesum, labels = format(thesum, 5), pos = 3, cex = .75)

R barplot of stacked vectors broken out by 2 categories