[R] Histogram of character elements
Luigi Marongiu
m@rong|u@|u|g| @end|ng |rom gm@||@com
Thu Jun 7 13:49:11 CEST 2018
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 <btupper using bigelow.org> 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 <marongiu.luigi using gmail.com> 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 <btupper using bigelow.org> 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 <marongiu.luigi using gmail.com> 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
>
> ______________________________________________
> 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.
>
>
> 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
More information about the R-help
mailing list