我需要你的帮助,我在R标记我的项目,我有一些问题。
这个情节很奇怪,因为一些很长的名字,比如第二个,所以情节很单薄,看起来很奇怪,而且小标题也不完整。我试着在情节中使用out. with ="100%",但是仍然很奇怪
另一个问题是,这里的第一个条很长,所以我不能把标签放在右边。但是底部的条很短,我不能把它们放在条里面。我该怎么解决这个问题
以下是数据集:https://www.kaggle.com/datasets/thedevastator/uncovering-insights-to-college-majors-and-their?select=all-ages.csv
这里是我的整个代码(图像情节代码是由大箭头标识)
install.packages("tidyverse")
install.packages("scales")
require(tidyverse)
require(scales)
collegeData <- read_csv("all-ages.csv")
collegeWomenData <-read_csv("women-stem.csv")
head(collegeData)
str(collegeData)
#Number students
##Number of students per major category #Melhorar labels #V
collegeData %>%
group_by(Major_category) %>%
summarize(Total= sum(Total)) %>%
ggplot() +
geom_col(mapping=aes(x=Total, y= reorder(Major_category, Total)),fill="darkblue")+
labs(title= "Number of students per Major Category")+ xlab("Total of students") + ylab("Major category")+
theme(axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)))+ theme(axis.title.y = element_text(margin = margin(t = 0, r = 25, b = 0, l = 0)))+
scale_x_continuous(labels = comma)+
geom_text(aes(x = Total, y = reorder(Major_category, Total), label= format(Total,digits=5)), color="white",fontface="bold",hjust = 1.1)
#Top 15 majors with more students
collegeData %>%
arrange(desc(Total)) %>%
slice(1:15) %>%
ggplot() +
geom_col(mapping=aes(x=Total, y= reorder(Major, Total)), fill="darkblue")+
labs(title= "Number of students per Major Category")+ xlab("Total of students") + ylab("Major category")+
theme(axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)),axis.title.y = element_text(margin = margin(t = 0, r = 25, b = 0, l = 0)),plot.title = element_text(hjust = 0.5))+
scale_x_continuous(labels = comma)+
geom_text(aes(x = Total, y = reorder(Major, Total), label= format(Total,digits=5)), color="white",fontface="bold",hjust = 1.1)
#Top 15 majors with less students # Second IMAGE <--------------------------------------------------
collegeData %>%
arrange(Total) %>%
slice(1:15) %>%
ggplot() +
geom_col(mapping=aes(x=Total, y= reorder(Major, Total)),fill="darkblue")+
labs(title= "Number of students per Major Category")+ xlab("Total of students") + ylab("Major category")+
theme(axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)),axis.title.y = element_text(margin = margin(t = 0, r = 25, b = 0, l = 0)),plot.title = element_text(hjust = 0.5))+
scale_x_continuous(labels = comma)+
geom_text(aes(x = Total, y = reorder(Major, Total), label= format(Total,digits=5)), color="white",fontface="bold",hjust = 1.1)
#Women
#Percentage of female by major STEM category
collegeWomenData %>%
group_by(Major_category) %>%
summarize(ShareWomen=mean(ShareWomen)) %>%
ggplot()+
geom_col(mapping=aes(x=ShareWomen, y= reorder(Major_category, ShareWomen)),fill="darkblue")+
labs(title= "Percentage of females by STEM category")+ xlab("Percentage of females") + ylab("Category")+
theme(axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)),axis.title.y = element_text(margin = margin(t = 0, r = 25, b = 0, l = 0)),plot.title = element_text(hjust = 0.5))+
scale_x_continuous(labels=scales::percent_format())+
geom_text(aes(x = ShareWomen, y = reorder(Major_category, ShareWomen), label= paste0(format(ShareWomen*100,digits=3), "%")), color="white",fontface="bold",hjust = 1.1)
#Top 15 majors with least females
collegeWomenData %>%
arrange(ShareWomen) %>%
slice(1:15) %>%
ggplot()+
geom_col(mapping=aes(x=ShareWomen, y= reorder(Major, ShareWomen)),fill="darkblue")+
labs(title= "Top 15 STEM majors with the lowest female percentage")+ xlab("Percentage of females") + ylab("Major")+
theme(axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)),axis.title.y = element_text(margin = margin(t = 0, r = 25, b = 0, l = 0)),plot.title = element_text(hjust = 0.5))+
scale_x_continuous(labels=scales::percent_format())+
geom_text(aes(x = ShareWomen, y = reorder(Major, ShareWomen), label= paste0(format(ShareWomen*100,digits=3), "%")), color="white",fontface="bold",hjust = 1.1)
#Top 15 majors with most females
collegeWomenData %>%
arrange(desc(ShareWomen)) %>%
slice(1:15) %>%
ggplot()+
geom_col(mapping=aes(x=ShareWomen, y= reorder(Major, ShareWomen)),fill="darkblue")+
labs(title= "Top 15 STEM majors with the highest female percentage")+ xlab("Percentage of females") + ylab("Major")+
theme(axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)),axis.title.y = element_text(margin = margin(t = 0, r = 25, b = 0, l = 0)),plot.title = element_text(hjust = 0.5))+
scale_x_continuous(labels=scales::percent_format())+
geom_text(aes(x = ShareWomen, y = reorder(Major, ShareWomen), label= paste0(format(ShareWomen*100,digits=3), "%")), color="white",fontface="bold",hjust = 1.1)
#Unemployment rate
#Unemployment rate per major category
collegeData %>%
group_by(Major_category) %>%
summarise(average_rate= mean(Unemployment_rate)) %>%
ggplot()+
geom_col(mapping=aes(x=average_rate, y=reorder(Major_category, average_rate)), fill="darkblue")+
theme(axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)),axis.title.y = element_text(margin = margin(t = 0, r = 25, b = 0, l = 0)),plot.title = element_text(hjust = 0.5))+
labs(title= "Unemployment rate per major category") + xlab("Unemployment rate")+ ylab("Major category")+
scale_x_continuous(labels=scales::percent_format())+
geom_text(aes(x = average_rate, y = reorder(Major_category, average_rate), label= paste0(format(average_rate*100,digits=3), "%")), color="white",fontface="bold",hjust = 1.1)
#Top 15 major with biggest Unemployment rate
collegeData %>%
group_by(Major) %>%
summarise(average_rate= mean(Unemployment_rate)) %>%
arrange(desc(average_rate)) %>%
slice(1:15) %>%
ggplot()+
geom_col(mapping=aes(x=average_rate, y=reorder(Major, average_rate)), fill="darkblue")+
theme(axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)) , axis.title.y = element_text(margin = margin(t = 0, r = 25, b = 0, l = 0)),plot.title = element_text(hjust = 0.5))+
labs(title= "Top 15 majors with the biggest unemployment rate") + xlab("Unemployment rate")+ ylab("Major")+
scale_x_continuous(labels=scales::percent_format())+
geom_text(aes(x = average_rate, y = reorder(Major, average_rate), label= paste0(format(average_rate*100,digits=3), "%")), color="white",fontface="bold",hjust = 1.1)
#Top 15 major with smallest Unemployment rate
collegeData %>%
group_by(Major) %>%
summarise(average_rate= mean(Unemployment_rate)) %>%
arrange(average_rate) %>%
slice(1:15) %>%
ggplot()+
geom_col(mapping=aes(x=average_rate, y=reorder(Major, average_rate)), fill="darkblue")+
theme(axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)) , axis.title.y = element_text(margin = margin(t = 0, r = 25, b = 0, l = 0)),plot.title = element_text(hjust = 0.5))+
labs(title= "Top 15 majors with the smallest unemployment rate") + xlab("Unemployment rate")+ ylab("Major")+
scale_x_continuous(labels=scales::percent_format())+
geom_text(aes(x = average_rate, y = reorder(Major, average_rate), label= paste0(format(average_rate*100,digits=3), "%")), color="white",fontface="bold",hjust = 1.1)
#Salary
#Salary per major category
collegeData %>%
group_by(Major_category) %>%
summarise(average_salary= mean(Median)) %>%
ggplot()+
geom_col(mapping=aes(x=average_salary, y=reorder(Major_category, average_salary)), fill="darkblue")+
theme(axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)) , axis.title.y = element_text(margin = margin(t = 0, r = 25, b = 0, l = 0)),plot.title = element_text(hjust = 0.5))+
labs(title= "Average salary per major category") + xlab("Salary")+ ylab("Major category") + scale_x_continuous(labels=scales::dollar_format())+
geom_text(aes(x = average_salary, y = reorder(Major_category, average_salary), label= paste0("$", format(average_salary,digits=5))), color="white",fontface="bold",hjust = 1.1)
#Top15 worst paying majors
collegeData %>%
arrange(Median) %>%
slice(1:15) %>%
ggplot()+
geom_col(mapping=aes(x=Median, y=reorder(Major, Median)), fill="darkblue")+
theme(axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)) , axis.title.y = element_text(margin = margin(t = 0, r = 25, b = 0, l = 0)),plot.title = element_text(hjust = 0.5))+
labs(title= "Top 15 major with lowest average salaries") + xlab("Salary")+ ylab("Major") + scale_x_continuous(labels=scales::dollar_format())+
geom_text(aes(x = Median, y = reorder(Major, Median), label= paste0("$",Median)), color="white",fontface="bold",hjust = 1.1)
#Top 15 best paying majors #FIRST IMAGE <----------------------------------------------------
collegeData %>%
arrange(desc(Median)) %>%
slice(1:15) %>%
ggplot()+
geom_col(mapping=aes(x=Median, y=reorder(Major, Median)), fill="darkblue")+
theme(axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)) , axis.title.y = element_text(margin = margin(t = 0, r = 25, b = 0, l = 0)),plot.title = element_text(hjust = 0.5))+
labs(title= "Top 15 major with highest average salaries") + xlab("Salary")+ ylab("Major") + scale_x_continuous(labels=scales::dollar_format())+
geom_text(aes(x = Median, y = reorder(Major, Median), label= paste0("$",Median)), color="white",fontface="bold",hjust = 1.1)
对于宽度问题,我尝试了="100%"和其他值。对于标签问题,我尝试了将值放在不同的位置(左、中、右),但似乎没有一个在视觉上是可以的
1条答案
按热度按时间0pizxfdo1#
你可以做很多事情,但我不知道什么对你最有效。
首先,可以在
scale_ _continuous
中使用expand
(无论是x还是y)来扩展绘图限制。对于一个长标题,你可以添加一个回车符(也可以是新行,下一行,* 按回车 *)。当使用这样的图(水平条)时,另一个选择是将标题在整个空间居中。查看
theme
中的附加调用。对于长名称,您可以修改它们。在这种情况下,对于某些图,文本全部大写。在其他情况下,您可以混合大小写。如果您采用全部大写的图并重新格式化文本,则会更适合。(注意数据中添加的
mutate
和对库stringr
的调用。)你可以将这些相同的原则应用于你在问题中使用的另一个图。