Bar Charts

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

Bar Charts

reichmaj
r-help forum

 

I have a database that I have performed a "group_by" of a variable called
CONTBR_OCCUPATION. I then simply want to plot out just the top 15 results as
a bar plot. How do I plot only the top 15 groups on the x -axis. Should I
just extract the top 15 records and plot them or is the a better way?

 

occup <- myDat %>%

  group_by(CONTBR_OCCUPATION) %>%

  summarize(count = n()) %>%

  arrange(desc(count))

 

Jeff

 


        [[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 Charts

Jim Lemon-4
Hi Jeff,
Let's say you have the following data:

set.seed(12345)
CONTBR_RESULT<-sample(20:200,30)

If you don't mind ordering the results, you can do this:

barplot(rev(sort(CONTBR_RESULT))[1:15],...)

If you want the values in the original order:

barplot(CONTBR_RESULT[order(CONTBR_RESULT) > 15],...)

Jim

On Wed, Oct 16, 2019 at 11:21 AM <[hidden email]> wrote:

>
> r-help forum
>
>
>
> I have a database that I have performed a "group_by" of a variable called
> CONTBR_OCCUPATION. I then simply want to plot out just the top 15 results as
> a bar plot. How do I plot only the top 15 groups on the x -axis. Should I
> just extract the top 15 records and plot them or is the a better way?
>
>
>
> occup <- myDat %>%
>
>   group_by(CONTBR_OCCUPATION) %>%
>
>   summarize(count = n()) %>%
>
>   arrange(desc(count))
>
>
>
> Jeff
>
>
>
>
>         [[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 Charts

Rui Barradas
Hello,

Inline.

Às 02:51 de 16/10/19, Jim Lemon escreveu:

> Hi Jeff,
> Let's say you have the following data:
>
> set.seed(12345)
> CONTBR_RESULT<-sample(20:200,30)
>
> If you don't mind ordering the results, you can do this:
>
> barplot(rev(sort(CONTBR_RESULT))[1:15],...)
>
> If you want the values in the original order:
>
> barplot(CONTBR_RESULT[order(CONTBR_RESULT) > 15],...)

This is not right, it keeps the 15 largest *indices*, not values.

Here is a way.


TopN <- function(x, n, keep.order = FALSE){
   nvec <- seq_len(n)
   if(keep.order){
     i <- order(x, decreasing = TRUE)[nvec]
     x[i[order(i)]]
   }else{
     sort(x, decreasing = TRUE)[nvec]
   }
}

barplot(TopN(CONTBR_RESULT, 15))
barplot(TopN(CONTBR_RESULT, 15, TRUE))


Hope this helps,

Rui Barradas

>
> Jim
>
> On Wed, Oct 16, 2019 at 11:21 AM <[hidden email]> wrote:
>>
>> r-help forum
>>
>>
>>
>> I have a database that I have performed a "group_by" of a variable called
>> CONTBR_OCCUPATION. I then simply want to plot out just the top 15 results as
>> a bar plot. How do I plot only the top 15 groups on the x -axis. Should I
>> just extract the top 15 records and plot them or is the a better way?
>>
>>
>>
>> occup <- myDat %>%
>>
>>    group_by(CONTBR_OCCUPATION) %>%
>>
>>    summarize(count = n()) %>%
>>
>>    arrange(desc(count))
>>
>>
>>
>> Jeff
>>
>>
>>
>>
>>          [[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.
>

______________________________________________
[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 Charts

Jorge Mendonça
In reply to this post by Jim Lemon-4
It`s like this?

barplot(CONTBR_RESULT[order(CONTBR_RESULT)][16:30])

Regards
Jorge

On Wed, 16 Oct 2019 at 02:51, Jim Lemon <[hidden email]> wrote:

> Hi Jeff,
> Let's say you have the following data:
>
> set.seed(12345)
> CONTBR_RESULT<-sample(20:200,30)
>
> If you don't mind ordering the results, you can do this:
>
> barplot(rev(sort(CONTBR_RESULT))[1:15],...)
>
> If you want the values in the original order:
>
> barplot(CONTBR_RESULT[order(CONTBR_RESULT) > 15],...)
>
> Jim
>
> On Wed, Oct 16, 2019 at 11:21 AM <[hidden email]> wrote:
> >
> > r-help forum
> >
> >
> >
> > I have a database that I have performed a "group_by" of a variable called
> > CONTBR_OCCUPATION. I then simply want to plot out just the top 15
> results as
> > a bar plot. How do I plot only the top 15 groups on the x -axis. Should I
> > just extract the top 15 records and plot them or is the a better way?
> >
> >
> >
> > occup <- myDat %>%
> >
> >   group_by(CONTBR_OCCUPATION) %>%
> >
> >   summarize(count = n()) %>%
> >
> >   arrange(desc(count))
> >
> >
> >
> > Jeff
> >
> >
> >
> >
> >         [[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.
>


--
Jorge Mendonça
Departamento de Matemática
Instituto Superior de Engenharia do Porto
Portugal

        [[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 Charts

Jim Lemon-4
Hi Rui & Jorge,
You're both right, thanks for the correction. I should have done:

CONTBR_RESULT[CONTBR_RESULT > quantile(CONTBR_RESULT,probs=0.5)]
[1] 161 171 112 115 105 195 122 113 179 190 156 160 167 125 138

Jim

On Wed, Oct 16, 2019 at 6:55 PM Jorge Mendonça <[hidden email]> wrote:

>
> It`s like this?
>
> barplot(CONTBR_RESULT[order(CONTBR_RESULT)][16:30])
>
> Regards
> Jorge
>
> On Wed, 16 Oct 2019 at 02:51, Jim Lemon <[hidden email]> wrote:
>>
>> Hi Jeff,
>> Let's say you have the following data:
>>
>> set.seed(12345)
>> CONTBR_RESULT<-sample(20:200,30)
>>
>> If you don't mind ordering the results, you can do this:
>>
>> barplot(rev(sort(CONTBR_RESULT))[1:15],...)
>>
>> If you want the values in the original order:
>>
>> barplot(CONTBR_RESULT[order(CONTBR_RESULT) > 15],...)
>>
>> Jim
>>
>> On Wed, Oct 16, 2019 at 11:21 AM <[hidden email]> wrote:
>> >
>> > r-help forum
>> >
>> >
>> >
>> > I have a database that I have performed a "group_by" of a variable called
>> > CONTBR_OCCUPATION. I then simply want to plot out just the top 15 results as
>> > a bar plot. How do I plot only the top 15 groups on the x -axis. Should I
>> > just extract the top 15 records and plot them or is the a better way?
>> >
>> >
>> >
>> > occup <- myDat %>%
>> >
>> >   group_by(CONTBR_OCCUPATION) %>%
>> >
>> >   summarize(count = n()) %>%
>> >
>> >   arrange(desc(count))
>> >
>> >
>> >
>> > Jeff
>> >
>> >
>> >
>> >
>> >         [[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.
>
>
>
> --
> Jorge Mendonça
> Departamento de Matemática
> Instituto Superior de Engenharia do Porto
> Portugal

______________________________________________
[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 Charts

reichmaj
In reply to this post by Jim Lemon-4
Jim

That’s certainly much more straight forward.

Jeff

-----Original Message-----
From: Jim Lemon <[hidden email]>
Sent: Tuesday, October 15, 2019 8:51 PM
To: Jeff Reichman <[hidden email]>
Cc: r-help mailing list <[hidden email]>
Subject: Re: [R] Bar Charts

Hi Jeff,
Let's say you have the following data:

set.seed(12345)
CONTBR_RESULT<-sample(20:200,30)

If you don't mind ordering the results, you can do this:

barplot(rev(sort(CONTBR_RESULT))[1:15],...)

If you want the values in the original order:

barplot(CONTBR_RESULT[order(CONTBR_RESULT) > 15],...)

Jim

On Wed, Oct 16, 2019 at 11:21 AM <[hidden email]> wrote:

>
> r-help forum
>
>
>
> I have a database that I have performed a "group_by" of a variable
> called CONTBR_OCCUPATION. I then simply want to plot out just the top
> 15 results as a bar plot. How do I plot only the top 15 groups on the
> x -axis. Should I just extract the top 15 records and plot them or is the a better way?
>
>
>
> occup <- myDat %>%
>
>   group_by(CONTBR_OCCUPATION) %>%
>
>   summarize(count = n()) %>%
>
>   arrange(desc(count))
>
>
>
> Jeff
>
>
>
>
>         [[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.