英文:
Cannot remove mysterious zero from beneath plots
问题
我有五个漂亮的图,但我不知道为什么每个图下面都会打印出一个 0
。我(至少尝试过)删除了 x 轴标题。我没有任何代码 - 好吧,至少我自己不知道有什么代码会在每个 x 轴下方显示零。我希望保留 "SP22" 等信息。以下是我两个图表的代码:
output1 <- ggplot(data=SP22_SP23, aes(x=semesters, y=UG_classif, group=1)) +
geom_line( aes(y=UG_classif), color="mediumaquamarine")+
geom_point() +
scale_x_discrete(limits = c("SP22", "SUM22", "FA22", "SP23"), expand = c(0, 0.1)) +
scale_y_continuous(n.breaks = 10, limits = c(500, 3500)) +
labs(title = "本科生注册情况:",
subtitle= "Sp'22至Sp'23\n", x = NULL, y = "学生人数") +
theme_minimal() +
removeGridX()
output2 <- ggplot(data=SP22_SP23, aes(x=semesters, y=NDseeking, group=1)) +
geom_line( aes(y=NDseeking), color="tan3")+
geom_point() +
scale_x_discrete(limits = c("SP22", "SUM22", "FA22", "SP23"), expand = c(0, 0.1)) +
scale_y_continuous(n.breaks = 10, limits = c(0, 400)) +
labs(title = "非学位课程注册情况:",
subtitle= "Sp'22至Sp'23\n", x=NULL, y = "学生人数") +
theme(axis.title.x=element_blank()) +
theme_minimal() +
removeGridX()
这是我所得到的截图:
如你所见,我尝试了在 labs()
和 theme(axis.title.x=element_blank())
中使用 x = NULL
。
非常感谢您的帮助!
1: https://i.stack.imgur.com/ESD0r.png
英文:
I've got five nice plots, but I've no idea why a 0
is printing beneath each. I've (at least tried) to remove x axis titles. I have no code--well, none I'm cognizant of--asking for the zeros beneath each x axis. I do wish to preserve the "SP22" etc. Here's the code for two of my plots:
output1 <- ggplot(data=SP22_SP23, aes(x=semesters, y=UG_classif, group=1)) +
geom_line( aes(y=UG_classif), color="mediumaquamarine")+
geom_point() +
scale_x_discrete(limits = c("SP22", "SUM22", "FA22", "SP23"), (expand = c(0,.1))) +
scale_y_continuous(n.breaks = 10, limits = c(500, 3500)) +
labs(title = "Undergraduate Enrollment:",
subtitle= "Sp'22 through Sp'23\n", x = NULL, y = "Student Count") +
theme_minimal() +
removeGridX()
output2 <- ggplot(data=SP22_SP23, aes(x=semesters, y=NDseeking, group=1)) +
geom_line( aes(y=NDseeking), color="tan3")+
geom_point() +
scale_x_discrete(limits = c("SP22", "SUM22", "FA22", "SP23"), (expand = c(0,.1))) +
scale_y_continuous(n.breaks = 10, limits = c(0, 400)) +
labs(title = "Non-degree Seeking Enrollment:",
subtitle= "Sp'22 through Sp'23\n", x=NULL, y = "Student Count") +
theme(axis.title.x=element_blank()) +
theme_minimal() +
removeGridX()
And here's a screenshot of what I'm getting:
As you can see, I've tried both putting x = NULL
with the labs()
and theme(axis.title.x=element_blank())
.
Sincere thanks for any help!
答案1
得分: 3
被打印出来的 0
是由于在 scale_x_discrete
调用中的 (expand = c(0,.1))
调用造成的;括号是为了简化 print
的方式。坐标轴上的零来自扩展调用中的第一个值。
要查看:
library(ggplot2)
ggplot(mtcars, aes(cyl, mpg)) + scale_x_discrete((expand = c(2,3)))
避免这种情况,不要用括号括住 expand
参数。
例如:
ggplot(mtcars, aes(cyl, mpg)) + scale_x_discrete(expand = c(2,3))
英文:
The 0
being printed is due to the (expand = c(0,.1))
call in the scale_x_discrete
call; the parenthesis is a short but to print
ing. The zero in the axis is from the first value in the expand call.
To see:
library(ggplot2)
ggplot(mtcars, aes(cyl, mpg)) + scale_x_discrete((expand = c(2,3)))
Avoid this by not surrounding the expand
argument by parenthesis.
e.g.
ggplot(mtcars, aes(cyl, mpg)) + scale_x_discrete(expand = c(2,3))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论