ggplot2 boxplot from count table

╄→尐↘猪︶ㄣ 提交于 2019-12-09 22:34:48

问题


I have a count table that I have generated with another tool, and I would like to get a boxplot from it with ggplot2.

For instance, suppose that I have:

df1 = data.frame(nSiblings = c(0, 1, 2), count = c(10, 15, 12))

instead of

df2 = data.frame(nSiblings = c(rep(0, 10), rep(1, 15), rep(2, 12)))

I know how to produce a boxplot from the second data frame:

qplot(y=df2$nSiblings, x=1, geom = "boxplot")

I know how to produce a histogram from the first data frame:

ggplot(df1, aes(x = nSiblings, y = count)) + geom_bar(stat = "identity")

But how can I get a boxplot from the first data frame?


回答1:


Ggplot is able to work with weights, so you could try this:

ggplot(df1, aes(x=1,y=nSiblings,weights=count)) + geom_boxplot()


来源:https://stackoverflow.com/questions/34204175/ggplot2-boxplot-from-count-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!