R 使用ggplot2缩放直方图中的POSIXlt数据

R 使用ggplot2缩放直方图中的POSIXlt数据,r,ggplot2,histogram,R,Ggplot2,Histogram,如何更改表格“2013-07-01 00:30:00”中x轴处的标签 但是在上面的曲线图上,这样曲线就符合..计数。。属性。您正在将数字x值传递给ggplot。您应该传递日期时间值并使用scale\u x\u datetime: times <- sample(seq(a, b, by = 2), 100) library(scales) ggplot(, aes(x = times)) + geom_histogram(aes(y= ..count.. ), binwidth =

如何更改表格“2013-07-01 00:30:00”中x轴处的标签


但是在上面的曲线图上,这样曲线就符合..计数。。属性。

您正在将数字x值传递给
ggplot
。您应该传递日期时间值并使用
scale\u x\u datetime

times <- sample(seq(a, b, by = 2), 100)

library(scales)
ggplot(, aes(x = times)) + 
  geom_histogram(aes(y= ..count.. ), binwidth = 10000, colour = "black") +
  theme(axis.text.x = element_text(angle = 45)) +
  scale_x_datetime(labels = date_format("%Y-%m-%d %H:%M:%S"))

这是令人困惑的,因为您在
geom\u直方图中明确指定了
y=…count..
(尽管它是默认值)。

许多thx,如果我想要更多标签,我也可以使用
scale\u x\u datetime
。关于密度函数,我正在寻找一条曲线来拟合这个直方图,就像geom_density拟合geom_直方图(…density…)。我编辑过,但怀疑我没有理解你的意思。由于密度函数是连续的,直方图是离散的,因此不能有相同大小的计数值。因此,除了我的答案中所示的缩放密度值之外,我不清楚您想要得到什么。我以前也尝试过,但我不知道如何将其与“ggplot(,aes(x=次))+geom_直方图(aes(fill=…density..),binwidth=20000,color=“black”)+scale_fill_gradient(“Count”,low=“green”,high=“red”)+主题相结合(axis.text.x=element_text(angle=45))+scale_x_datetime(labels=date_格式(“%Y-%m-%d%H:%m:%S”)+geom_density(alpha=.2,fill=“#FF6666”)`FYI使用
POSIXct
而不是
POSIXlt
 geom_density(alpha=.2, fill="#FF6666")
times <- sample(seq(a, b, by = 2), 100)

library(scales)
ggplot(, aes(x = times)) + 
  geom_histogram(aes(y= ..count.. ), binwidth = 10000, colour = "black") +
  theme(axis.text.x = element_text(angle = 45)) +
  scale_x_datetime(labels = date_format("%Y-%m-%d %H:%M:%S"))
ggplot(, aes(x = times)) + 
  geom_histogram(aes(y= ..density..), binwidth = 10000, colour = "black") +
  theme(axis.text.x = element_text(angle = 45)) +
  scale_x_datetime(labels = date_format("%Y-%m-%d %H:%M:%S")) +
  geom_density(alpha = .2, fill = "#FF6666")