r ggplot 带有零且没有逗号作为大数字分隔符
r ggplot with zeroes and no comma as the big number separator
我正在尝试按月计算实例数,将它们绘制在图表上并将每月计数作为标签添加到条形图的顶部。以下是我遇到的问题的可重现示例:
library(scales)
library(ggplot2)
set.seed(1)
df <- data.frame(DueDate = as.Date(paste("2015",
sample(1:6, 6000, replace=T),
sample(1:30, 6000, replace=T), sep = "-")),
stringsAsFactors = F)
ggplot(df, aes(as.Date(cut(DueDate,
breaks = "month")) )) +
geom_bar() +
geom_text(stat = 'bin',
aes(label = ..count..),
vjust = -1,
size = 2) +
scale_y_continuous(labels = comma) +
labs(x = "Month", y = "Frequency") +
theme_minimal()
问题是,当我创建绘图时,条形之间有 0,条形顶部的数字没有逗号作为大数字分隔符。
你可以为月份做一个新的专栏,然后做剧情。我使用 lubridate
包来帮助处理 R 中的日期。
# Functions to help handle dates
library(lubridate)
# Make a new month column
df$month <- month(df$DueDate, label = TRUE)
# Plot with aes(month)
ggplot(df, aes(month)) +
geom_bar() +
geom_text(stat = 'bin',
aes(label = ..count..),
vjust = -1,
size = 2) +
scale_y_continuous(labels = comma) +
labs(x = "Month", y = "Frequency") +
theme_minimal()
数据中有一些 NA,由图中的最后一个条表示。这可能是因为您在生成数据时为二月创建的日期无效(例如,没有二月 30 日)。
更正了我上面评论中的几个错误。从日期序列中抽样可以让您计算该月的第 31 天,并避免 NA 从 2 月的第 29-30 天开始。
set.seed(1)
df <- data.frame(DueDate = format(
sample(
seq( as.Date("2015-01-01"),
as.Date("2015-06-30"), by="1 day") ,
6000,replace=T), "%b"),
stringsAsFactors = F)
# This does all the aggregation in one step.
# Could probably leave them as Dates and use `format` in the `aes` call
ggplot(df, aes(DueDate)) +
geom_bar() +
geom_text(stat = 'bin',
aes(label = formatC(..count.., big.mark=",") ),
vjust = -1,
size = 2) +
scale_y_continuous(labels = comma) +
labs(x = "Month", y = "Frequency") +
theme_minimal()
将样本量乘以 2 以表明 comma
-y 尺度的参数有效。
我正在尝试按月计算实例数,将它们绘制在图表上并将每月计数作为标签添加到条形图的顶部。以下是我遇到的问题的可重现示例:
library(scales)
library(ggplot2)
set.seed(1)
df <- data.frame(DueDate = as.Date(paste("2015",
sample(1:6, 6000, replace=T),
sample(1:30, 6000, replace=T), sep = "-")),
stringsAsFactors = F)
ggplot(df, aes(as.Date(cut(DueDate,
breaks = "month")) )) +
geom_bar() +
geom_text(stat = 'bin',
aes(label = ..count..),
vjust = -1,
size = 2) +
scale_y_continuous(labels = comma) +
labs(x = "Month", y = "Frequency") +
theme_minimal()
问题是,当我创建绘图时,条形之间有 0,条形顶部的数字没有逗号作为大数字分隔符。
你可以为月份做一个新的专栏,然后做剧情。我使用 lubridate
包来帮助处理 R 中的日期。
# Functions to help handle dates
library(lubridate)
# Make a new month column
df$month <- month(df$DueDate, label = TRUE)
# Plot with aes(month)
ggplot(df, aes(month)) +
geom_bar() +
geom_text(stat = 'bin',
aes(label = ..count..),
vjust = -1,
size = 2) +
scale_y_continuous(labels = comma) +
labs(x = "Month", y = "Frequency") +
theme_minimal()
数据中有一些 NA,由图中的最后一个条表示。这可能是因为您在生成数据时为二月创建的日期无效(例如,没有二月 30 日)。
更正了我上面评论中的几个错误。从日期序列中抽样可以让您计算该月的第 31 天,并避免 NA 从 2 月的第 29-30 天开始。
set.seed(1)
df <- data.frame(DueDate = format(
sample(
seq( as.Date("2015-01-01"),
as.Date("2015-06-30"), by="1 day") ,
6000,replace=T), "%b"),
stringsAsFactors = F)
# This does all the aggregation in one step.
# Could probably leave them as Dates and use `format` in the `aes` call
ggplot(df, aes(DueDate)) +
geom_bar() +
geom_text(stat = 'bin',
aes(label = formatC(..count.., big.mark=",") ),
vjust = -1,
size = 2) +
scale_y_continuous(labels = comma) +
labs(x = "Month", y = "Frequency") +
theme_minimal()
将样本量乘以 2 以表明 comma
-y 尺度的参数有效。