ggplot2 - R Histogram With Density Curve and Translucent Area Curve -


i using program found made histogram shown in attached picture. trying make identical 1 different data set. however, keep getting strange results. data represents how many mvp winners there each age category. note have scroll view more code.

any figuring out issue appreciated.

    df <- read.table(textconnection(     'age    count       20    2       21    0       22    2       23    3       24    6       25    6       26    9       27    11       28    7       29    8       30    6       31    3       32    2       33    2       34    1       35    0       36    2       37    2       38    1       39    1'), header = true)       library(ggplot2)       ggplot(df,aes(x=age)) +       geom_histogram()+       labs(x="age",            y="count",            title="age vs mlb mvp count")       ggplot(df,aes(x=age)) +       geom_histogram(binwidth=2,         fill="cornsilk",color="black")+         labs(x="age",            y="count",            title="age vs mlb mvp count")       ggplot(df,aes(x=age,..density..)) +       geom_histogram(binwidth=2,         fill="cornsilk",color="black")+         labs(x="age",            y="count",            title="age vs mlb mvp count")        histplot <- ggplot(df,aes(x=age,..density..))+        geom_histogram(binwidth=2, fill="cornsilk",color="black")+         labs(x="age",            y="count",            title="age vs mlb mvp count")       histplot       histplot + geom_freqpoly(binwidth=2,color="red",size=1.2)       histplot + geom_line(stat="density",color="blue",size=1.2)+       xlim(20,39)       histplot +       geom_density(adjust=.4,fill="cyan",color="black",alpha=.40)+       xlim(20,39) 

image

ggplot's histogram feature typically not meant summarized data. crude way make example work nicely recreate non-summarized data:

df = data.frame(age=rep(df$age, df$count)) 

then graphs go through. in practice created summaries somehow skip summarizing , feed raw data ggplot.


Comments