简体   繁体   English

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

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

I have this dataframe that I'm trying to make a vertical line on an x-axis that is categorical. 我有这个数据框,我试图在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'))

I tried adding a field to make grade numeric and that helped 我尝试添加一个字段来使等级数字,这有帮助

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

I used ggplot to get abubble graph but I was wondering how I would edit it to use my company's standard colors 我使用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")

How can I get a vertical line between C+ and D? 如何在C +和D之间获得垂直线? I see a lot of information out there if your x axis is a date but not for other categorical values 如果您的x轴是日期而不是其他分类值,我会看到很多信息

Hardcoded solutions are error-prone 硬编码解决方案容易出错

MrSnake's solution works - but only for the given data set because the value of 7.5 is hardcoded . MrSnake的解决方案可行 - 但仅适用于给定的数据集,因为7.5的值是硬编码的

It will fail with just a minor change to the data, eg, by replacing grade "A+" in row 1 of data by an "A" . 仅通过对数据的微小改变就会失败,例如,通过用"A+"替换data的第1行中的等级"A+" "A"

Using the hardcoded xintercept of 7.5 使用7.5的硬编码xintercept

p + geom_vline(xintercept = 7.5)

draws the line between grades C- and C+ instead of C+ and D : 绘制等级C-C +而不是C +D之间的界线:

在此输入图像描述

This can be solved using ordered factors . 这可以使用有序因子来解决。 But first note that the chart contains another flaw: The grades on the x-axis are ordered alphabetically 但首先请注意,图表包含另一个缺陷:x轴上的等级按字母顺序排列

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

where I would have expected 我原以为预期的地方

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

Fixing the x-axis 固定x轴

This can be fixed by turning MathGrade into an ordered factor with levels in a given order: 这可以通过将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() would be sufficient to plot a properly ordered x-axis but we need an ordered factor for the next step, the correct placement of the vertical line. factor()足以绘制正确排序的x轴,但我们需要一个有序因子用于下一步,正确放置垂直线。

Programmatically placing the vertical line 以编程方式放置垂直线

Let's suppose that the vertical line should be drawn between grades C- and D+ . 假设应在C-D +等级之间绘制垂直线。 However, it may happen that either or both grades are missing from the data. 但是,可能会发生数据中缺少其中一个或两个等级的情况。 Missing factors won't be plotted. 不会绘制缺失因子。 In the sample data set, there are no data with grade D+ , so the vertical line should be plotted between grades C- and D . 在样本数据集中,没有D +等级的数据,因此应在C-D等级之间绘制垂直线。

So, we need to look for the lowest grade equal or greater D+ and the highest grade equal or less than C- in the data set: 因此,我们需要在数据集中寻找等于或大于D +的最低等级以及等于或低于C-的最高等级:

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

These are the grades in the actual data set where the vertical line is to be plotted between: 这些是实际数据集中的等级,其中垂直线绘制在:

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

在此输入图像描述

Just add geom_vline ;) 只需添加geom_vline ;)

p + geom_vline(xintercept = 7.5)

在此输入图像描述

For changing the colors as to fit your company scheme, you can add something like: 要更改颜色以适合您的公司方案,您可以添加以下内容:

  + 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