在ggplot2中没有在条形图上看到计数标签
我想将计数标签添加到一个简单的条形图中。我已经尝试使用和没有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用您的数据示例更新。
小行星
未算,因此我添加了count(class)
。我添加了第二个版本,其中数据未数。img src =“ https://i.sstatic.net/jp7pw.png.png
alt =“”> < >由
Updated with your data sample.
asteroid
is uncounted, so I've addedcount(class)
. I've added a second version where the data is uncounted.Created on 2022-06-06 by the reprex package (v2.0.1)