Histogram of character elements

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

Histogram of character elements

Luigi
Dear all,
I have a dataframe with a column representing the names of the
elements (a, b, etc) and one with their frequencies.
How can I plot the frequencies so that each element has an associated
frequency value?
I have been thinking of a histogram, but I have found it difficult to
implement. I have tried the following:

group <- c("a", "b", "c", "d", "e")
freq <-c(1, 2, 2, 5, 3)
df <- data.frame(group, freq, stringsAsFactors = FALSE)
hist(df$freq)
library(lattice)
histogram( ~ df$group)
histogram( ~ as.factor(df$group))
histogram(df$freq ~ as.factor(df$group))

hist(df$freq) returns a histogram in which the values 1 and 2 appear 3
times, the values 3 and 5 appear once and 4 never. This is not what I
wanted; I want instead a graph telling me that a appears once, b twice
etc.

histogram( ~ df$group) gives the error:
Error in hist.default(as.numeric(x), breaks = breaks, plot = FALSE,
include.lowest = include.lowest,  :
  negative length vectors are not allowed

histogram( ~ as.factor(df$group)) and histogram(df$freq ~
as.factor(df$group)) report all groups on the x axis (that is good)
but all at 20% level.

What am I missing?
Thank you.

--
Best regards,
Luigi

______________________________________________
[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: Histogram of character elements

Ben Tupper-2
Hi,

Is this what you are after?

group <- c("a", "b", "c", "d", "e")
freq <-c(1, 2, 2, 5, 3)
x = rep(group, freq)
barplot(table(x))

Cheers,
Ben



> On Jun 7, 2018, at 6:00 AM, Luigi Marongiu <[hidden email]> wrote:
>
> Dear all,
> I have a dataframe with a column representing the names of the
> elements (a, b, etc) and one with their frequencies.
> How can I plot the frequencies so that each element has an associated
> frequency value?
> I have been thinking of a histogram, but I have found it difficult to
> implement. I have tried the following:
>
> group <- c("a", "b", "c", "d", "e")
> freq <-c(1, 2, 2, 5, 3)
> df <- data.frame(group, freq, stringsAsFactors = FALSE)
> hist(df$freq)
> library(lattice)
> histogram( ~ df$group)
> histogram( ~ as.factor(df$group))
> histogram(df$freq ~ as.factor(df$group))
>
> hist(df$freq) returns a histogram in which the values 1 and 2 appear 3
> times, the values 3 and 5 appear once and 4 never. This is not what I
> wanted; I want instead a graph telling me that a appears once, b twice
> etc.
>
> histogram( ~ df$group) gives the error:
> Error in hist.default(as.numeric(x), breaks = breaks, plot = FALSE,
> include.lowest = include.lowest,  :
>  negative length vectors are not allowed
>
> histogram( ~ as.factor(df$group)) and histogram(df$freq ~
> as.factor(df$group)) report all groups on the x axis (that is good)
> but all at 20% level.
>
> What am I missing?
> Thank you.
>
> --
> Best regards,
> Luigi
>
> ______________________________________________
> [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.
>

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

Ecological Forecasting: https://eco.bigelow.org/






        [[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: Histogram of character elements

Luigi
exactly! Thank you!
but it is possible to do it with lattice? I might have an extra level
of information, for instance super-group, and in that case, I could
plot all the supergroup easily together.
On Thu, Jun 7, 2018 at 12:48 PM Ben Tupper <[hidden email]> wrote:

>
> Hi,
>
> Is this what you are after?
>
> group <- c("a", "b", "c", "d", "e")
> freq <-c(1, 2, 2, 5, 3)
> x = rep(group, freq)
> barplot(table(x))
>
> Cheers,
> Ben
>
>
>
> On Jun 7, 2018, at 6:00 AM, Luigi Marongiu <[hidden email]> wrote:
>
> Dear all,
> I have a dataframe with a column representing the names of the
> elements (a, b, etc) and one with their frequencies.
> How can I plot the frequencies so that each element has an associated
> frequency value?
> I have been thinking of a histogram, but I have found it difficult to
> implement. I have tried the following:
>
> group <- c("a", "b", "c", "d", "e")
> freq <-c(1, 2, 2, 5, 3)
> df <- data.frame(group, freq, stringsAsFactors = FALSE)
> hist(df$freq)
> library(lattice)
> histogram( ~ df$group)
> histogram( ~ as.factor(df$group))
> histogram(df$freq ~ as.factor(df$group))
>
> hist(df$freq) returns a histogram in which the values 1 and 2 appear 3
> times, the values 3 and 5 appear once and 4 never. This is not what I
> wanted; I want instead a graph telling me that a appears once, b twice
> etc.
>
> histogram( ~ df$group) gives the error:
> Error in hist.default(as.numeric(x), breaks = breaks, plot = FALSE,
> include.lowest = include.lowest,  :
>  negative length vectors are not allowed
>
> histogram( ~ as.factor(df$group)) and histogram(df$freq ~
> as.factor(df$group)) report all groups on the x axis (that is good)
> but all at 20% level.
>
> What am I missing?
> Thank you.
>
> --
> Best regards,
> Luigi
>
> ______________________________________________
> [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.
>
>
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 60 Bigelow Drive, P.O. Box 380
> East Boothbay, Maine 04544
> http://www.bigelow.org
>
> Ecological Forecasting: https://eco.bigelow.org/
>
>
>
>
>


--
Best regards,
Luigi

______________________________________________
[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: Histogram of character elements

Luigi
In reply to this post by Ben Tupper-2
also, with this approach, I need to re-arrange the data. Is it
possible to work directly on a dataframe?
On Thu, Jun 7, 2018 at 12:48 PM Ben Tupper <[hidden email]> wrote:

>
> Hi,
>
> Is this what you are after?
>
> group <- c("a", "b", "c", "d", "e")
> freq <-c(1, 2, 2, 5, 3)
> x = rep(group, freq)
> barplot(table(x))
>
> Cheers,
> Ben
>
>
>
> On Jun 7, 2018, at 6:00 AM, Luigi Marongiu <[hidden email]> wrote:
>
> Dear all,
> I have a dataframe with a column representing the names of the
> elements (a, b, etc) and one with their frequencies.
> How can I plot the frequencies so that each element has an associated
> frequency value?
> I have been thinking of a histogram, but I have found it difficult to
> implement. I have tried the following:
>
> group <- c("a", "b", "c", "d", "e")
> freq <-c(1, 2, 2, 5, 3)
> df <- data.frame(group, freq, stringsAsFactors = FALSE)
> hist(df$freq)
> library(lattice)
> histogram( ~ df$group)
> histogram( ~ as.factor(df$group))
> histogram(df$freq ~ as.factor(df$group))
>
> hist(df$freq) returns a histogram in which the values 1 and 2 appear 3
> times, the values 3 and 5 appear once and 4 never. This is not what I
> wanted; I want instead a graph telling me that a appears once, b twice
> etc.
>
> histogram( ~ df$group) gives the error:
> Error in hist.default(as.numeric(x), breaks = breaks, plot = FALSE,
> include.lowest = include.lowest,  :
>  negative length vectors are not allowed
>
> histogram( ~ as.factor(df$group)) and histogram(df$freq ~
> as.factor(df$group)) report all groups on the x axis (that is good)
> but all at 20% level.
>
> What am I missing?
> Thank you.
>
> --
> Best regards,
> Luigi
>
> ______________________________________________
> [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.
>
>
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 60 Bigelow Drive, P.O. Box 380
> East Boothbay, Maine 04544
> http://www.bigelow.org
>
> Ecological Forecasting: https://eco.bigelow.org/
>
>
>
>
>


--
Best regards,
Luigi

______________________________________________
[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: Histogram of character elements

Ben Tupper-2
Hi again,

I'm sort of pre-coffee still, but does this do it?  The data frame only has one variable, a factor where the order of the levels is specified.

library(lattice)
group   <- c("a", "b", "c", "d", "e")
freq    <- c(1, 2, 2, 5, 3)
x       <- rep(group, freq)
df      <- data.frame(group = factor(x, levels = c("d", "a", "b", "c", "e")) )
histogram(~ group, data = df)

As far as super-grouping the answer is likely yes, but without details and an example (and coffee) I'm at a loss.   I suggest getting a your hands on a copy of https://www.r-project.org/doc/bib/R-books_bib.html#R:Sarkar:2008 <https://www.r-project.org/doc/bib/R-books_bib.html#R:Sarkar:2008> It's really worth it if you plan to spend time with lattice.

Cheers,
Ben


> On Jun 7, 2018, at 7:02 AM, Luigi Marongiu <[hidden email]> wrote:
>
> also, with this approach, I need to re-arrange the data. Is it
> possible to work directly on a dataframe?
> On Thu, Jun 7, 2018 at 12:48 PM Ben Tupper <[hidden email]> wrote:
>>
>> Hi,
>>
>> Is this what you are after?
>>
>> group <- c("a", "b", "c", "d", "e")
>> freq <-c(1, 2, 2, 5, 3)
>> x = rep(group, freq)
>> barplot(table(x))
>>
>> Cheers,
>> Ben
>>
>>
>>
>> On Jun 7, 2018, at 6:00 AM, Luigi Marongiu <[hidden email]> wrote:
>>
>> Dear all,
>> I have a dataframe with a column representing the names of the
>> elements (a, b, etc) and one with their frequencies.
>> How can I plot the frequencies so that each element has an associated
>> frequency value?
>> I have been thinking of a histogram, but I have found it difficult to
>> implement. I have tried the following:
>>
>> group <- c("a", "b", "c", "d", "e")
>> freq <-c(1, 2, 2, 5, 3)
>> df <- data.frame(group, freq, stringsAsFactors = FALSE)
>> hist(df$freq)
>> library(lattice)
>> histogram( ~ df$group)
>> histogram( ~ as.factor(df$group))
>> histogram(df$freq ~ as.factor(df$group))
>>
>> hist(df$freq) returns a histogram in which the values 1 and 2 appear 3
>> times, the values 3 and 5 appear once and 4 never. This is not what I
>> wanted; I want instead a graph telling me that a appears once, b twice
>> etc.
>>
>> histogram( ~ df$group) gives the error:
>> Error in hist.default(as.numeric(x), breaks = breaks, plot = FALSE,
>> include.lowest = include.lowest,  :
>> negative length vectors are not allowed
>>
>> histogram( ~ as.factor(df$group)) and histogram(df$freq ~
>> as.factor(df$group)) report all groups on the x axis (that is good)
>> but all at 20% level.
>>
>> What am I missing?
>> Thank you.
>>
>> --
>> Best regards,
>> Luigi
>>
>> ______________________________________________
>> [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.
>>
>>
>> Ben Tupper
>> Bigelow Laboratory for Ocean Sciences
>> 60 Bigelow Drive, P.O. Box 380
>> East Boothbay, Maine 04544
>> http://www.bigelow.org
>>
>> Ecological Forecasting: https://eco.bigelow.org/
>>
>>
>>
>>
>>
>
>
> --
> Best regards,
> Luigi
>

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

Ecological Forecasting: https://eco.bigelow.org/






        [[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: Histogram of character elements

Luigi
Thank you Ben, this also works! I have a copy of the Sarkar but,
usually, I don't work with histograms. I'll brush it up, then. Best
regards, Luigi

On Thu, Jun 7, 2018 at 1:43 PM Ben Tupper <[hidden email]> wrote:

>
> Hi again,
>
> I'm sort of pre-coffee still, but does this do it?  The data frame only has one variable, a factor where the order of the levels is specified.
>
> library(lattice)
> group   <- c("a", "b", "c", "d", "e")
> freq    <- c(1, 2, 2, 5, 3)
> x       <- rep(group, freq)
> df      <- data.frame(group = factor(x, levels = c("d", "a", "b", "c", "e")) )
> histogram(~ group, data = df)
>
> As far as super-grouping the answer is likely yes, but without details and an example (and coffee) I'm at a loss.   I suggest getting a your hands on a copy of https://www.r-project.org/doc/bib/R-books_bib.html#R:Sarkar:2008 It's really worth it if you plan to spend time with lattice.
>
> Cheers,
> Ben
>
>
> On Jun 7, 2018, at 7:02 AM, Luigi Marongiu <[hidden email]> wrote:
>
> also, with this approach, I need to re-arrange the data. Is it
> possible to work directly on a dataframe?
> On Thu, Jun 7, 2018 at 12:48 PM Ben Tupper <[hidden email]> wrote:
>
>
> Hi,
>
> Is this what you are after?
>
> group <- c("a", "b", "c", "d", "e")
> freq <-c(1, 2, 2, 5, 3)
> x = rep(group, freq)
> barplot(table(x))
>
> Cheers,
> Ben
>
>
>
> On Jun 7, 2018, at 6:00 AM, Luigi Marongiu <[hidden email]> wrote:
>
> Dear all,
> I have a dataframe with a column representing the names of the
> elements (a, b, etc) and one with their frequencies.
> How can I plot the frequencies so that each element has an associated
> frequency value?
> I have been thinking of a histogram, but I have found it difficult to
> implement. I have tried the following:
>
> group <- c("a", "b", "c", "d", "e")
> freq <-c(1, 2, 2, 5, 3)
> df <- data.frame(group, freq, stringsAsFactors = FALSE)
> hist(df$freq)
> library(lattice)
> histogram( ~ df$group)
> histogram( ~ as.factor(df$group))
> histogram(df$freq ~ as.factor(df$group))
>
> hist(df$freq) returns a histogram in which the values 1 and 2 appear 3
> times, the values 3 and 5 appear once and 4 never. This is not what I
> wanted; I want instead a graph telling me that a appears once, b twice
> etc.
>
> histogram( ~ df$group) gives the error:
> Error in hist.default(as.numeric(x), breaks = breaks, plot = FALSE,
> include.lowest = include.lowest,  :
> negative length vectors are not allowed
>
> histogram( ~ as.factor(df$group)) and histogram(df$freq ~
> as.factor(df$group)) report all groups on the x axis (that is good)
> but all at 20% level.
>
> What am I missing?
> Thank you.
>
> --
> Best regards,
> Luigi
>
> ______________________________________________
> [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.
>
>
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 60 Bigelow Drive, P.O. Box 380
> East Boothbay, Maine 04544
> http://www.bigelow.org
>
> Ecological Forecasting: https://eco.bigelow.org/
>
>
>
>
>
>
>
> --
> Best regards,
> Luigi
>
>
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 60 Bigelow Drive, P.O. Box 380
> East Boothbay, Maine 04544
> http://www.bigelow.org
>
> Ecological Forecasting: https://eco.bigelow.org/
>
>
>
>
>


--
Best regards,
Luigi

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