变量内某个平均值的比例条形图

时间:2018-12-03 05:46:14

标签: r

您好,我想用R中的简单图形功能,如何制作条形图来显示变量结果的比例(按年计算未解决的凶杀案),可以认为是 将任何特定年份未解决的凶杀案除以 当年未解决凶杀的总数 年。从每个计算的百分比中,我想将它们全部放入一个大条形图中  到目前为止,我已经做到了:

#create a table with desired data (Total cases and year)
(TC<-table(dataset$Crime.Solved,dataset$Year))
 **Year** 
 1980  1981  1982  1983  1984  1985  1986  1987  1988  1989  1990  1991  
       1992  1993  1994

 **No** 
  6104  5177  5252  4925  4573  4522  4975  5076  5171  5491  5985  7048  
       7676  7654  7095

 **Yes**
 16371 15446 14720 14179 12915 13250 14380 13101 13042 13834 14642 14876 
       15506 16050 15469

  **Year** 
  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  
   2007  2008  2009

  **No** 
  6571  5681  5458  4542  4241  4289  4608  4821  5112  4917  5082  5191  
  5371  4931  4532

 **Yes**
  13970 12727 11735 10949  9896  9831 10627 10917 10854 10827 11249 11550 
  11392 10186 10856

 **Year**
 2010  2011  2012  2013  2014

 **No**
  4328  4236  4391  4321  4312

 **Yes**
 10406 10163 10340  9774  9689
#create a table with desired data (unsolved cases and year)
(USC<-table(unsolvedCASES$Crime.Solved =="No",unsolvedCASES$Year))
**Year**
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 
       1994 1995 1996 1997

  **TRUE**
  6104 5177 5252 4925 4573 4522 4975 5076 5171 5491 5985 7048 7676 7654 
       7095 6571 5681 5458

  **Year**
  1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 
       2012 2013 2014

  **TRUE**
  4542 4241 4289 4608 4821 5112 4917 5082 5191 5371 4931 4532 4328 4236 
       4391 4321 4312
#now try to get the percent of unsolved homicides each year by deviding unsolved homicides each year by total homicides each year

proportions <- table((dataset$Crime.Solved=="No"/dataset$Crime.Solved ),dataset$Year)

但这只是给我说了错误

  

(“否”,dataset $ Crime.Solved):“ /”对因素没有意义

地狱!

谢谢!

[https://i.stack.imgur.com/8ED9l.png][1]

2 个答案:

答案 0 :(得分:1)

(这是我对先前答案的修改) 这是一个简单的解决方案: 假设您的数据大致如下所示(仅包含您感兴趣的两个变量):

df <- data.frame(
  Year = c(rep(1980,4), rep(1981,5), rep(1982,6)),
  Crime.Solved = c("yes","no","yes","yes","no","yes","no","no","yes","yes","no","no","yes","no","no"    )
)

现在您创建一个2x2频率列表:

freq <- table(df$Year, df$Crime.Solved)

接下来,您将按行计算比例(参见数字1):

data <- prop.table(freq, 1) 
              no       yes
  1980 0.2500000 0.7500000
  1981 0.6000000 0.4000000
  1982 0.6666667 0.3333333

您最关注的数据-每年未解决的犯罪比例-在最左边的列中,即data[,1]中;要在每个栏中显示相应的年份,请通过rownames(data)访问它们:

barplot(data[,1], main = "Proportions of unsolved crime per year",
    names.arg = row.names(data), las = 3)

enter image description here

答案 1 :(得分:0)

这是一个非常简单的解决方案:

首先,像这样构造数据(每个变量都在单独的列中):

df <- data.frame(
  year = c(paste("198", 0:9, sep="")),
  crimes = c(sample(100, 10)),
  unsolved = c(sample(40, 10))
)
head(df)
  year crimes unsolved
1 1980     86        2
2 1981     43       23
3 1982     75       37
4 1983      4       33
5 1984     99       20
6 1985     37       28

要获取所有犯罪中未解决犯罪的比例,请定义新变量$prop.unsolved,方法是将$unsolved除以$crimes

df$prop.unsolved <- df$unsolved/df$crimes

现在您可以在条形图中绘制这些比例:

barplot(df$prop.unsolved, main = "Proportions of unsolved crime per year",
    names.arg = df$year, las = 3)

enter image description here