通过列值在 R 中创建条形图
Create a barplot in R by a column value
例如,设数据框df为
Location
Snowfall
London
0.4
London
1.3
NYC
4.3
NYC
0.2
Paris
3.0
London
2.0
有没有办法按位置制作总降雪量的条形图?
伦敦的酒吧长度为 3.7 等等...
这里有一个tidyverse
方法
数据
df <-
tibble::tribble(
~Location, ~Snowfall,
"London", 0.4,
"London", 1.3,
"NYC", 4.3,
"NYC", 0.2,
"Paris", 3,
"London", 2
)
情节
library(dplyr)
library(ggplot2)
df %>%
#Grouping by Location
group_by(Location) %>%
#Sum of Snowfall by Location
summarise(Snowfall = sum(Snowfall)) %>%
ggplot(aes(Location,Snowfall))+
geom_col(width = .5)
在基础 R 中,您可以先 aggregate
然后使用 barplot
。
barplot(Snowfall~Location, aggregate(Snowfall~Location, df, sum))
例如,设数据框df为
Location | Snowfall |
---|---|
London | 0.4 |
London | 1.3 |
NYC | 4.3 |
NYC | 0.2 |
Paris | 3.0 |
London | 2.0 |
有没有办法按位置制作总降雪量的条形图?
伦敦的酒吧长度为 3.7 等等...
这里有一个tidyverse
方法
数据
df <-
tibble::tribble(
~Location, ~Snowfall,
"London", 0.4,
"London", 1.3,
"NYC", 4.3,
"NYC", 0.2,
"Paris", 3,
"London", 2
)
情节
library(dplyr)
library(ggplot2)
df %>%
#Grouping by Location
group_by(Location) %>%
#Sum of Snowfall by Location
summarise(Snowfall = sum(Snowfall)) %>%
ggplot(aes(Location,Snowfall))+
geom_col(width = .5)
在基础 R 中,您可以先 aggregate
然后使用 barplot
。
barplot(Snowfall~Location, aggregate(Snowfall~Location, df, sum))