无法删除图表下方的神秘零。

huangapple go评论74阅读模式
英文:

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 &lt;- ggplot(data=SP22_SP23, aes(x=semesters, y=UG_classif, group=1)) +
  geom_line( aes(y=UG_classif), color=&quot;mediumaquamarine&quot;)+
  geom_point() + 
  scale_x_discrete(limits = c(&quot;SP22&quot;, &quot;SUM22&quot;, &quot;FA22&quot;, &quot;SP23&quot;), (expand = c(0,.1))) +
  scale_y_continuous(n.breaks = 10, limits = c(500, 3500)) +
    labs(title = &quot;Undergraduate Enrollment:&quot;,
       subtitle= &quot;Sp&#39;22 through Sp&#39;23\n&quot;, x = NULL, y = &quot;Student Count&quot;) +
  theme_minimal() +
  removeGridX()

output2 &lt;- ggplot(data=SP22_SP23, aes(x=semesters, y=NDseeking, group=1)) +
  geom_line( aes(y=NDseeking), color=&quot;tan3&quot;)+
  geom_point() + 
  scale_x_discrete(limits = c(&quot;SP22&quot;, &quot;SUM22&quot;, &quot;FA22&quot;, &quot;SP23&quot;), (expand = c(0,.1))) +
  scale_y_continuous(n.breaks = 10, limits = c(0, 400)) +
    labs(title = &quot;Non-degree Seeking Enrollment:&quot;,
       subtitle= &quot;Sp&#39;22 through Sp&#39;23\n&quot;, x=NULL, y = &quot;Student Count&quot;) +
  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 printing. 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))

huangapple
  • 本文由 发表于 2023年5月29日 06:37:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定