在ggplot2中没有在条形图上看到计数标签

发布于 2025-02-04 18:17:02 字数 2717 浏览 1 评论 0原文

我想将计数标签添加到一个简单的条形图中。我已经尝试使用和没有y = count的以下代码;它运行没有错误,但没有显示标签:

Class <- ggplot(asteroid, aes(x=class, y=count, fill = class, labels = TRUE))

Class + 
geom_bar() + 
ggtitle('Frequency of Hazardous and Non-Hazardous Asteroids by Class') +
xlab('Class Based on Solar System Location') + 
labs(caption = '(*) indicates a potentially hazardous object') +
scale_fill_discrete(labels=c('Amor*','Apollo','Apollo*','Aten','Aten*','Interior Earth Object*'))

我也尝试了

Class <- ggplot(asteroid, aes(x=class, y=count, fill = class))
    
Class + 
geom_bar() + 
ggtitle('Frequency of Hazardous and Non-Hazardous Asteroids by Class') +
xlab('Class Based on Solar System Location') + 
labs(caption = '(*) indicates a potentially hazardous object') +
scale_fill_discrete(labels=c('Amor*','Apollo','Apollo*','Aten','Aten*','Interior Earth Object*'))+
geom_bar(stat = "identity") + 
geom_text(aes(label = count), vjust = 0)

以下错误:

"Error in `f()`:
! Aesthetics must be valid data columns. Problematic aesthetic(s): y = count. 
Did you mistype the name of a data column or forget to add after_stat()?" 

显然,R正在考虑“计数”为count(),但是我可以似乎找到了一种解决这个问题的方法。尝试使用FREQ,计数等。这些都是长镜头,但我有点困惑。

编辑:这是我正在使用的字符变量的快照:

> head(dput(asteroid$class))
> [1] "APO*" "APO*" "APO*" "APO*" "APO*" "APO*"

以及数据集:

  head(dput(小行星))
 
# A tibble: 6 × 17
  Object...1      `Epoch (TDB)` `a (AU)`     e `i (deg)` `w (deg)` `Node (deg)` `M (deg)` `q (AU)` `Q (AU)` `P (yr)`
  <chr>                   <dbl>    <dbl> <dbl>     <dbl>     <dbl>        <dbl>     <dbl>    <dbl>    <dbl>    <dbl>
1 1566 Icarus             57800     1.08 0.827     22.8       31.4         88.0      216.    0.187     1.97     1.12
2 1620 Geographos         57800     1.25 0.335     13.3      277.         337.       104.    0.828     1.66     1.39
3 1862 Apollo             55249     1.47 0.560      6.35     286.          35.7      175.    0.647     2.29     1.78
4 1981 Midas              57800     1.78 0.650     39.8      268.         357.       173.    0.621     2.93     2.37
5 2101 Adonis             57800     1.87 0.765      1.33      43.4        350.       235.    0.441     3.31     2.57
6 2102 Tantalus           57800     1.29 0.299     64.0       61.6         94.4      355.    0.904     1.68     1.47
# … with 6 more variables: `H (mag)` <dbl>, `MOID (AU)` <dbl>, ref <dbl>, class <chr>, Object...16 <chr>,
#   Hazardous <dbl>

I'd like to add count labels to a simple bar chart. I've tried the following code with and without y=count; it runs without error but doesn't display the labels:

Class <- ggplot(asteroid, aes(x=class, y=count, fill = class, labels = TRUE))

Class + 
geom_bar() + 
ggtitle('Frequency of Hazardous and Non-Hazardous Asteroids by Class') +
xlab('Class Based on Solar System Location') + 
labs(caption = '(*) indicates a potentially hazardous object') +
scale_fill_discrete(labels=c('Amor*','Apollo','Apollo*','Aten','Aten*','Interior Earth Object*'))

I have also tried this:

Class <- ggplot(asteroid, aes(x=class, y=count, fill = class))
    
Class + 
geom_bar() + 
ggtitle('Frequency of Hazardous and Non-Hazardous Asteroids by Class') +
xlab('Class Based on Solar System Location') + 
labs(caption = '(*) indicates a potentially hazardous object') +
scale_fill_discrete(labels=c('Amor*','Apollo','Apollo*','Aten','Aten*','Interior Earth Object*'))+
geom_bar(stat = "identity") + 
geom_text(aes(label = count), vjust = 0)

but I'm met with the following error:

"Error in `f()`:
! Aesthetics must be valid data columns. Problematic aesthetic(s): y = count. 
Did you mistype the name of a data column or forget to add after_stat()?" 

Obviously R is thinking "count" to be count() but I can't seem to find a way to work around this. Have tried using freq instead, Count, etc. Those were long shots but I'm a bit stumped.

Edit: Here's a snapshot of the character variable I'm working with:

> head(dput(asteroid$class))
> [1] "APO*" "APO*" "APO*" "APO*" "APO*" "APO*"

And the dataset:

   head(dput(asteroid))
# A tibble: 6 × 17
  Object...1      `Epoch (TDB)` `a (AU)`     e `i (deg)` `w (deg)` `Node (deg)` `M (deg)` `q (AU)` `Q (AU)` `P (yr)`
  <chr>                   <dbl>    <dbl> <dbl>     <dbl>     <dbl>        <dbl>     <dbl>    <dbl>    <dbl>    <dbl>
1 1566 Icarus             57800     1.08 0.827     22.8       31.4         88.0      216.    0.187     1.97     1.12
2 1620 Geographos         57800     1.25 0.335     13.3      277.         337.       104.    0.828     1.66     1.39
3 1862 Apollo             55249     1.47 0.560      6.35     286.          35.7      175.    0.647     2.29     1.78
4 1981 Midas              57800     1.78 0.650     39.8      268.         357.       173.    0.621     2.93     2.37
5 2101 Adonis             57800     1.87 0.765      1.33      43.4        350.       235.    0.441     3.31     2.57
6 2102 Tantalus           57800     1.29 0.299     64.0       61.6         94.4      355.    0.904     1.68     1.47
# … with 6 more variables: `H (mag)` <dbl>, `MOID (AU)` <dbl>, ref <dbl>, class <chr>, Object...16 <chr>,
#   Hazardous <dbl>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃→可爱长大的 2025-02-11 18:17:02

使用您的数据示例更新。

小行星未算,因此我添加了count(class)。我添加了第二个版本,其中数据未数。

library(tidyverse)

asteroid <- data.frame(class = c("apo",'ieo','amor','aten', 'amor'))

# Counted
asteroid |> 
  count(class) |> 
  ggplot(aes(class, n, fill = class)) + 
  geom_col() + 
  ggtitle('Frequency of Hazardous and Non-Hazardous Asteroids by Class') + 
  xlab('Class Based on Solar System Location') + 
  labs(caption = '() indicates a potentially hazardous object') + 
  geom_text(aes(label = n), vjust = 0)

# Uncounted
asteroid |> 
  ggplot(aes(class, fill = class)) + 
  geom_bar() + 
  ggtitle('Frequency of Hazardous and Non-Hazardous Asteroids by Class') + 
  xlab('Class Based on Solar System Location') + 
  labs(caption = '() indicates a potentially hazardous object') + 
  geom_text(aes(label = ..count..), stat = 'count', vjust = 0)

img src =“ https://i.sstatic.net/jp7pw.png.png

alt =“”> < >由

Updated with your data sample.

asteroid is uncounted, so I've added count(class). I've added a second version where the data is uncounted.

library(tidyverse)

asteroid <- data.frame(class = c("apo",'ieo','amor','aten', 'amor'))

# Counted
asteroid |> 
  count(class) |> 
  ggplot(aes(class, n, fill = class)) + 
  geom_col() + 
  ggtitle('Frequency of Hazardous and Non-Hazardous Asteroids by Class') + 
  xlab('Class Based on Solar System Location') + 
  labs(caption = '() indicates a potentially hazardous object') + 
  geom_text(aes(label = n), vjust = 0)

# Uncounted
asteroid |> 
  ggplot(aes(class, fill = class)) + 
  geom_bar() + 
  ggtitle('Frequency of Hazardous and Non-Hazardous Asteroids by Class') + 
  xlab('Class Based on Solar System Location') + 
  labs(caption = '() indicates a potentially hazardous object') + 
  geom_text(aes(label = ..count..), stat = 'count', vjust = 0)

Created on 2022-06-06 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文