[R] [FORGED] Re: bar plot add space to group data
David L Carlson
dc@r|@on @end|ng |rom t@mu@edu
Sun Aug 19 20:10:29 CEST 2018
Actually the data you provided are a data frame and not a matrix as R uses the term. Two columns of gceac are numeric and one is a factor. If we read your data with read.csv() we get:
> str(gceac)
'data.frame': 35 obs. of 3 variables:
$ year : int 2014 2014 2014 2014 2014 2014 2014 2015 2015 2015 ...
$ grade : Factor w/ 7 levels "A","A*","B","C",..: 2 1 3 4 5 6 7 2 1 3 ...
$ percentage: int 9 23 27 19 13 7 2 9 23 27 ...
Now we see problem in grade column. Your plot has A* before A, but the factor is created alphabetically so A comes before A*. We can fix that
> gceac$grade <- factor(gceac$grade, levels=c("A*", "A", "B", "C", "D",
"E", "U"))
> levels(gceac$grade)
[1] "A*" "A" "B" "C" "D" "E" "U"
Now you need a matrix for the barplot. That is simple with xtabs()
> gceac.mat <- xtabs(percentage~grade+year, gceac)
> gceac.mat
year
grade 2014 2015 2016 2017 2018
A* 9 9 8 8 8
A 23 23 23 23 22
B 27 27 27 24 23
C 19 19 19 20 20
D 13 13 13 14 15
E 7 7 7 8 8
U 2 2 3 3 3
Now we can build your bar plot using almost the same command you used:
> clrs <- c('aliceblue', 'aquamarine', 'blue', 'chocolate', 'darkgreen',
+ 'firebrick', 'violet')
> barplot(gceac.mat, xlab='year', ylab='percentage of each grade',
+ col=clrs, legend=TRUE, args.legend = list("topright",
+ title='grades'), main='A-level grades, chemistry', beside=T,
+ space=c(0, 1), ylim=c(0,35))
I defined clrs so that the barplot() function would be easier to read, but it works the same your way. Now we just need legend=TRUE and we can position the legend in the top right with the args.legend= argument. The legend overlaps the bars a bit so I increased the y-axis to 35. A .png file is attached.
David L. Carlson
Department of Anthropology
Texas A&M University
-----Original Message-----
From: R-help [mailto:r-help-bounces using r-project.org] On Behalf Of Peter Langfelder
Sent: Sunday, August 19, 2018 10:51 AM
To: citc using disroot.org
Cc: r-help <r-help using r-project.org>
Subject: Re: [R] [FORGED] Re: bar plot add space to group data
On Sun, Aug 19, 2018 at 7:15 AM <citc using disroot.org> wrote:
>
> August 19, 2018 4:58 AM, "Peter Langfelder" <peter.langfelder using gmail.com> wrote:
>
> > To the OP, try formatting the data to be plotted as a matrix, not as a
> > vector
>
> CSV data provided in a previous message; is not the data formatted as a matrix?
I meant the data you give to barplot - your code supplies only the
third column of the data frame, so barplot only sees a vector. I would
try something like
plotData = do.call(cbind, tapply(csv.data$percentage, csv.data$year, identity))
barplot(plotData, <rest of your argument>)
Peter
______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: BarPlot.png
Type: image/png
Size: 8565 bytes
Desc: BarPlot.png
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20180819/784c7f8c/attachment-0002.png>
More information about the R-help
mailing list