r - Generating a histogram and density plot from binned data -
i've binned data , have dataframe consists of 2 columns, 1 specifies bin range , specifies frequency this:-
> head(data) binrange frequency 1 (0,0.025] 88 2 (0.025,0.05] 72 3 (0.05,0.075] 92 4 (0.075,0.1] 38 5 (0.1,0.125] 20 6 (0.125,0.15] 16
i want plot histogram , density plot using can't seem find way of doing without having generate new bins etc. using solution here tried following:-
p <- ggplot(data, aes(x= binrange, y=frequency)) + geom_histogram(stat="identity")
but crashes. know of how deal this?
thank you
the problem ggplot doesnt understand data way input it, need reshape (i not regex-master, surely there better ways is):
df <- read.table(header = true, text = " binrange frequency 1 (0,0.025] 88 2 (0.025,0.05] 72 3 (0.05,0.075] 92 4 (0.075,0.1] 38 5 (0.1,0.125] 20 6 (0.125,0.15] 16") library(stringr) library(splitstackshape) library(ggplot2) # extract numbers out, df$binrange <- str_extract(df$binrange, "[0-9].*[0-9]+") # split data using , columns: # 1 start-point , 1 end-point df <- csplit(df, "binrange") # plot it, dont need second column ggplot(df, aes(x = binrange_1, y = frequency, width = 0.025)) + geom_bar(stat = "identity", breaks=seq(0,0.125, by=0.025))
or if don't want data interpreted numerically, can following:
df <- read.table(header = true, text = " binrange frequency 1 (0,0.025] 88 2 (0.025,0.05] 72 3 (0.05,0.075] 92 4 (0.075,0.1] 38 5 (0.1,0.125] 20 6 (0.125,0.15] 16") library(ggplot2) ggplot(df, aes(x = binrange, y = frequency)) + geom_bar(stat = "identity")
you won't able plot density-plot data, given not continous rather categorical, thats why prefer second way of showing it,
Comments
Post a Comment