不确定ggplot2输入

时间:2019-04-04 01:16:05

标签: r ggplot2

我有一个非常简单的数据框。它是用于员工的技能矩阵,其中包含用户技能和35-36列IT技能列,排名从0-5。我汇总了每一列,然后按技能值对它们进行了排序。现在,我正在寻找创建条形图,但不确定将x值放入什么。

我尝试过使用大写和大写字母

将CSV读入R

skillsMatrix <- read.csv(file="skillsmatrix.csv", header=TRUE, sep=",")

colsums查找具有最高价值的技能,按DESC排序

skills <- skillsMatrix[,names(sort(colSums(skillsMatrix[-1:-2]), decreasing = T))]
skills
library(ggplot2)
g <- ggplot(skills, aes(x= colSums(skills)), y=(colnames(skills))) + 
  geom_bar(stat = "identity", colour = "black")
g

预期的结果是获得一个条形图,该条形图以降序显示每个技能的值。

实际结果是此错误:

  

错误:美学的长度必须为1或与数据(55)相同:x

这是str(skills)的一些输出,旨在为您提供一个思路。

> str(skills)
'data.frame':   55 obs. of  35 variables:
 $ SQL                                         : int  4 3 2 3 3 2 3 3 3 4 ...
 $ IIS                                         : int  4 3 2 4 2 1 4 0 2 4 ...
 $ SQL.Server..SSIS..SSAS..SSRS.               : int  3 3 2 3 3 1 3 3 2 3 ...
 $ C.                                          : int  4 4 2 3 2 1 0 0 2 4 ...
 $ .Net..WCF..WPF.                             : int  4 2 1 2 2 2 0 0 2 4 ...
 $ VB..Net                                     : int  4 2 1 3 2 1 0 0 1 4 ...
 $ HTML.5                                      : int  3 4 3 2 1 1 0 2 1 2 ...
 $ Java.Script                                 : int  3 3 2 1 3 1 0 2 1 3 ...
 $ AppInsights                                 : int  1 1 1 3 2 0 3 0 0 3 ...
 $ Angular.JS                                  : int  2 3 2 2 2 0 0 2 2 2 ...

2 个答案:

答案 0 :(得分:1)

美学应与数据长度相同。 skills数据集的维数与美观的维数不同。我们可以创建一个新的数据框,其中包含每种技术的技能总和(以降序排列),然后将其用于绘图。

library(ggplot2)

new_df <- stack(sort(colSums(skills), decreasing = TRUE))

ggplot(new_df) + 
      aes(ind, values) + 
      geom_bar(stat = "identity")

enter image description here

数据

skills <- data.frame(SQL = c(4, 3, 2, 4, 2, 3),IIS = c(5, 1, 2, 4, 5, 5), 
                     Javascript = c(1, 2,3, 4, 5, 5))

答案 1 :(得分:0)

这里是tidyverse的一个选项。我们summarise的列将sum的每一列gather转换为“长”格式,然后使用geom_bar中的ggplot2绘制条形图。

library(tidyverse)
library(ggplot2)
skillsMatrix %>% 
   summarise_all(sum) %>%
   gather %>% 
   ggplot(., aes(key, value)) + 
      geom_bar(stat = "identity")

enter image description here

数据

skillsMatrix <- structure(list(SQL = c(4, 3, 2, 4, 2, 3), IIS = c(5, 1, 2, 4, 
 5, 5), Javascript = c(1, 2, 3, 4, 5, 5)), class = "data.frame", row.names = c(NA, 
  -6L))