[R] Histogram of character elements

Ben Tupper btupper @end|ng |rom b|ge|ow@org
Thu Jun 7 12:47:59 CEST 2018


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/






	[[alternative HTML version deleted]]




More information about the R-help mailing list