r - 如何使用 R lattice reshape 堆叠条形图的数据

标签 r plot bar-chart reshape lattice

<分区>

我在表中有一堆数据(从 csv 导入),格式如下:

date        classes         score
9/1/11       french          34
9/1/11       english         34
9/1/11       french          34
9/1/11       spanish         34
9/2/11       french          34
9/2/11       english         34
9/3/11       spanish         34
9/3/11       spanish         34
9/5/11       spanish         34
9/5/11       english         34
9/5/11       french          34
9/5/11       english         34

忽略分数栏,它不重要。

我需要根据日期计算参加英语、西类牙语或法语类(class)的学生总数,即。我需要先按日期对其进行分组,然后根据语言将每一天分成更多的 block ,并将其绘制为堆叠条形图,如下所示。每个条代表一个日期,条的每个横截面代表一种语言。

一旦我得到矩阵形式的数据,我就知道如何做到这一点,其中每一行代表一个日期,每一列代表一个属性(或语言)。所以我假设数据是 csv 中的那种形式:

ie           french      english       spanish
9/1/11       2           1             1
9/2/11       1           1             0          
9/3/11       0           0             2
9/5/11       1           2             1

然后我可以做:

directory<-"C:\\test\\language.csv"
ourdata6<-read.csv(directory)

language<-as.matrix(ourdata6)

barchart(prop.table(language), horizontal=FALSE, auto.key = list(space='right',cex=.5,border=T,points=F, lines=F,lwd=5,text=c('french','spanish','enligsh'),cex=.6), main = list(label="Distribution of classes 10",cex=2.5),  ylab = list(", cex=1.7),xlab.top=list("testing",cex=1.2))

挑战在于将数据从原始格式转换为我需要的格式。

我试过了

a<-count(language, c("date", "classes"))

它给我按两者排序的计数,但它是垂直形式

ie
9/1/11       french           2             
9/1/11       english          1                       
9/1/11       spanish          1            
etc...

我需要调整它,使其成为每个日期的一行。另外,如果其中一些可能为零,那么我需要它们的占位符,即。第一列必须对应法语,第二列必须对应英语才能使我当前的设置正常工作。

关于如何执行此操作的任何想法,或者我使用 matrix + prop.table 的方法是否正确?有没有更简单的方法来做到这一点?

最佳答案

假设您的数据位于名为 df 的数据框中,您可以借助 dplyrtidyr 包来实现:

library(dplyr)
library(tidyr)

wide <- df %>% select(date,classes) %>%
  group_by(date,classes) %>%
  summarise(n=n()) %>%            # as @akrun said, you can also use tally()
  spread(classes, n, fill=0)

使用您提供的示例数据,这将产生以下数据框:

  date english french spanish
9/1/11       1      2       1
9/2/11       1      1       0
9/3/11       0      0       2
9/5/11       2      1       1

现在你可以制作一个格子图:

barchart(date ~ english + french + spanish, data=wide, stack = TRUE,
         main = list(label="Distribution of language classes",cex=1.6),
         xlab = list("Number of classes", cex=1.1),
         ylab = list("Date", cex=1.1),
         auto.key = list(space='right',cex=1.2,text=c('Enligsh','French','Spanish')))

给出了以下情节: enter image description here


编辑:除了使用点阵图,您还可以使用 ggplot2,这(至少在我看来)更容易理解。一个例子:

# convert the wide dataframe to a long one
long <- wide %>% gather(class, n, -date)

# load ggplot2
library(ggplot2)

# create the plot
ggplot(long, aes(date, n, fill=class)) +
  geom_bar(stat="identity", position="stack") +
  coord_flip() +
  theme_bw() +
  theme(axis.title=element_blank(), axis.text=element_text(size=12))

给出: enter image description here

关于r - 如何使用 R lattice reshape 堆叠条形图的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25935202/

相关文章:

r - 如何在 GGPLOT theme_classic() 中启用 x 轴和 y 轴

python - 在 Python 中调用其他函数来制作子图

python - 减小矢量化等高线图的大小

javascript - D3 X轴上加间隔

r - 基于 R 中日期的匹配值的 if 语句

arrays - R中的元素比较

Python:如何使用 matplotlib 在 python 中绘制条形图?

python - 如何使用 pyplot.barh() 在每个条上显示条的值

r - 比较 R 中的两个字符向量

r - 使用 for 循环将威 bool 观测值保存在矩阵中