在R中绘制分类变量(x)与连续变量(y)的关系图

时间:2018-03-12 23:12:22

标签: r graph categorical-data

请有人能帮助我绘制数据所需的R代码吗?我一直在努力工作几个小时,在网上搜索,我真的很挣扎。

这是我将数据放入R Studio中的表/矩阵的示例:

      Percentage Correct
4     71.88
20    65.80
40    63.92
60    63.47

4204060是分类变量 - 它们代表不同级别的分类干扰。

Percentage Correct是参与者对每个干扰级别都正确的图像百分比。

例如,63.47表示当干扰类别为60时,所有参与者的正确回答的平均百分比为63.47%。

下面我附上了一些我想要实现的图形图像。我不一定希望我的那种复杂,但就像基本风格一样。

我正在努力实际绘制任何内容,而不会出现任何错误。

非常感谢任何帮助,非常感谢!

Example Graph 1

Example Graph 2

1 个答案:

答案 0 :(得分:1)

以下是使用ggplot2开始的可能性:

# Your sample data
df <- read.table(text =
    "x y
4     71.88
20    65.80
40    63.92
60    63.47", header = T);

library(ggplot2);
ggplot(df, aes(as.factor(x), y)) + 
    geom_point() + 
    labs(y = "Percentage correct", x = "Categorical variable");

enter image description here

如果您将x变为factor,则会将其视为分类变量。

或作为条形图(由@neilfws建议):

library(ggplot2);
ggplot(df, aes(as.factor(x), y)) +
    geom_bar(stat = "identity") + 
    labs(y = "Percentage correct", x = "Categorical variable");

enter image description here