因此,我成功地用我朋友的数据在R中做了一个政治罗盘,看起来是这样的:
Name Economic Social House Gender
Jimmy -3.75 -2.46 B St Male
Beth -4.75 -7.38 A St Female
代码运行良好,但我觉得我写了太多的代码来得到我想要的,我如何减少代码量才能得到同样的结果。
另外,让geom_hline/geom_vline
停在方块停止的地方也是很好的,你有什么办法吗?
代码:
df <- data.frame(
stringsAsFactors = FALSE,
Name = c("Jimmy", "Beth"),
Economic = c(-0.04, -4.75),
Social = c(-2.46, -7.38),
House = c("B St", "A St"),
Gender = c("Male", "Female"))
ggplot(df,aes(Economic,Social, color = House))+
xlim(-10,10)+
ylim(-10,10)+
annotate("rect", xmin = 0, xmax = 10, ymin = 0, ymax = 10, fill= "deepskyblue3") +
annotate("rect", xmin = -10, xmax = 0, ymin = -10, ymax = 0 , fill= "palegreen1") +
annotate("rect", xmin = 0, xmax = 10, ymin = -10, ymax = 0, fill= "plum3") +
annotate("rect", xmin = -10, xmax = 0, ymin = 0, ymax = 10, fill= "indianred1") +
geom_hline(yintercept = 0) +
geom_vline(xintercept=0) +
geom_point(size = 4)+
ggtitle("The Gang gets Political")+
theme_minimal()+
geom_text(aes(label=Name),hjust=0, vjust=0, size =4, color = "black")+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())+
geom_text(
label="Authoritarian",
x=0,
y=10.5,
color = "azure4"
)+
geom_text(
label="Libertatian",
x=0,
y=-10.5,
color = "azure4"
)+
geom_text(
label="Left",
x=-10.5,
y=0,
color = "azure4"
)+
geom_text(
label="Right",
x=10.5,
y=0,
color = "azure4"
)
转载请注明出处:http://www.jxbyjx.net/article/20230525/1710771.html