R 使用ggplot绘制按性别划分的前3种最常食用水果的直方图
我有以下数据表:R 使用ggplot绘制按性别划分的前3种最常食用水果的直方图,r,ggplot2,R,Ggplot2,我有以下数据表: dt <- data.table(Gender=c("M", "F", "M", "M", "F"), Strawberry=c(1,2,2,1,5), Pineapple=c(1,5,4,1,3), Apple=c(1,1,1,1,1), Grapefruit=c(9,2,4,1,2)) dt类似于此,使用reformae2和dplyr修复数据: library(reshape2) library(dplyr) dt$id <- row.names(dt) dt
dt <- data.table(Gender=c("M", "F", "M", "M", "F"), Strawberry=c(1,2,2,1,5), Pineapple=c(1,5,4,1,3), Apple=c(1,1,1,1,1), Grapefruit=c(9,2,4,1,2))
dt类似于此,使用reformae2
和dplyr
修复数据:
library(reshape2)
library(dplyr)
dt$id <- row.names(dt)
dt <- melt(dt, id = row.names(id))
dt <- dt %>% group_by(Gender, variable) %>%
summarise(total = sum(value)) %>%
group_by(Gender) %>%
filter(total %in% sort(total, decreasing = TRUE)[1:3])
ggplot(data = dt, aes(x = variable, y=total)) +
geom_bar(stat = 'identity') + facet_grid(~Gender)
library(重塑2)
图书馆(dplyr)
dt$id%
按性别划分的组别%>%
筛选器(排序百分比中的总计%(总计,递减=真)[1:3])
ggplot(数据=dt,aes(x=变量,y=总数))+
几何图形栏(stat='identity')+facet\u网格(~Gender)
我相信重新排列可以用更少的步骤完成
library(reshape2)
library(dplyr)
dt$id <- row.names(dt)
dt <- melt(dt, id = row.names(id))
dt <- dt %>% group_by(Gender, variable) %>%
summarise(total = sum(value)) %>%
group_by(Gender) %>%
filter(total %in% sort(total, decreasing = TRUE)[1:3])
ggplot(data = dt, aes(x = variable, y=total)) +
geom_bar(stat = 'identity') + facet_grid(~Gender)