R:在直方图上将 X 轴更改为“整数”
Posted
技术标签:
【中文标题】R:在直方图上将 X 轴更改为“整数”【英文标题】:R: Changing X-Axis to "Integers" on Histogram 【发布时间】:2021-12-14 03:06:04 【问题描述】:我正在使用 R 编程语言。我下载了以下世界人口数据并尝试制作直方图:
#data source: https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/CSV_FILES/WPP2019_TotalPopulationBySex.csv
`WPP2019_TotalPopulationBySex.(1)` <- read.csv("C:/Users/ME/Downloads/WPP2019_TotalPopulationBySex (1).csv")
world_data = WPP2019_TotalPopulationBySex.(1)
> head(world_data)
LocID Location VarID Variant Time MidPeriod PopMale PopFemale PopTotal PopDensity
1 4 Afghanistan 2 Medium 1950 1950.5 4099.243 3652.874 7752.117 11.874
2 4 Afghanistan 2 Medium 1951 1951.5 4134.756 3705.395 7840.151 12.009
3 4 Afghanistan 2 Medium 1952 1952.5 4174.450 3761.546 7935.996 12.156
4 4 Afghanistan 2 Medium 1953 1953.5 4218.336 3821.348 8039.684 12.315
5 4 Afghanistan 2 Medium 1954 1954.5 4266.484 3884.832 8151.316 12.486
6 4 Afghanistan 2 Medium 1955 1955.5 4318.945 3952.047 8270.992 12.669
world_data$PopMale = as.numeric(substr(world_data$PopMale,1,1))
world_data$PopFemale = as.numeric(substr(world_data$PopFemale,1,1))
world_data$PopTotal = as.numeric(substr(world_data$PopTotal,1,1))
world_data$PopDensity = as.numeric(substr(world_data$PopDensity,1,1))
我尝试制作直方图:
library(ggplot2)
library(scales)
library(cowplot2)
g1 = ggplot(world_data, aes(x=PopMale)) + geom_histogram() + + ggtitle("Male Population")
g2 = ggplot(world_data, aes(x=PopFemale)) + geom_histogram() + ggtitle("Female Population")
g3 = ggplot(world_data, aes(x=PopTotal)) + geom_histogram() + ggtitle("Total Population")
g4 = ggplot(world_data, aes(x=PopDensity)) + geom_histogram()+ ggtitle("Population Density")
plot_row = plot_grid(g1, g2, g3, g4)
# now add the title
title <- ggdraw() +
draw_label(
"World Population: Does Benford's Law Exist?",
fontface = 'bold',
x = 0,
hjust = 0
) +
theme(
# add margin on the left of the drawing canvas,
# so title is aligned with left edge of first plot
plot.margin = margin(0, 0, 0, 7)
)
plot_grid(
title, plot_row,
ncol = 1,
# rel_heights values control vertical title margins
rel_heights = c(0.1, 1)
)
问题:我正在尝试格式化直方图的 x 轴,以便它们显示整数(例如 0,1,2,3,4,5,6,7,8,9 )。我尝试使用以下代码来执行此操作:
integer_breaks <- function(n = 5, ...)
fxn <- function(x)
breaks <- floor(pretty(x, n, ...))
names(breaks) <- attr(breaks, "labels")
breaks
return(fxn)
library(ggplot2)
g1 = ggplot(world_data, aes(x=PopMale)) + geom_histogram() + scale_y_continuous(breaks = integer_breaks()) + ggtitle("Male Population")
g2 = ggplot(world_data, aes(x=PopFemale)) + geom_histogram() + scale_y_continuous(breaks = integer_breaks() + ggtitle("Female Population")
g3 = ggplot(world_data, aes(x=PopTotal)) + geom_histogram() + scale_y_continuous(breaks = integer_breaks() + ggtitle("Total Population")
g4 = ggplot(world_data, aes(x=PopDensity)) + geom_histogram()+ scale_y_continuous(breaks = integer_breaks() + ggtitle("Population Density")
plot_row = plot_grid(g1, g2, g3, g4)
# now add the title
title <- ggdraw() +
draw_label(
"World Population: Does Benford's Law Exist?",
fontface = 'bold',
x = 0,
hjust = 0
) +
theme(
# add margin on the left of the drawing canvas,
# so title is aligned with left edge of first plot
plot.margin = margin(0, 0, 0, 7)
)
plot_grid(
title, plot_row,
ncol = 1,
# rel_heights values control vertical title margins
rel_heights = c(0.1, 1)
)
问题:但这仍然像以前一样显示 x 轴。
谁能告诉我如何解决这个问题?
谢谢!
参考资料:
How to display only integer values on an axis using ggplot2【问题讨论】:
如果你想改变x
轴使用scale_x_continuous
,而不是scale_y_continuous
@MrFlick:感谢您的回复!我尝试了以下代码: g1 = ggplot(world_data, aes(x=PopMale)) + geom_histogram() + scale_x_continuous(breaks = integer_breaks()) + ggtitle("Male Population") .. 但它仍然没有解决问题.你知道这是为什么吗?如果您稍后有时间 - 请您看一下吗?谢谢!
【参考方案1】:
我找到了答案:
library(ggplot2)
g1 = ggplot(world_data, aes(x=PopMale)) + geom_histogram() + scale_x_continuous(breaks = seq(0, 10,1)) + ggtitle("Male Population")
g2 = ggplot(world_data, aes(x=PopFemale)) + geom_histogram() + scale_x_continuous(breaks = seq(0, 10,1)) + ggtitle("Female Population")
g3 = ggplot(world_data, aes(x=PopTotal)) + geom_histogram() + scale_x_continuous(breaks = seq(0, 10,1)) + ggtitle("Total Population")
g4 = ggplot(world_data, aes(x=PopDensity)) + geom_histogram()+ scale_x_continuous(breaks = seq(0, 10,1)) + ggtitle("Population Density")
plot_row = plot_grid(g1, g2, g3, g4)
# now add the title
title <- ggdraw() +
draw_label(
"World Population: Does Benford's Law Exist?",
fontface = 'bold',
x = 0,
hjust = 0
) +
theme(
# add margin on the left of the drawing canvas,
# so title is aligned with left edge of first plot
plot.margin = margin(0, 0, 0, 7)
)
plot_grid(
title, plot_row,
ncol = 1,
# rel_heights values control vertical title margins
rel_heights = c(0.1, 1)
)
【讨论】:
【参考方案2】:对于这种情况,你应该制作条形图而不是直方图:
library(tidyverse)
df <- read_csv("https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/CSV_FILES/WPP2019_TotalPopulationBySex.csv")
#>
#> ── Column specification ────────────────────────────────────────────────────────
#> cols(
#> LocID = col_double(),
#> Location = col_character(),
#> VarID = col_double(),
#> Variant = col_character(),
#> Time = col_double(),
#> MidPeriod = col_double(),
#> PopMale = col_double(),
#> PopFemale = col_double(),
#> PopTotal = col_double(),
#> PopDensity = col_double()
#> )
dfplot <- df %>%
select(starts_with("Pop")) %>%
pivot_longer(cols = everything(), names_to = "variable", values_to = "value") %>%
mutate(firstdigit = substr(as.character(value), 1, 1))
ggplot(dfplot, aes(firstdigit)) +
geom_bar() +
facet_wrap(~variable)
由reprex package (v2.0.1) 于 2021 年 10 月 28 日创建
【讨论】:
以上是关于R:在直方图上将 X 轴更改为“整数”的主要内容,如果未能解决你的问题,请参考以下文章