Help on Histogram ~ Barplot

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Help on Histogram ~ Barplot

Shivi82
Hello All,
I need help on creating a histogram for one of my data. The data is as below (sample):
MFST_WT Hours PROCESS Month Weekday Day of the Month
6,828 13       INBOUND Mar             Fri 13
2,504 16       INBOUND Mar             Fri 27
20        16       INBOUND Mar             Fri 27
10,262 16       INBOUND Mar             Fri 27
2,500 17       INBOUND Mar             Fri 13
3,938 16       INBOUND Feb            Thu 26
798        10       INBOUND Feb            Sat 14
5,439 15       INBOUND Feb            Mon 16

This data has these columns and total rows are 45000.
Now I need to group the data and create a bar plot based on total weight on a monthly basis. The code I have used is:
barplot(mwlc$MFST_WT, names.arg = mwlc$Month, las=2, ylim=c(0,10000), col ="red",
        border="orange", main="Monthly Weight")
but this is showing an error. Please suggest.
Reply | Threaded
Open this post in threaded view
|

Re: Help on Histogram ~ Barplot

Jim Lemon-4
Hi Shivi82,
My suggestion is that the error concerned a mismatch between the
number of labels and the number of bars.

More seriously, you seem to want to sum the values of weight (MFST_WT)
within each month. So:

tot_mon_wt<-by(mwlc$MFST_WT,mwlc$Month,sum)
barplot(tot_mon_wt, names.arg = sort(unique(mwlc$Month)), las=2,
  ylim=c(0,25000),col="red",border="orange", main="Monthly Weight")

Jim


On Wed, May 27, 2015 at 7:28 PM, Shivi82 <[hidden email]> wrote:

> Hello All,
> I need help on creating a histogram for one of my data. The data is as below
> (sample):
> MFST_WT Hours   PROCESS Month   Weekday Day of the Month
> 6,828   13             INBOUND  Mar                  Fri        13
> 2,504   16             INBOUND  Mar                  Fri        27
> 20              16             INBOUND  Mar                  Fri        27
> 10,262  16             INBOUND  Mar                  Fri        27
> 2,500   17             INBOUND  Mar                  Fri        13
> 3,938   16             INBOUND  Feb                 Thu 26
> 798             10             INBOUND  Feb                 Sat 14
> 5,439   15             INBOUND  Feb                 Mon 16
>
> This data has these columns and total rows are 45000.
> Now I need to group the data and create a bar plot based on total weight on
> a monthly basis. The code I have used is:
> barplot(mwlc$MFST_WT, names.arg = mwlc$Month, las=2, ylim=c(0,10000), col
> ="red",
>         border="orange", main="Monthly Weight")
> but this is showing an error. Please suggest.
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Help-on-Histogram-Barplot-tp4707739.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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: Help on Histogram ~ Barplot

Shivi82
HI Jim,

Thanks for the help however R throws an error when i create a var tot_mon_wt-
tot_mon_wt<-by(mwlc$MFST_WT,mwlc$Month,sum). It gives me an error =
Error in Summary.factor(c(1L, 1L), na.rm = FALSE) :
  ‘sum’ not meaningful for factors

Not sure what this error refers to. Thank you, Shivi
Reply | Threaded
Open this post in threaded view
|

Re: Help on Histogram ~ Barplot

Michael Dewey-3


On 27/05/2015 11:56, Shivi82 wrote:
> HI Jim,
>
> Thanks for the help however R throws an error when i create a var
> tot_mon_wt-
> tot_mon_wt<-by(mwlc$MFST_WT,mwlc$Month,sum). It gives me an error =
> Error in Summary.factor(c(1L, 1L), na.rm = FALSE) :
>    ‘sum’ not meaningful for factors
>
> Not sure what this error refers to.

Dear Shivi
It means that you have tried to use sum on a variable which is a factor.
You can easily convert it to numeric but if it were me I would want to
find out how it got turned into a factor before I transformed it.

  Thank you, Shivi

>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Help-on-Histogram-Barplot-tp4707739p4707744.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.
>

--
Michael
http://www.dewey.myzen.co.uk/home.html

______________________________________________
[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: Help on Histogram ~ Barplot

David Winsemius
In reply to this post by Shivi82

On May 27, 2015, at 2:28 AM, Shivi82 wrote:

> Hello All,
> I need help on creating a histogram for one of my data. The data is as below
> (sample):
> MFST_WT Hours PROCESS Month Weekday Day of the Month
> 6,828 13       INBOUND Mar             Fri 13
> 2,504 16       INBOUND Mar             Fri 27
> 20        16       INBOUND Mar             Fri 27
> 10,262 16       INBOUND Mar             Fri 27
> 2,500 17       INBOUND Mar             Fri 13
> 3,938 16       INBOUND Feb            Thu 26
> 798        10       INBOUND Feb            Sat 14
> 5,439 15       INBOUND Feb            Mon 16
>

What I'm seeing here is the likelihood that the data object has been input incorrectly (or is a mess in the original file?) There appear to be commas in what would presumably be a numeric column and a column name with spaces in it. There is also misregistration of some lines of data possible with tab-characters that were eaten by Nabble's interface.  If you want help under these circumstances you should either respond with the first 20 lines of the original data file posted through a mail-client bypassing Nabble or post dput( head(objname, 20))


> This data has these columns and total rows are 45000.
> Now I need to group the data and create a bar plot based on total weight on
> a monthly basis. The code I have used is:
> barplot(mwlc$MFST_WT, names.arg = mwlc$Month, las=2, ylim=c(0,10000), col
> ="red",
>        border="orange", main="Monthly Weight")
> but this is showing an error. Please suggest.
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Help-on-Histogram-Barplot-tp4707739.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.

David Winsemius
Alameda, CA, USA

______________________________________________
[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: Help on Histogram ~ Barplot

Shivi82
HI David,

So if I understand from your post below, when we import a file in R- we need to make sure that the variable names do not have any space nor they should be in special characters or not in comma format.
Please correct me I am wrong.

Now I have changed the file to a new file as RData.csv file and now when I use the below code it gives me an error
graph<- read.csv("RData.csv", header = TRUE)
MonthlyWeight<-by(RData$Weight,names.arg = RData$Month,sum)

The error is : Error in by(RData$Weight, names.arg = RData$Month, sum) :
  object 'RData' not found. Whereas I checked using "getwd()" this file is saved in the working directory.
Please suggest.
Reply | Threaded
Open this post in threaded view
|

Re: Help on Histogram ~ Barplot

Sarah Goslee
Hello,


> Now I have changed the file to a new file as RData.csv file and now when I
> use the below code it gives me an error
> graph<- read.csv("RData.csv", header = TRUE)

Here you read RData.csv into R as a data frame named graph.

> MonthlyWeight<-by(RData$Weight,names.arg = RData$Month,sum)

So here you need to refer to that data frame using the name you gave it.

Your problem looks like a lack of basic understanding of how R works.
Here are a couple of sources that might help you get started:
http://www.burns-stat.com/documents/tutorials/impatient-r/
http://cyclismo.org/tutorial/R/

Sarah

--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[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: Help on Histogram ~ Barplot

kmwolf
In reply to this post by Shivi82
I don't know why that doesn't work, but try adding in
sep=","
In your read.csv()

That shouldn't matter as to whether or not it recognizes your object though, but it will matter in how your object is read.

Other simple things might be to make sure the file is saved and then close it out to make sure there is no open dialog box preventing the save from occurring. Then run the read.csv again and see if the object shows up in R.



~ Kristina

Kristina Wolf
PhD Candidate, Graduate Group in Ecology
M.S. Soil Science, B.S. Animal Science
KristinaMWolf.com
Restoration Ecology Lab
Department of Plant Sciences
University of California, Davis
(530) 750-9771

> On May 28, 2015, at 6:13 AM, Shivi82 <[hidden email]> wrote:
>
> HI David,
>
> So if I understand from your post below, when we import a file in R- we need
> to make sure that the variable names do not have any space nor they should
> be in special characters or not in comma format.
> Please correct me I am wrong.
>
> Now I have changed the file to a new file as RData.csv file and now when I
> use the below code it gives me an error
> graph<- read.csv("RData.csv", header = TRUE)
> MonthlyWeight<-by(RData$Weight,names.arg = RData$Month,sum)
>
> The error is : Error in by(RData$Weight, names.arg = RData$Month, sum) :
>  object 'RData' not found. Whereas I checked using "getwd()" this file is
> saved in the working directory.
> Please suggest.
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Help-on-Histogram-Barplot-tp4707739p4707839.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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: Help on Histogram ~ Barplot

David Winsemius
In reply to this post by Shivi82
Adding back context which was omitted by this Nabble user who still does not understand the mailing list conventions:

> On May 27, 2015, at 2:28 AM, Shivi82 wrote:
>
>> Hello All,
>> I need help on creating a histogram for one of my data. The data is as below
>> (sample):
>> MFST_WT Hours PROCESS Month Weekday Day of the Month
>> 6,828 13       INBOUND Mar             Fri 13
>> 2,504 16       INBOUND Mar             Fri 27
>> 20        16       INBOUND Mar             Fri 27
>> 10,262 16       INBOUND Mar             Fri 27
>> 2,500 17       INBOUND Mar             Fri 13
>> 3,938 16       INBOUND Feb            Thu 26
>> 798        10       INBOUND Feb            Sat 14
>> 5,439 15       INBOUND Feb            Mon 16
>>
> D.Winsemius wrote:
> What I'm seeing here is the likelihood that the data object has been input incorrectly (or is a mess in the original file?) There appear to be commas in what would presumably be a numeric column and a column name with spaces in it. There is also misregistration of some lines of data possible with tab-characters that were eaten by Nabble's interface.  If you want help under these circumstances you should either respond with the first 20 lines of the original data file posted through a mail-client bypassing Nabble or post dput( head(objname, 20))

On May 28, 2015, at 6:13 AM, Shivi82 wrote:

> HI David,
>
> So if I understand from your post below, when we import a file in R- we need
> to make sure that the variable names do not have any space nor they should
> be in special characters or not in comma format.

Well .... perhaps. I asked to see the first several lines of the data. If it really were a csv file, then there should not have been any commas in the first column when the R data-object was printed. Any spaces in the header row should have been converted to periods. That's what the `make.names` function does when it encounters spaces or special characters.

--
David.


> Please correct me I am wrong.
>
> Now I have changed the file to a new file as RData.csv file and now when I
> use the below code it gives me an error
> graph<- read.csv("RData.csv", header = TRUE)
> MonthlyWeight<-by(RData$Weight,names.arg = RData$Month,sum)
>
> The error is : Error in by(RData$Weight, names.arg = RData$Month, sum) :
>  object 'RData' not found. Whereas I checked using "getwd()" this file is
> saved in the working directory.
> Please suggest.
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Help-on-Histogram-Barplot-tp4707739p4707839.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.

David Winsemius
Alameda, CA, USA

______________________________________________
[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: Help on Histogram ~ Barplot

Bert Gunter-2
In reply to this post by kmwolf
Unless I have missed something somewhere, the object is named "graph" not
"RData" . Further, the call to by() is complete nonsense, with arguments
misspecified -- there is no "names.arg" argument to by() (the 2nd argument
is named "INDICES")

It appears that some people need to spend some time with R tutorials before
further posting (or replying) on this list.

Cheers,
Bert

graph<- read.csv("RData.csv", header = TRUE)

On Thu, May 28, 2015 at 7:08 AM, Kristina Wolf <[hidden email]> wrote:

> I don't know why that doesn't work, but try adding in
> sep=","
> In your read.csv()
>
> That shouldn't matter as to whether or not it recognizes your object
> though, but it will matter in how your object is read.
>
> Other simple things might be to make sure the file is saved and then close
> it out to make sure there is no open dialog box preventing the save from
> occurring. Then run the read.csv again and see if the object shows up in R.
>
>
>
> ~ Kristina
>
> Kristina Wolf
> PhD Candidate, Graduate Group in Ecology
> M.S. Soil Science, B.S. Animal Science
> KristinaMWolf.com
> Restoration Ecology Lab
> Department of Plant Sciences
> University of California, Davis
> (530) 750-9771
>
> > On May 28, 2015, at 6:13 AM, Shivi82 <[hidden email]> wrote:
> >
> > HI David,
> >
> > So if I understand from your post below, when we import a file in R- we
> need
> > to make sure that the variable names do not have any space nor they
> should
> > be in special characters or not in comma format.
> > Please correct me I am wrong.
> >
> > Now I have changed the file to a new file as RData.csv file and now when
> I
> > use the below code it gives me an error
> > graph<- read.csv("RData.csv", header = TRUE)
> > MonthlyWeight<-by(RData$Weight,names.arg = RData$Month,sum)
> >
> > The error is : Error in by(RData$Weight, names.arg = RData$Month, sum) :
> >  object 'RData' not found. Whereas I checked using "getwd()" this file is
> > saved in the working directory.
> > Please suggest.
> >
> >
> >
> > --
> > View this message in context:
> http://r.789695.n4.nabble.com/Help-on-Histogram-Barplot-tp4707739p4707839.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > [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.
>

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