我想比较同一所学校内的性别表现。但是,R一直将我的male_scores列作为一个因素导入,因此为了解决该问题,我编写了以下代码:
该代码过去为我解决了类似的问题,但是在这种情况下,as.double()无法有效地工作,因为它会将male_scores转换为从最高到最低的排名(即1,2,3,4)等)。
我进一步尝试了as.numeric(),但不幸的是它也没有用。
library(ggplot2)
library(readxl)
library(tidyr)
library(dplyr)
library("ggalt")
gender_comparison <- read.csv(file = "dumbbell_plot.csv")
# Change variables from character into numeric format
gender_comparison <- gender_comparison %>%
mutate(male_scores = as.double(male_scores))
# Check format of variables
sapply(gender_comparison, class)
## Create a dumbbell plot to compare boys and girls performance within the same school
ggplot(gender_comparison, aes(x=male_scores, xend=female_scores, y=school_name,)) +
geom_dumbbell(size=1,color="grey",
colour_x = "blue", colour_xend = "red",
dot_guide=TRUE, dot_guide_size=0.20) +
scale_x_continuous(limits = c(70,90)) +
labs(x="Average Exam Scores", y="City",
title="The Gender Standardized Exams Score Gap Remains Prevalent Within Schools",
theme(panel.grid.major.x=element_line(size=0.20)) +
theme(panel.grid.major.y=element_blank())+
theme(axis.text.y=element_text(size = rel(0.55)))