繁体   English   中英

垂直线ggplot用于x分类变量(不是日期)

[英]Vertical Line ggplot for x categorical variable (not date)

我有这个数据框,我试图在x轴上做一个绝对的垂直线。

data <- data.frame(
  condition = c('1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3'),
  AssessmentGrade = c('400', '410', '420', '430', '440', '500', '510', '520', '530', '540', 
                      '300', '310', '320', '330', '340'), 
  Freq = c('1', '2', '1', '5', '7', '9', '1', '5', '3', '4', '5', '8', '1', '3', '5'), 
  MathGrade = c('A+', 'B-', 'C-', 'D', 'F', 'A-', 'B', 'C+', 'D-', 'F', 'A+', 'D', 'D', 'F', 'C'), 
  Condition = c('Condition 1', 'Condition 1', 'Condition 1', 'Condition 1', 'Condition 1', 
                'Condition 2', 'Condition 2', 'Condition 2', 'Condition 2', 'Condition 2', 
                'Condition 3', 'Condition 3', 'Condition 3', 'Condition 3', 'Condition 3'))

我尝试添加一个字段来使等级数字,这有帮助

data$Gradenum <- as.numeric(data$MathGrade)

我使用ggplot来获取abubble图,但我想知道如何编辑它以使用我公司的标准颜色

p <- ggplot(data, aes(x = MathGrade, y = AssessmentGrade, size = Freq, fill = Condition)) +
 geom_point(aes(colour = Condition)) +
 ggtitle("Main Title") +
 labs(x = "First Math Grade", y = "Math Assessment Score")

如何在C +和D之间获得垂直线? 如果您的x轴是日期而不是其他分类值,我会看到很多信息

硬编码解决方案容易出错

MrSnake的解决方案可行 - 但仅适用于给定的数据集,因为7.5的值是硬编码的

仅通过对数据的微小改变就会失败,例如,通过用"A+"替换data的第1行中的等级"A+" "A"

使用7.5的硬编码xintercept

p + geom_vline(xintercept = 7.5)

绘制等级C-C +而不是C +D之间的界线:

在此输入图像描述

这可以使用有序因子来解决。 但首先请注意,图表包含另一个缺陷:x轴上的等级按字母顺序排列

A,A-,A +,B,B-,C,C-,C +,D,D-,F

我原以为预期的地方

A +,A,A-,B,B-,C +,C,C-,D-,D-,F

固定x轴

这可以通过将MathGrade转换为具有给定顺序的级别的有序因子来修复:

grades <- c(as.vector(t(outer(LETTERS[1:4], c("+", "", "-"), paste0))), "F")
grades
  [1] "A+" "A" "A-" "B+" "B" "B-" "C+" "C" "C-" "D+" "D" "D-" "F" 
data$MathGrade <- ordered(data$MathGrade, levels = grades)

factor()足以绘制正确排序的x轴,但我们需要一个有序因子用于下一步,正确放置垂直线。

以编程方式放置垂直线

假设应在C-D +等级之间绘制垂直线。 但是,可能会发生数据中缺少其中一个或两个等级的情况。 不会绘制缺失因子。 在样本数据集中,没有D +等级的数据,因此应在C-D等级之间绘制垂直线。

因此,我们需要在数据集中寻找等于或大于D +的最低等级以及等于或低于C-的最高等级:

upper <- as.character(min(data$MathGrade[data$MathGrade >= "D+"]))
lower <- as.character(max(data$MathGrade[data$MathGrade <= "C-"]))

这些是实际数据集中的等级,其中垂直线绘制在:

xintercpt <- mean(which(levels(droplevels(data$MathGrade)) %in% c(lower, upper)))
p + geom_vline(xintercept = xintercpt)

在此输入图像描述

只需添加geom_vline ;)

p + geom_vline(xintercept = 7.5)

在此输入图像描述

要更改颜色以适合您的公司方案,您可以添加以下内容:

  + scale_color_manual(values = c('Condition 1' = 'grey20', 
                                'Condition 2' = 'darkred', 
                                'Condition 3' = 'blue'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM