r - ggplot side by side geom_bar() -


i want create side side barplot using geom_bar() of data frame,

> dfp1    value   percent1   percent 1 (18,29] 0.20909091 0.4545455 2 (29,40] 0.23478261 0.5431034 3 (40,51] 0.15492958 0.3661972 4 (51,62] 0.10119048 0.1726190 5 (62,95] 0.05660377 0.1194969 

with values on x-axis , percents side side barplots. have tried using code,

p = ggplot(dfp1, aes(x = value, y= c(percent, percent1)), xlab="age group") p = p + geom_bar(stat="identity", width=.5)   

however, error: error: aesthetics must either length one, or same length dataproblems:value. percent , percent1 same length value, confused. help.

you need melt data first on value. create variable called value default, need renames (i called percent). then, plot new data set using fill in order separate data groups, , position = "dodge" in order put bars side side (instead of on top of each other)

library(reshape2) library(ggplot2) dfp1 <- melt(dfp1) names(dfp1)[3] <- "percent" ggplot(dfp1, aes(x = value, y= percent, fill = variable), xlab="age group") + geom_bar(stat="identity", width=.5, position = "dodge")   

enter image description here


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -