数据框中组的频率图
Plot of frequencies of groups in data frame
这是我的一个数据表的摘录:
YR POPSTAT Freq
2001 0 34
2002 0 45
2003 0 32
2015 0 16
2001 1 7
2002 1 11
2003 1 8
2014 1 7
2015 1 3
我想绘制所有 POPSTAT>0 的每年 (YR) 频率直方图。 None 显然可以做到这一点:
for (POPSTAT>0) barplot(table.popstat$Freq)
plot(table.popstat$Freq~table.popstat$YR)
我还需要年份在 x 轴上。任何帮助将不胜感激。
如果要按年份拆分数据,则需要使用循环或 ggplot
require(ggplot2)
ggplot(subset(table.popstat, POPSTAT != 0), aes(x = Freq)) + geom_histogram() + facet_wrap(~YR)
您也可以考虑使用箱线图
ggplot(subset(table.popstat, POPSTAT != 0), aes(x = YR, y = Freq)) + geom_boxplot()
有两种方法可以做到这一点,但首先您需要获取符合您的标准的数据子集 (POPSTAT>0
)。
正在获取 R 中的数据:
plotdata <- dput(plotdata)
structure(list(YR = c(2001L, 2002L, 2003L, 2015L, 2001L, 2002L,
2003L, 2014L, 2015L), POPSTAT = c(0L, 0L, 0L, 0L, 1L, 1L, 1L,
1L, 1L), Freq = c(34L, 45L, 32L, 16L, 7L, 11L, 8L, 7L, 3L)), .Names = c("YR",
"POPSTAT", "Freq"), class = "data.frame", row.names = c(NA, -9L
))
获取子集:
plt_df <- subset(plotdata,POPSTAT>0,select = c(1,3)) #You only want the Year and Freq columns
绘制图形:
基础 R
bplot <- barplot(plt_df$Freq, plt_df$YR, ylim = c(0,13),axes=F)
axis(1,at=bplot,labels=plt_df$YR)
axis(2,seq(0,15,3),c(0,3,6,9,12,15))
ggplot 包
install.packages('ggplot2')
library(ggplot2)
ggplot(plt_df, aes(x=YR,y=Freq)) + geom_bar(stat='identity')
希望对您有所帮助。
这可以分两步完成:过滤、绘图和一行:
with(subset(df, POPSTAT > 0), barplot(Freq, names.arg=YR))
如果你喜欢ggplot2
:
library(ggplot2)
ggplot(subset(df, POPSTAT > 0)) + aes(x=YR, y=Freq) + geom_bar(stat='identity')
这是您的数据的 dput
,因此您的示例是 reproducible。
df <- structure(list(YR = c(2001L, 2002L, 2003L, 2015L, 2001L, 2002L,
2003L, 2014L, 2015L), POPSTAT = c(0L, 0L, 0L, 0L, 1L, 1L, 1L,
1L, 1L), Freq = c(34L, 45L, 32L, 16L, 7L, 11L, 8L, 7L, 3L)),
.Names = c("YR", "POPSTAT", "Freq"),
class = "data.frame", row.names = c(NA, -9L))
这是我的一个数据表的摘录:
YR POPSTAT Freq
2001 0 34
2002 0 45
2003 0 32
2015 0 16
2001 1 7
2002 1 11
2003 1 8
2014 1 7
2015 1 3
我想绘制所有 POPSTAT>0 的每年 (YR) 频率直方图。 None 显然可以做到这一点:
for (POPSTAT>0) barplot(table.popstat$Freq)
plot(table.popstat$Freq~table.popstat$YR)
我还需要年份在 x 轴上。任何帮助将不胜感激。
如果要按年份拆分数据,则需要使用循环或 ggplot
require(ggplot2)
ggplot(subset(table.popstat, POPSTAT != 0), aes(x = Freq)) + geom_histogram() + facet_wrap(~YR)
您也可以考虑使用箱线图
ggplot(subset(table.popstat, POPSTAT != 0), aes(x = YR, y = Freq)) + geom_boxplot()
有两种方法可以做到这一点,但首先您需要获取符合您的标准的数据子集 (POPSTAT>0
)。
正在获取 R 中的数据:
plotdata <- dput(plotdata)
structure(list(YR = c(2001L, 2002L, 2003L, 2015L, 2001L, 2002L,
2003L, 2014L, 2015L), POPSTAT = c(0L, 0L, 0L, 0L, 1L, 1L, 1L,
1L, 1L), Freq = c(34L, 45L, 32L, 16L, 7L, 11L, 8L, 7L, 3L)), .Names = c("YR",
"POPSTAT", "Freq"), class = "data.frame", row.names = c(NA, -9L
))
获取子集:
plt_df <- subset(plotdata,POPSTAT>0,select = c(1,3)) #You only want the Year and Freq columns
绘制图形:
基础 R
bplot <- barplot(plt_df$Freq, plt_df$YR, ylim = c(0,13),axes=F)
axis(1,at=bplot,labels=plt_df$YR)
axis(2,seq(0,15,3),c(0,3,6,9,12,15))
ggplot 包
install.packages('ggplot2')
library(ggplot2)
ggplot(plt_df, aes(x=YR,y=Freq)) + geom_bar(stat='identity')
希望对您有所帮助。
这可以分两步完成:过滤、绘图和一行:
with(subset(df, POPSTAT > 0), barplot(Freq, names.arg=YR))
如果你喜欢ggplot2
:
library(ggplot2)
ggplot(subset(df, POPSTAT > 0)) + aes(x=YR, y=Freq) + geom_bar(stat='identity')
这是您的数据的 dput
,因此您的示例是 reproducible。
df <- structure(list(YR = c(2001L, 2002L, 2003L, 2015L, 2001L, 2002L,
2003L, 2014L, 2015L), POPSTAT = c(0L, 0L, 0L, 0L, 1L, 1L, 1L,
1L, 1L), Freq = c(34L, 45L, 32L, 16L, 7L, 11L, 8L, 7L, 3L)),
.Names = c("YR", "POPSTAT", "Freq"),
class = "data.frame", row.names = c(NA, -9L))