如何制作带有标签和计数的条形图?
How do I make a bar graph with labels and counts?
我知道这是一个简单的问题,但我无法正确显示此图表。
我有一个像这样的数据集:
pet
pet_counts
dog
22
cat
100
birs
2
我想制作一个条形图,在 X 轴上标记每只动物,在 Y 轴上标记计数。当我指定时,实验室只是更改了标签中的文字,而不是刻度线下方的值.
我希望 x 轴表示狗,然后在 Y 轴上有一个 f 22 的计数,例如。
我试过:
Graph <- ggplot(data = animals, aes(pet_counts)) + geom_bar(stat=“count”) + labs(x = “pet”)
我认为您正在寻找 geom_col()
而不是 geom_bar()
:
library(dplyr)
library(ggplot2)
animals <- tibble(
pet = c("dog", "cat", "birds"),
pet_counts = c(22, 100, 2)
)
animals %>%
ggplot(aes(x = pet, y = pet_counts)) +
geom_col() +
labs(
x = "Pet",
y = "Count"
)
labs()
函数是可选的,只会将轴上的名称更改为更易读的名称。
结果:
geom_col()
和geom_bar()
的区别,根据文档:
geom_bar()
makes the height of the bar proportional to the number of cases in each group. If you want the heights of the bars to represent values in the data, use geom_col()
instead.
既然你已经有了 pet_counts
,你应该使用 geom_col()
。
我知道这是一个简单的问题,但我无法正确显示此图表。
我有一个像这样的数据集:
pet | pet_counts |
---|---|
dog | 22 |
cat | 100 |
birs | 2 |
我想制作一个条形图,在 X 轴上标记每只动物,在 Y 轴上标记计数。当我指定时,实验室只是更改了标签中的文字,而不是刻度线下方的值.
我希望 x 轴表示狗,然后在 Y 轴上有一个 f 22 的计数,例如。
我试过:
Graph <- ggplot(data = animals, aes(pet_counts)) + geom_bar(stat=“count”) + labs(x = “pet”)
我认为您正在寻找 geom_col()
而不是 geom_bar()
:
library(dplyr)
library(ggplot2)
animals <- tibble(
pet = c("dog", "cat", "birds"),
pet_counts = c(22, 100, 2)
)
animals %>%
ggplot(aes(x = pet, y = pet_counts)) +
geom_col() +
labs(
x = "Pet",
y = "Count"
)
labs()
函数是可选的,只会将轴上的名称更改为更易读的名称。
结果:
geom_col()
和geom_bar()
的区别,根据文档:
geom_bar()
makes the height of the bar proportional to the number of cases in each group. If you want the heights of the bars to represent values in the data, usegeom_col()
instead.
既然你已经有了 pet_counts
,你应该使用 geom_col()
。