R 如何将数据分组,然后在ggplot2中绘制条形图
我有数据帧df和3列,例如R 如何将数据分组,然后在ggplot2中绘制条形图,r,ggplot2,grouping,bar-chart,R,Ggplot2,Grouping,Bar Chart,我有数据帧df和3列,例如 NUMERIC1: NUMERIC2: GROUP(CHARACTER): 100 1 A 200 2 B 300 3 C 400 4 A 我想按GROUPCHARACTER对NUMERIC1进行分组,然后计算每组的平均值。 诸如此类: mean(N
NUMERIC1: NUMERIC2: GROUP(CHARACTER):
100 1 A
200 2 B
300 3 C
400 4 A
我想按GROUPCHARACTER对NUMERIC1进行分组,然后计算每组的平均值。
诸如此类:
mean(NUMERIC1): GROUP(CHARACTER):
250 A
200 B
300 C
最后,我想使用ggplot2绘制条形图,其中x轴上有GROUPCHARACTER,y轴上有meanNUMERIC。
它应该是这样的:
我曾经
mean <- tapply(df$NUMERIC1, df$GROUP(CHARACTER), FUN=mean)
但我不确定是否可以,即使可以,我也不知道下一步该怎么办。我建议如下:
#Imports; data.table, which allows for really convenient "apply a function to
#"each part of a df, by unique value", and ggplot2
library(data.table)
library(ggplot2)
#Convert df to a data.table. It remains a data.frame, so any function that works
#on a data.frame can still work here.
data <- as.data.table(df)
#By each unique value in "CHARACTER", subset and calculate the mean of the
#NUMERIC1 values within that subset. You end up with a data.frame/data.table
#with the columns CHARACTER and mean_value
data <- data[, j = list(mean_value = mean(NUMERIC1)), by = "CHARACTER"]
#And now we play the plotting game (the plotting game is boring, lets
#play Hungry Hungry Hippos!)
plot <- ggplot(data, aes(CHARACTER, mean_value)) + geom_bar()
#And that should do it.
res <- aggregate(NUMERIC1 ~ GROUP, data = df, FUN = mean)
ggplot(res, aes(x = GROUP, y = NUMERIC1)) + geom_bar(stat = "identity")
尝试以下方法:
#Imports; data.table, which allows for really convenient "apply a function to
#"each part of a df, by unique value", and ggplot2
library(data.table)
library(ggplot2)
#Convert df to a data.table. It remains a data.frame, so any function that works
#on a data.frame can still work here.
data <- as.data.table(df)
#By each unique value in "CHARACTER", subset and calculate the mean of the
#NUMERIC1 values within that subset. You end up with a data.frame/data.table
#with the columns CHARACTER and mean_value
data <- data[, j = list(mean_value = mean(NUMERIC1)), by = "CHARACTER"]
#And now we play the plotting game (the plotting game is boring, lets
#play Hungry Hungry Hippos!)
plot <- ggplot(data, aes(CHARACTER, mean_value)) + geom_bar()
#And that should do it.
res <- aggregate(NUMERIC1 ~ GROUP, data = df, FUN = mean)
ggplot(res, aes(x = GROUP, y = NUMERIC1)) + geom_bar(stat = "identity")
数据
下面是一个使用dplyr创建摘要的解决方案。在这种情况下,摘要是在ggplot中动态创建的,但是您也可以先创建一个单独的摘要数据帧,然后将其提供给ggplot
由于绘制的是平均值,而不是计数,因此使用点而不是条形图可能更有意义。例如:
ggplot(df %>% group_by(GROUP) %>%
summarise(`Mean NUMERIC1`=mean(NUMERIC1)),
aes(GROUP, `Mean NUMERIC1`)) +
geom_point(pch=21, size=5, fill="blue") +
coord_cartesian(ylim=c(0,310))
这就是统计结果玛丽。。。设计用于:
colnames(df) <- c("N1","N2","GROUP")
library(ggplot2)
ggplot(df) + stat_summary(aes(x=GROUP,y=N1),fun.y=mean,geom="bar",
fill="lightblue",col="grey50")
当您可以对自己的代码和条形图执行相同操作时,为什么要进行ggplot:
barplot(tapply(df$NUMERIC1, df$GROUP, FUN=mean))
我已经安装并加载了ggplot2,但是当我尝试绘制绘图时,我可以看到:错误:找不到函数ggplot2,因为没有这样的函数。现在试试+1,关于统计摘要的观点很好!这就是生成条形图的ggplot2惯用方法。