[英]Density plot exceeds x-axis interval
I am attempting to make some density plots using ggplot2 but the distribution exceeds the bounds of my data.我正在尝试使用 ggplot2 制作一些密度图,但分布超出了我的数据范围。 Specifically, I am trying to show the distribution of GPS locations in 2 habitat types over time (hours of the day).
具体来说,我试图显示 GPS 位置随时间(一天中的几个小时)在 2 种栖息地类型中的分布。 As I am only interested in displaying the distribution of locations during daylight (0500 to 2100), I have filtered out hours occurring at night.
因为我只对在白天(0500 到 2100)显示位置分布感兴趣,所以我过滤掉了夜间发生的时间。 However when I plot the data, the distribution exceeds both hours 5 and 21 on the x-axis.
但是,当我绘制数据时,分布在 x 轴上超过了 5 小时和 21 小时。 I have a feeling it has to do with "scale_x_continuous" in ggplot, where I have specified the limits to be (0,24), but that doesn't explain why the distribution exceeds daytime hours when there is not data before or after those hours.
我有一种感觉,它与 ggplot 中的“scale_x_continuous”有关,我在其中指定了限制为 (0,24),但这并不能解释为什么在没有数据之前或之后分布超过白天的时间小时。 FYI, I do want the entire time series to show even though I don't have data for each hour.
仅供参考,即使我没有每小时的数据,我也确实希望显示整个时间序列。
But again, I only have data between the hours of 5 and 21. Can someone explain what might be going on here?但同样,我只有 5 到 21 小时之间的数据。有人可以解释这里可能发生的事情吗? Hopefully I am making sense.
希望我是有道理的。 Thanks!
谢谢!
Sample code:示例代码:
locs.19
locs.19 <- subset(locs, hour >= 5 & hour <=21)
> head(locs.19)
ID x y datetime hour shelfhab
2019_01 -122.9979 37.68930 2019-06-07 05:04 5 inner
2019_01 -122.9977 37.68833 2019-06-07 05:06 5 inner
2019_01 -122.9975 37.68737 2019-06-07 05:08 5 inner
2019_01 -122.9974 37.68644 2019-06-07 05:10 5 inner
2019_01 -122.9974 37.68550 2019-06-07 05:12 5 inner
2019_01 -122.9974 37.68457 2019-06-07 05:14 5 inner
> str(locs.19)
'data.frame' : 6531 obs. of 6 variables:
$ ID : chr "2019_01" "2019_01" "2019_01" "2019_01" ...
$ x : num -123 -123 -123 -123 -123 ...
$ y : num 37.7 37.7 37.7 37.7 37.7 ...
$ datetime : chr "2019-06-07 05:04" "2019-06-07 05:06" "2019-06-07 05:08" "2019-06-07 05:10" ...
$ hour : int 5 5 5 5 5 5 5 5 5 5 ...
$ shelfhab : chr "inner" "inner" "inner" "inner" ...
### Plot ###
p19 <- ggplot(locs.19, aes(x = hour))+
geom_density(aes(fill = shelfhab), alpha = 0.4)+
xlab("Time of Day (24 h)")+
theme(legend.position = "right",panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
text = element_text(size = 14,family = "Calibri"))+
scale_x_continuous(breaks=seq(0,24,2),limits = c(0, 24), expand = c(0,1))
p19
The issue is that you set the limits in scale_x_continuous
.问题是您在
scale_x_continuous
设置了限制。 Thereby you set the range over which the denisty is estimated.因此,您可以设置估计密度的范围。 To achieve your desired result simply set the limits via
coord_cartesian
.要获得您想要的结果,只需通过
coord_cartesian
设置限制coord_cartesian
。 This way the density is only estimated on your data while you still get a scale ranging from 0 to 24 hours.这样,密度仅根据您的数据进行估计,而您仍然可以获得 0 到 24 小时的范围。
Using some random example data:使用一些随机示例数据:
set.seed(42)
# Example data
locs.19 <- data.frame(hour = sample(5:21, 1000, replace = TRUE),
shelfhab = sample(c("inner", "outer"), 1000, replace = TRUE))
library(ggplot2)
ggplot(locs.19, aes(x = hour))+
geom_density(aes(fill = shelfhab), alpha = 0.4)+
xlab("Time of Day (24 h)")+
theme(legend.position = "right",panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
text = element_text(size = 14))+
scale_x_continuous(breaks=seq(0,24,2), expand = c(0,1)) +
coord_cartesian(xlim = c(0, 24))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.