如何清除我的 plot(x,y) 折线图标签以防止过度绘制?
How can I clean up my plot(x,y) line graph labels from overplotting?
我正在使用 babynames
包,并试图在 babynames::applications
数据框中绘制每年的出生人数。
目前我已经接近终点线
library(babynames) plot(births[1:109,],births[1,], type="l",main="Birthcount Over The Century",xlab="year",)
但结果很糟糕,我不太明白索引括号内发生了什么。
我该如何清理它?我尝试更改为 plot(births[1:109,],births[1:109,]
但这使得它用更混乱的标签绘制得更多。
方括号内到底发生了什么?
您可以在 base
R
图形中使用 axis()
函数控制轴标签,这可能会帮助您 de-crowd Y 轴。来自 this 答案的灵感。
library(babynames)
plot(births[1:109, ], type = "l", main = "Birthcount Over The Century", yaxt="n")
axis(2, at = axTicks(2), labels = paste(formatC(axTicks(2)/1000, format = 'd'), 'k', sep = ''))
由 reprex package (v2.0.1)
于 2022-01-27 创建
或者,如果您对 non-base 包持开放态度...您可以使用 {ggplot2} 获得更多控制权。在这里,我使用 scales::comma
来控制 y 轴标签的格式,并使用 ggthemes::theme_base()
让它看起来像 base
图,如果这就是你想要的。 {scales} 有很多很好的函数来控制这种格式以满足您的需求。
library(babynames)
library(tidyverse)
library(scales)
library(ggthemes)
births %>%
filter(year < 2018) %>%
ggplot(aes(year, births)) +
geom_line() +
scale_y_continuous(labels = scales::comma) +
labs(title = "Birthcount Over The Century") +
ggthemes::theme_base() +
theme(plot.title = element_text(hjust = 0.5))
由 reprex package (v2.0.1)
于 2022-01-27 创建
我正在使用 babynames
包,并试图在 babynames::applications
数据框中绘制每年的出生人数。
目前我已经接近终点线
library(babynames) plot(births[1:109,],births[1,], type="l",main="Birthcount Over The Century",xlab="year",)
但结果很糟糕,我不太明白索引括号内发生了什么。
我该如何清理它?我尝试更改为 plot(births[1:109,],births[1:109,]
但这使得它用更混乱的标签绘制得更多。
方括号内到底发生了什么?
您可以在 base
R
图形中使用 axis()
函数控制轴标签,这可能会帮助您 de-crowd Y 轴。来自 this 答案的灵感。
library(babynames)
plot(births[1:109, ], type = "l", main = "Birthcount Over The Century", yaxt="n")
axis(2, at = axTicks(2), labels = paste(formatC(axTicks(2)/1000, format = 'd'), 'k', sep = ''))
由 reprex package (v2.0.1)
于 2022-01-27 创建或者,如果您对 non-base 包持开放态度...您可以使用 {ggplot2} 获得更多控制权。在这里,我使用 scales::comma
来控制 y 轴标签的格式,并使用 ggthemes::theme_base()
让它看起来像 base
图,如果这就是你想要的。 {scales} 有很多很好的函数来控制这种格式以满足您的需求。
library(babynames)
library(tidyverse)
library(scales)
library(ggthemes)
births %>%
filter(year < 2018) %>%
ggplot(aes(year, births)) +
geom_line() +
scale_y_continuous(labels = scales::comma) +
labs(title = "Birthcount Over The Century") +
ggthemes::theme_base() +
theme(plot.title = element_text(hjust = 0.5))
由 reprex package (v2.0.1)
于 2022-01-27 创建