条形图上的离散频率多边形 [英] Discrete frequency polygon over a bar plot

查看:49
本文介绍了条形图上的离散频率多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用ggplot2在条形图上绘制一个频率多边形.我使用下面的命令

I want to draw a frequency polygon over a bar plot with ggplot2. I use the commands below

library(ggplot2)
g <- ggplot(diamonds) + geom_bar(aes(cut))
g + geom_freqpoly(aes(as.numeric(cut)),binwidth=1)

,但是多边形顶点不在条形的中心.我尝试了不同的binwidth,但没有成功.

but the polygon vertices are not on the center of the bars. I tried different binwidth without success.

推荐答案

您应从 aes()中删除 as.numeric(),然后添加 group= 1 在aes()内以确保通过线连接点.

You should remove as.numeric() from aes() and then add group=1 inside aes() to ensure that points are connected by line.

ggplot(diamonds) + geom_bar(aes(cut))+
          geom_freqpoly(aes(cut,group=1))

或者简单地

ggplot(diamonds,aes(cut,group=1)) + geom_bar()+
          geom_freqpoly()

这篇关于条形图上的离散频率多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆