R中的ggplot或qplot直方图

时间:2022-05-08 14:57:35

I know this is pretty much a basic question, but I have some trouble in drawing histograms from a single vector containing these numbers (dat):

我知道这是一个基本的问题,但我在从一个包含这些数字的向量中绘制直方图时遇到了一些麻烦(dat):

30.90 31.00 32.75 32.65 32.50 31.60 31.80 30.70 31.20 28.10 29.50 28.60 31.70 33.10

30.90 31.00 32.75 32.65 32.50 31.60 30.80 31.70 28.10 29.50 28.60 31.70

The qplot is straight forward:

qplot是直接向前的:

qplot(PorData,  binwidth=1.0, geo="histogram", xlab="Data", ylab="Frequency") 

This gives me a default histogram: R中的ggplot或qplot直方图

这给了我一个默认的直方图:

I would love to do a bit more aesthetically pleasing histogram, which would also contain a density curve showing the skewness of the data and to change the bin colors with a black outline, somewhat like this one: R中的ggplot或qplot直方图

我想做一个更美观的直方图,它也包含一个密度曲线,显示数据的偏斜度,并且用黑色的轮廓来改变bin的颜色,有点像这个:

Is it better to use the qplot function or the ggplot? Thanks in advance!

使用qplot函数还是使用ggplot?提前谢谢!

1 个解决方案

#1


3  

Here's an approach to create a histogram together with a density curve in ggplot2.

这里有一种方法可以创建一个直方图,并在ggplot2中创建一个密度曲线。

The data:

数据:

dat <- scan(textConnection("30.90 31.00 32.75 32.65 32.50 31.60 31.80 30.70 31.20 28.10 29.50 28.60 31.70 33.10"))

The plot:

场景:

library(ggplot2)
qplot(dat,  binwidth = 1.0, geom = "histogram", xlab = "Data", ylab = "Frequency",
      y = ..density.., fill = I("white"), colour = I("black")) +
  stat_density(geom = "line")

Here, y = ..density.. is used to use relative frequencies on the y axis.

在这里,y = . .密度. .用于在y轴上使用相对频率。

R中的ggplot或qplot直方图

#1


3  

Here's an approach to create a histogram together with a density curve in ggplot2.

这里有一种方法可以创建一个直方图,并在ggplot2中创建一个密度曲线。

The data:

数据:

dat <- scan(textConnection("30.90 31.00 32.75 32.65 32.50 31.60 31.80 30.70 31.20 28.10 29.50 28.60 31.70 33.10"))

The plot:

场景:

library(ggplot2)
qplot(dat,  binwidth = 1.0, geom = "histogram", xlab = "Data", ylab = "Frequency",
      y = ..density.., fill = I("white"), colour = I("black")) +
  stat_density(geom = "line")

Here, y = ..density.. is used to use relative frequencies on the y axis.

在这里,y = . .密度. .用于在y轴上使用相对频率。

R中的ggplot或qplot直方图