如何将逗号分隔符添加到 geom_text 中的文本标签?
How do I add a comma separator to a text label in geom_text?
我需要更改 geom_text()
中的数字格式以包含逗号。
我已经看到了相关问题,但我无法使这些解决方案起作用。我试过 "sep =" 一种,count/sum(count) 那种,还有一些我只是在不知道什么意思的情况下转录的其他代码。在这让我发疯之前,我需要一条生命线。
这是我的数据:
N_PASSENGERS Count Mean_Dist Mean_Time Mean_Fare
<int> <int> <dbl> <dbl> <dbl>
1 1 57216 2.16 10.2 145.
2 2 8421 1.92 9.21 213.
3 3 2022 2.01 9.67 234.
4 4 572 1.96 9.22 351.
5 5 306 2.40 9.84 505.
6 6 184 1.90 7.63 446.
ggplot(Difference, aes(x = N_PASSENGERS, y = Mean_Dist, size = Count)) +
geom_point() +
scale_size(range = c(0, 20)) +
xlim(0, 6) +
ylim(1.75, 2.5) +
geom_text(aes(label = Count),
size = 3, vjust = 4.2,
WHAT THE HELL GOES HERE TO MAKE SOME COMMAS HAPPEN?) +
theme_minimal() +
theme(legend.position = "none") +
labs(x = "Number of Passengers",
y = "Mean Distance",
title = "Trips by Number of Rides and Distance") +
theme(plot.title = element_text(hjust = .5))
我希望在我的数据点旁边看到像 10,000 这样的数字。相反,我看到像 10000 这样的数字。我很欣赏这是一个幼稚的简单问题。我正在尝试自学 R,因此非常感谢您对此提供的帮助。
您可以使用 scales
包,它允许一些格式选项,例如 comma
、dollar
或 percent
。
df <- data.frame(a=c("a","b","c","d"), b=c(300,1000,2000,4000))
library(ggplot2)
library(scales)
ggplot(df, aes(a, b)) +
geom_point(size=4) +
scale_y_continuous(labels = comma)
您可以在 geom_text
中的美学映射 aes()
内格式化文本标签。
而不是:
... +
geom_text(aes(label = Count), size = 3, vjust = 4.2) +
...
使用:
... +
geom_text(aes(label = scales::comma(Count)), size = 3, vjust = 4.2) +
...
完整数据和代码:
Difference <- read.table(text = "
N_PASSENGERS Count Mean_Dist Mean_Time Mean_Fare
1 1 57216 2.16 10.2 145.
2 2 8421 1.92 9.21 213.
3 3 2022 2.01 9.67 234.
4 4 572 1.96 9.22 351.
5 5 306 2.40 9.84 505.
6 6 184 1.90 7.63 446.")
ggplot(Difference, aes(x = N_PASSENGERS, y = Mean_Dist, size = Count)) +
geom_point() +
scale_size(range = c(0, 20)) +
xlim(0, 6) +
ylim(1.75, 2.5) +
geom_text(aes(label = scales::comma(Count)),
size = 3, vjust = 4.2) +
theme_minimal() +
theme(legend.position = "none") +
labs(x = "Number of Passengers",
y = "Mean Distance",
title = "Trips by Number of Rides and Distance") +
theme(plot.title = element_text(hjust = .5))
我需要更改 geom_text()
中的数字格式以包含逗号。
我已经看到了相关问题,但我无法使这些解决方案起作用。我试过 "sep =" 一种,count/sum(count) 那种,还有一些我只是在不知道什么意思的情况下转录的其他代码。在这让我发疯之前,我需要一条生命线。
这是我的数据:
N_PASSENGERS Count Mean_Dist Mean_Time Mean_Fare
<int> <int> <dbl> <dbl> <dbl>
1 1 57216 2.16 10.2 145.
2 2 8421 1.92 9.21 213.
3 3 2022 2.01 9.67 234.
4 4 572 1.96 9.22 351.
5 5 306 2.40 9.84 505.
6 6 184 1.90 7.63 446.
ggplot(Difference, aes(x = N_PASSENGERS, y = Mean_Dist, size = Count)) +
geom_point() +
scale_size(range = c(0, 20)) +
xlim(0, 6) +
ylim(1.75, 2.5) +
geom_text(aes(label = Count),
size = 3, vjust = 4.2,
WHAT THE HELL GOES HERE TO MAKE SOME COMMAS HAPPEN?) +
theme_minimal() +
theme(legend.position = "none") +
labs(x = "Number of Passengers",
y = "Mean Distance",
title = "Trips by Number of Rides and Distance") +
theme(plot.title = element_text(hjust = .5))
我希望在我的数据点旁边看到像 10,000 这样的数字。相反,我看到像 10000 这样的数字。我很欣赏这是一个幼稚的简单问题。我正在尝试自学 R,因此非常感谢您对此提供的帮助。
您可以使用 scales
包,它允许一些格式选项,例如 comma
、dollar
或 percent
。
df <- data.frame(a=c("a","b","c","d"), b=c(300,1000,2000,4000))
library(ggplot2)
library(scales)
ggplot(df, aes(a, b)) +
geom_point(size=4) +
scale_y_continuous(labels = comma)
您可以在 geom_text
中的美学映射 aes()
内格式化文本标签。
而不是:
... +
geom_text(aes(label = Count), size = 3, vjust = 4.2) +
...
使用:
... +
geom_text(aes(label = scales::comma(Count)), size = 3, vjust = 4.2) +
...
完整数据和代码:
Difference <- read.table(text = "
N_PASSENGERS Count Mean_Dist Mean_Time Mean_Fare
1 1 57216 2.16 10.2 145.
2 2 8421 1.92 9.21 213.
3 3 2022 2.01 9.67 234.
4 4 572 1.96 9.22 351.
5 5 306 2.40 9.84 505.
6 6 184 1.90 7.63 446.")
ggplot(Difference, aes(x = N_PASSENGERS, y = Mean_Dist, size = Count)) +
geom_point() +
scale_size(range = c(0, 20)) +
xlim(0, 6) +
ylim(1.75, 2.5) +
geom_text(aes(label = scales::comma(Count)),
size = 3, vjust = 4.2) +
theme_minimal() +
theme(legend.position = "none") +
labs(x = "Number of Passengers",
y = "Mean Distance",
title = "Trips by Number of Rides and Distance") +
theme(plot.title = element_text(hjust = .5))