李克特堆叠条形图

likert stacked bar chart

我是 R 的新手。我浏览了几个关于如何在 R 中制作李克特堆叠条形图的网站(以及本网站上的一个问题)。我一个都不懂。每个示例都有无数的命令。就好像他们在向我展示如何通过包括他们能想到的每一个可能的属性来绘图,而我想要的只是一个答案:plot(x,y)

为简单起见,假设我的数据有 2 个问题,按照 3pt Likert 量表(A、B 和 C)排列在 CSV 中,如下所示:

        A    B    C
  Q1   25   31   56
  Q2   73   19    4

数字代表用该回答回答问题的人数。例如,对于问题 #2,有 19 人选择了李克特答案 B。

可以从中创建堆积条形图的最短命令数是多少?

这应该让您了解步骤:

Question <- c("Q1", "Q2")
A <- c(25,73)
B <- c(31,19)
C <- c(56,4)


data <- data.frame(Question, A, B, C)


# Install the "reshape" package
install.packages("reshape")

# Load reshape package into working directory

library(reshape)

# Melt data to long format

data.melt <- melt(data, id = ("Question"), measure.vars = c("A", "B", "C"))

# Install ggplot2 package

install.packages("ggplot2")

# Load ggplot2 package into working directory

library(ggplot2)

# Create your figure

ggplot(data.melt, aes(x = Question, y = value, fill = variable)) +
    geom_bar(stat = "identity")