R 用于在绘图外部设置图例的方法
到目前为止,我看到的所有解决方案都涉及到手动设置图例的坐标,使其位于绘图之外。我有>100个绘图,因此希望能够在每个绘图上重复使用相同的代码,以便在默认情况下将图例放置在绘图之外 这里有两个数据框,一个是成功的求职者,另一个是申请该职位的所有人:R 用于在绘图外部设置图例的方法,r,plot,position,legend,R,Plot,Position,Legend,到目前为止,我看到的所有解决方案都涉及到手动设置图例的坐标,使其位于绘图之外。我有>100个绘图,因此希望能够在每个绘图上重复使用相同的代码,以便在默认情况下将图例放置在绘图之外 这里有两个数据框,一个是成功的求职者,另一个是申请该职位的所有人: Id <- c(1,5,7,9,11,12,13,15,17,18) Type <- c("Beginner", "Expert", "Intermediate", "Beginner", "Professional", "Expert"
Id <- c(1,5,7,9,11,12,13,15,17,18)
Type <- c("Beginner", "Expert", "Intermediate", "Beginner",
"Professional", "Expert", "Intermediate", "Professional", "Professional",
"Expert")
Response<- c(0,1,2,2,1,2,1,2,1,1)
Successful <- data.frame(Id, Type, Response)
Successful
# Successful
Id Type Response
1 Beginner 0
5 Expert 1
7 Intermediate 2
9 Beginner 2
11 Professional 1
12 Expert 2
13 Intermediate 1
15 Professional 2
17 Professional 1
18 Expert 1
Id <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18)
Type <- c("Beginner", "Expert", "Professional", "Beginner", "Expert",
"Expert", "Intermediate", "Expert", "Beginner", "Intermediate",
"Professional", "Expert", "Intermediate","Intermediate", "Professional",
"Beginner", "Professional","Expert")
Response<- c(0,2,2,1,1,0,2,0,2,0,1,2,1,1,2,2,1,1)
AllApplicants <- data.frame(Id, Type, Response)
AllApplicants
# AllApplicants
Id Type Response
1 Beginner 0
2 Expert 2
3 Professional 2
4 Beginner 1
5 Expert 1
6 Expert 0
7 Intermediate 2
8 Expert 0
9 Beginner 2
10 Intermediate 0
11 Professional 1
12 Expert 2
13 Intermediate 1
14 Intermediate 1
15 Professional 2
16 Beginner 2
17 Professional 1
18 Expert 1
但是,我希望保留在barplot()函数中包含legend.text=TRUE时自动生成的图例,并使其自动将图例放置在绘图的外部和右侧。我还需要它留在那里,当我调整打印和缩放
任何帮助都将不胜感激 解决方案是否可行
library(ggplot2)
df <- as.data.frame(round(100*prop.table(table(AllApplicants$Response, AllApplicants$Type),2), 1))
ggplot(df, aes(x=Var2, y=Freq)) +
geom_col(aes(fill=Var1)) +
ggtitle("Responses of applicants") +
theme(plot.title = element_text(hjust=0.5)) +
xlab("Level") + ylab("Proportion") +
theme(legend.title = element_blank())
库(ggplot2)
df解决方案是否可行
library(ggplot2)
df <- as.data.frame(round(100*prop.table(table(AllApplicants$Response, AllApplicants$Type),2), 1))
ggplot(df, aes(x=Var2, y=Freq)) +
geom_col(aes(fill=Var1)) +
ggtitle("Responses of applicants") +
theme(plot.title = element_text(hjust=0.5)) +
xlab("Level") + ylab("Proportion") +
theme(legend.title = element_blank())
库(ggplot2)
df
library(ggplot2)
df <- as.data.frame(round(100*prop.table(table(AllApplicants$Response, AllApplicants$Type),2), 1))
ggplot(df, aes(x=Var2, y=Freq)) +
geom_col(aes(fill=Var1)) +
ggtitle("Responses of applicants") +
theme(plot.title = element_text(hjust=0.5)) +
xlab("Level") + ylab("Proportion") +
theme(legend.title = element_blank())