bar plot add space to group data

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

bar plot add space to group data

citc
R-users,

Can someone please advise how to improve the code below that was used to produce the graph shown at the following hyperlink (https://chemistryinthecity.neocities.org/content/entry1808.html#17)? The request is to add space between the annual data groups.

barplot(gceac[,3], xlab='year', ylab='percentage of each grade', col=c('aliceblue', 'aquamarine', 'blue', 'chocolate', 'darkgreen', 'firebrick', 'violet'), legend=gceac[1:7,2], args.legend = list(x = 40, y = 30, title='grades'), main='A-level grades, chemistry', beside=T, space=c(0,2), ylim=c(0,30))
years<-c(2014,2015,2016,2017,2018)
mtext(years, side=1, at=c(5, 12, 19, 26, 33))
R-users, Can someone please advise how to improve the code below that was used to produce the graph shown at the following hyperlink (https://chemistryinthecity.neocities.org/content/entry1808.html#17)? The request is to add space between the annual data groups.  barplot(gceac[,3], xlab='year', ylab='percentage of each grade', col=c('aliceblue', 'aquamarine', 'blue', 'chocolate', 'darkgreen', 'firebrick', 'violet'), legend=gceac[1:7,2], args.legend = list(x = 40, y = 30, title='grades'), main='A-level grades, chemistry', beside=T, space=c(0,2), ylim=c(0,30)) years<-c(2014,2015,2016,2017,2018) mtext(years, side=1, at=c(5, 12, 19, 26, 33))

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: bar plot add space to group data

Christopher W. Ryan
Using the lattice package would provide an easy way to distinguish years, by putting them in different panels. Lattice would also help avoid some other features of this graph that, in my opinion, are suboptimal. See Tufte or Cleveland.

Chris Ryan
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.

On August 17, 2018 7:55:43 AM EDT, [hidden email] wrote:

>R-users,
>
>Can someone please advise how to improve the code below that was used
>to produce the graph shown at the following hyperlink
>(https://chemistryinthecity.neocities.org/content/entry1808.html#17)?
>The request is to add space between the annual data groups.
>
>barplot(gceac[,3], xlab='year', ylab='percentage of each grade',
>col=c('aliceblue', 'aquamarine', 'blue', 'chocolate', 'darkgreen',
>'firebrick', 'violet'), legend=gceac[1:7,2], args.legend = list(x = 40,
>y = 30, title='grades'), main='A-level grades, chemistry', beside=T,
>space=c(0,2), ylim=c(0,30))
>years<-c(2014,2015,2016,2017,2018)
>mtext(years, side=1, at=c(5, 12, 19, 26, 33))
>R-users, Can someone please advise how to improve the code below that
>was used to produce the graph shown at the following hyperlink
>(https://chemistryinthecity.neocities.org/content/entry1808.html#17)?
>The request is to add space between the annual data groups.
>barplot(gceac[,3], xlab='year', ylab='percentage of each grade',
>col=c('aliceblue', 'aquamarine', 'blue', 'chocolate', 'darkgreen',
>'firebrick', 'violet'), legend=gceac[1:7,2], args.legend = list(x = 40,
>y = 30, title='grades'), main='A-level grades, chemistry', beside=T,
>space=c(0,2), ylim=c(0,30)) years<-c(2014,2015,2016,2017,2018)
>mtext(years, side=1, at=c(5, 12, 19, 26, 33))
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: bar plot add space to group data

David Carlson
In reply to this post by citc
Notice below that your message is substantially scrambled. R-Help is a plain text only list so you should set your email client to produce plain text messages.

The best place to start is with the manual page for the barplot() function:

?barplot or help(barplot)

You will find the description of the space= argument useful.

----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352

-----Original Message-----
From: R-help <[hidden email]> On Behalf Of [hidden email]
Sent: Friday, August 17, 2018 6:56 AM
To: [hidden email]
Subject: [R] bar plot add space to group data

R-users,

Can someone please advise how to improve the code below that was used to produce the graph shown at the following hyperlink (https://chemistryinthecity.neocities.org/content/entry1808.html#17)? The request is to add space between the annual data groups.

barplot(gceac[,3], xlab='year', ylab='percentage of each grade', col=c('aliceblue', 'aquamarine', 'blue', 'chocolate', 'darkgreen', 'firebrick', 'violet'), legend=gceac[1:7,2], args.legend = list(x = 40, y = 30, title='grades'), main='A-level grades, chemistry', beside=T, space=c(0,2), ylim=c(0,30))
years<-c(2014,2015,2016,2017,2018)
mtext(years, side=1, at=c(5, 12, 19, 26, 33)) R-users, Can someone please advise how to improve the code below that was used to produce the graph shown at the following hyperlink (https://chemistryinthecity.neocities.org/content/entry1808.html#17)? The request is to add space between the annual data groups.  barplot(gceac[,3], xlab='year', ylab='percentage of each grade', col=c('aliceblue', 'aquamarine', 'blue', 'chocolate', 'darkgreen', 'firebrick', 'violet'), legend=gceac[1:7,2], args.legend = list(x = 40, y = 30, title='grades'), main='A-level grades, chemistry', beside=T, space=c(0,2), ylim=c(0,30)) years<-c(2014,2015,2016,2017,2018) mtext(years, side=1, at=c(5, 12, 19, 26, 33))

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: bar plot add space to group data

Rolf Turner

On 18/08/18 02:37, David L Carlson wrote:

> Notice below that your message is substantially scrambled. R-Help is a
> plain text only list so you should set your email client to produce plain text messages.
>
> The best place to start is with the manual page for the barplot() function:
>
> ?barplot or help(barplot)
>
> You will find the description of the space= argument useful.

<SNIP>

If one struggles through the garbled html bumff, one sees that the OP
*did* indeed use the "space=" argument.  However it does not appear to
have had the desired effect, and I cannot see why.  Since the OP did not
supply the data, I cannot experiment.

Perhaps someone else will have some insight.

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: bar plot add space to group data

Jim Lemon-4
In reply to this post by citc
Hi citc,
Try this:

geac<-matrix(c(9,9,8,8,8,23,23,23,23,22,27,27,27,25,24,
 19,19,19,20,20,17,17,17,18,19,8,8,8,9,9,2,2,3,3,3),ncol=5,byrow=TRUE)
library(plotrix)
barp(geac,names.arg=2014:2018,main="A level grades chemistry",
 xlab="Year",ylab="Percentage of each grade",ylim=c(0,30),
 col=c("white","lightblue","blue","orange","green","red","pink"))

Jim

On Fri, Aug 17, 2018 at 9:55 PM,  <[hidden email]> wrote:

> R-users,
>
> Can someone please advise how to improve the code below that was used to produce the graph shown at the following hyperlink (https://chemistryinthecity.neocities.org/content/entry1808.html#17)? The request is to add space between the annual data groups.
>
> barplot(gceac[,3], xlab='year', ylab='percentage of each grade', col=c('aliceblue', 'aquamarine', 'blue', 'chocolate', 'darkgreen', 'firebrick', 'violet'), legend=gceac[1:7,2], args.legend = list(x = 40, y = 30, title='grades'), main='A-level grades, chemistry', beside=T, space=c(0,2), ylim=c(0,30))
> years<-c(2014,2015,2016,2017,2018)
> mtext(years, side=1, at=c(5, 12, 19, 26, 33))
> R-users, Can someone please advise how to improve the code below that was used to produce the graph shown at the following hyperlink (https://chemistryinthecity.neocities.org/content/entry1808.html#17)? The request is to add space between the annual data groups.  barplot(gceac[,3], xlab='year', ylab='percentage of each grade', col=c('aliceblue', 'aquamarine', 'blue', 'chocolate', 'darkgreen', 'firebrick', 'violet'), legend=gceac[1:7,2], args.legend = list(x = 40, y = 30, title='grades'), main='A-level grades, chemistry', beside=T, space=c(0,2), ylim=c(0,30)) years<-c(2014,2015,2016,2017,2018) mtext(years, side=1, at=c(5, 12, 19, 26, 33))
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: bar plot add space to group data

Rolf Turner

Jim:

(a) There's no legend.

(b) I am still curious as to why the OP's code didn't work, in that
the "space=c(0,2)" argument seemed to have no effect.

cheers,

Rolf

On 18/08/18 20:45, Jim Lemon wrote:

> Hi citc,
> Try this:
>
> geac<-matrix(c(9,9,8,8,8,23,23,23,23,22,27,27,27,25,24,
>   19,19,19,20,20,17,17,17,18,19,8,8,8,9,9,2,2,3,3,3),ncol=5,byrow=TRUE)
> library(plotrix)
> barp(geac,names.arg=2014:2018,main="A level grades chemistry",
>   xlab="Year",ylab="Percentage of each grade",ylim=c(0,30),
>   col=c("white","lightblue","blue","orange","green","red","pink"))
>
> Jim
>
> On Fri, Aug 17, 2018 at 9:55 PM,  <[hidden email]> wrote:
>> R-users,
>>
>> Can someone please advise how to improve the code below that was used to produce the graph shown at the following hyperlink (https://chemistryinthecity.neocities.org/content/entry1808.html#17)? The request is to add space between the annual data groups.
>>
>> barplot(gceac[,3], xlab='year', ylab='percentage of each grade', col=c('aliceblue', 'aquamarine', 'blue', 'chocolate', 'darkgreen', 'firebrick', 'violet'), legend=gceac[1:7,2], args.legend = list(x = 40, y = 30, title='grades'), main='A-level grades, chemistry', beside=T, space=c(0,2), ylim=c(0,30))
>> years<-c(2014,2015,2016,2017,2018)
>> mtext(years, side=1, at=c(5, 12, 19, 26, 33))
>> R-users, Can someone please advise how to improve the code below that was used to produce the graph shown at the following hyperlink (https://chemistryinthecity.neocities.org/content/entry1808.html#17)? The request is to add space between the annual data groups.  barplot(gceac[,3], xlab='year', ylab='percentage of each grade', col=c('aliceblue', 'aquamarine', 'blue', 'chocolate', 'darkgreen', 'firebrick', 'violet'), legend=gceac[1:7,2], args.legend = list(x = 40, y = 30, title='grades'), main='A-level grades, chemistry', beside=T, space=c(0,2), ylim=c(0,30)) years<-c(2014,2015,2016,2017,2018) mtext(years, side=1, at=c(5, 12, 19, 26, 33))

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: bar plot add space to group data

Jim Lemon-4
Hi Rolf,
That's what comes of being in a hurry.

legend(4.1,30,c("A+","A","B","C","D","E","U"),
 fill=c("white","lightblue","blue","orange","green","red","pink"))

and I thank you for alerting me to the fact that the legend arguments
in barp don't position the legend properly. I'll fix it.

Jim


On Sun, Aug 19, 2018 at 9:52 AM, Rolf Turner <[hidden email]> wrote:

>
> Jim:
>
> (a) There's no legend.
>
> (b) I am still curious as to why the OP's code didn't work, in that
> the "space=c(0,2)" argument seemed to have no effect.
>
> cheers,
>
> Rolf
>
> On 18/08/18 20:45, Jim Lemon wrote:
>>
>> Hi citc,
>> Try this:
>>
>> geac<-matrix(c(9,9,8,8,8,23,23,23,23,22,27,27,27,25,24,
>>   19,19,19,20,20,17,17,17,18,19,8,8,8,9,9,2,2,3,3,3),ncol=5,byrow=TRUE)
>> library(plotrix)
>> barp(geac,names.arg=2014:2018,main="A level grades chemistry",
>>   xlab="Year",ylab="Percentage of each grade",ylim=c(0,30),
>>   col=c("white","lightblue","blue","orange","green","red","pink"))
>>
>> Jim
>>
>> On Fri, Aug 17, 2018 at 9:55 PM,  <[hidden email]> wrote:
>>>
>>> R-users,
>>>
>>> Can someone please advise how to improve the code below that was used to
>>> produce the graph shown at the following hyperlink
>>> (https://chemistryinthecity.neocities.org/content/entry1808.html#17)? The
>>> request is to add space between the annual data groups.
>>>
>>> barplot(gceac[,3], xlab='year', ylab='percentage of each grade',
>>> col=c('aliceblue', 'aquamarine', 'blue', 'chocolate', 'darkgreen',
>>> 'firebrick', 'violet'), legend=gceac[1:7,2], args.legend = list(x = 40, y =
>>> 30, title='grades'), main='A-level grades, chemistry', beside=T,
>>> space=c(0,2), ylim=c(0,30))
>>> years<-c(2014,2015,2016,2017,2018)
>>> mtext(years, side=1, at=c(5, 12, 19, 26, 33))
>>> R-users, Can someone please advise how to improve the code below that was
>>> used to produce the graph shown at the following hyperlink
>>> (https://chemistryinthecity.neocities.org/content/entry1808.html#17)? The
>>> request is to add space between the annual data groups.  barplot(gceac[,3],
>>> xlab='year', ylab='percentage of each grade', col=c('aliceblue',
>>> 'aquamarine', 'blue', 'chocolate', 'darkgreen', 'firebrick', 'violet'),
>>> legend=gceac[1:7,2], args.legend = list(x = 40, y = 30, title='grades'),
>>> main='A-level grades, chemistry', beside=T, space=c(0,2), ylim=c(0,30))
>>> years<-c(2014,2015,2016,2017,2018) mtext(years, side=1, at=c(5, 12, 19, 26,
>>> 33))

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: bar plot add space to group data

plangfelder
In reply to this post by Rolf Turner
My guess is that space has no effect because (1) the first element is
zero and (2) the code in OP's message has
barplot(gceac[,3], ...

i.e. barplot does not see a matrix, only a vector.

To the OP, try formatting the data to be plotted as a matrix, not as a
vector, then the space argument should be useful to add space between
groups.

Peter


On Sat, Aug 18, 2018 at 4:53 PM Rolf Turner <[hidden email]> wrote:

>
>
> Jim:
>
> (a) There's no legend.
>
> (b) I am still curious as to why the OP's code didn't work, in that
> the "space=c(0,2)" argument seemed to have no effect.
>
> cheers,
>
> Rolf
>
> On 18/08/18 20:45, Jim Lemon wrote:
> > Hi citc,
> > Try this:
> >
> > geac<-matrix(c(9,9,8,8,8,23,23,23,23,22,27,27,27,25,24,
> >   19,19,19,20,20,17,17,17,18,19,8,8,8,9,9,2,2,3,3,3),ncol=5,byrow=TRUE)
> > library(plotrix)
> > barp(geac,names.arg=2014:2018,main="A level grades chemistry",
> >   xlab="Year",ylab="Percentage of each grade",ylim=c(0,30),
> >   col=c("white","lightblue","blue","orange","green","red","pink"))
> >
> > Jim
> >
> > On Fri, Aug 17, 2018 at 9:55 PM,  <[hidden email]> wrote:
> >> R-users,
> >>
> >> Can someone please advise how to improve the code below that was used to produce the graph shown at the following hyperlink (https://chemistryinthecity.neocities.org/content/entry1808.html#17)? The request is to add space between the annual data groups.
> >>
> >> barplot(gceac[,3], xlab='year', ylab='percentage of each grade', col=c('aliceblue', 'aquamarine', 'blue', 'chocolate', 'darkgreen', 'firebrick', 'violet'), legend=gceac[1:7,2], args.legend = list(x = 40, y = 30, title='grades'), main='A-level grades, chemistry', beside=T, space=c(0,2), ylim=c(0,30))
> >> years<-c(2014,2015,2016,2017,2018)
> >> mtext(years, side=1, at=c(5, 12, 19, 26, 33))
> >> R-users, Can someone please advise how to improve the code below that was used to produce the graph shown at the following hyperlink (https://chemistryinthecity.neocities.org/content/entry1808.html#17)? The request is to add space between the annual data groups.  barplot(gceac[,3], xlab='year', ylab='percentage of each grade', col=c('aliceblue', 'aquamarine', 'blue', 'chocolate', 'darkgreen', 'firebrick', 'violet'), legend=gceac[1:7,2], args.legend = list(x = 40, y = 30, title='grades'), main='A-level grades, chemistry', beside=T, space=c(0,2), ylim=c(0,30)) years<-c(2014,2015,2016,2017,2018) mtext(years, side=1, at=c(5, 12, 19, 26, 33))
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: bar plot add space to group data

Rolf Turner

On 19/08/18 16:58, Peter Langfelder wrote:

> My guess is that space has no effect because (1) the first element is
> zero and (2) the code in OP's message has
> barplot(gceac[,3], ...
>
> i.e. barplot does not see a matrix, only a vector.
>
> To the OP, try formatting the data to be plotted as a matrix, not as a
> vector, then the space argument should be useful to add space between
> groups.

Thanks Peter.  That would appear to be a sound analysis.  Thanks for the
insight.

cheers,

Rolf

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: bar plot add space to group data

Rolf Turner
In reply to this post by Jim Lemon-4
On 19/08/18 16:12, Jim Lemon wrote:
> Hi Rolf,
> That's what comes of being in a hurry.
>
> legend(4.1,30,c("A+","A","B","C","D","E","U"),
>   fill=c("white","lightblue","blue","orange","green","red","pink"))
>
> and I thank you for alerting me to the fact that the legend arguments
> in barp don't position the legend properly. I'll fix it.

And I wasn't even aware that I was doing any alerting! :-)

cheers,

Rolf

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: bar plot add space to group data

citc
In reply to this post by Rolf Turner
August 17, 2018 10:24 PM, "Rolf Turner" <[hidden email]> wrote:

> On 18/08/18 02:37, David L Carlson wrote:
>
>> Notice below that your message is substantially scrambled. R-Help is a
>> plain text only list so you should set your email client to produce plain text messages.

Apologies, forgot to change the default settings of the web-mail.

>
> If one struggles through the garbled html bumff, one sees that the OP *did* indeed use the "space="
> argument. However it does not appear to have had the desired effect, and I cannot see why. Since
> the OP did not supply the data, I cannot experiment.
>

csv data below:

year,grade,percentage
2014,A*,9
2014,A,23
2014,B,27
2014,C,19
2014,D,13
2014,E,7
2014,U,2
2015,A*,9
2015,A,23
2015,B,27
2015,C,19
2015,D,13
2015,E,7
2015,U,2
2016,A*,8
2016,A,23
2016,B,27
2016,C,19
2016,D,13
2016,E,7
2016,U,3
2017,A*,8
2017,A,23
2017,B,24
2017,C,20
2017,D,14
2017,E,8
2017,U,3
2018,A*,8
2018,A,22
2018,B,23
2018,C,20
2018,D,15
2018,E,8
2018,U,3

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: bar plot add space to group data

citc
In reply to this post by plangfelder
August 19, 2018 4:58 AM, "Peter Langfelder" <[hidden email]> 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?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: bar plot add space to group data

plangfelder
On Sun, Aug 19, 2018 at 7:15 AM <[hidden email]> wrote:
>
> August 19, 2018 4:58 AM, "Peter Langfelder" <[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: bar plot add space to group data

David Carlson
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:[hidden email]] On Behalf Of Peter Langfelder
Sent: Sunday, August 19, 2018 10:51 AM
To: [hidden email]
Cc: r-help <[hidden email]>
Subject: Re: [R] [FORGED] Re: bar plot add space to group data

On Sun, Aug 19, 2018 at 7:15 AM <[hidden email]> wrote:
>
> August 19, 2018 4:58 AM, "Peter Langfelder" <[hidden email]> 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

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.

BarPlot.png (11K) Download Attachment