[R] Histogram of character elements

Luigi Marongiu m@rongiu@luigi @ending from gm@il@com
Thu Jun 7 12:00:57 CEST 2018


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



More information about the R-help mailing list