简体   繁体   English

R矩阵和字符的条形图

[英]R Barplot of Matrix and characters

I am new to R, trying to learn as much as possible, but can't claim to know much at this point! 我是R的新手,它试图学习尽可能多的知识,但现在还不能声称知道太多!

I am trying to plot the outcome , in respect of each time number from the matrix of data I have below. 我试图从下面的数据矩阵中绘制每个timeoutcome

no. time    outcome
1   7   win
2   7   win
3   8   win
4   8   lose
5   8   draw
6   10  win
7   10  win
8   10  lose
9   10  win
10  10  draw

So for example, for any given time, I would like to use barplot to show bars for the amount of win , lose , or draw occurrences. 因此,举例来说,对于任何给定的时间,我想使用barplot来显示winlosedraw次数的小节。

I have sort of done this by creating a subset based on time , then creating a vector of the outcome , then making a table of the vector, and then plotting the table against a vector of numbers for the height. 我通过基于time创建一个子集,然后创建outcome的向量,然后创建向量的表,然后针对高度的数字向量绘制表来完成此工作。

Sorry, I can't post images, but anyway, the above works, but only for one given time . 抱歉,我无法发布图片,但是无论如何,以上方法都有效,但仅在给定time可用。 I would like to show the occurrences for all times. 我想展示所有时间的情况。

I'd greatly appreciate some help if possible, even if just a point in the direction of useful reading material! 我将不胜感激,如果有可能的话,即使只是对有用的阅读材料的指导一点帮助!

This task is easy with ggplot2 : 使用ggplot2可以轻松完成此任务:

dat <- read.table(text = "no. time    outcome
1   7   win
2   7   win
3   8   win
4   8   lose
5   8   draw
6   10  win
7   10  win
8   10  lose
9   10  win
10  10  draw", header = TRUE)

library(ggplot2)
ggplot(dat, aes(x = as.factor(time), fill = outcome)) +
  geom_bar(stat = "bin")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM