繁体   English   中英

在 ggplot2 中绘制多个变量 - 散点和线

[英]Plotting more than one variable in ggplot2 - scatter and line

我不得不编辑这个问题。 归根结底,这种方式更有意义。 所以我试图根据某些动物的太阳以图形方式确定活动。

我在这里创建了一个显示日出和日落的图表。 此外,圆点代表版本。 但是,我想用 map 代替 geom_errorbar(),日出时各一行,日落各一行。

我使用的代码如下所示:

library(ggplot2)
library(lubridate)

d$Photo.date = as.Date(d$Photo.Date)
d$Photo.time = hms(d$Photo.time)
d$up_dt <- ymd_hms(d$up)
d$down_dt <- ymd_hms(d$down)

ggplot(d) +
  geom_errorbar(aes(x = date(up_dt), 
                    ymin = hour(up_dt)+1/60*minute(up_dt), 
                    ymax = hour(down_dt)+1/60*minute(down_dt)),
                lwd = 3, color = "light grey")+
  geom_count(aes(x = Photo.date, y = Photo.time@hour + Photo.time@minute/60))+
  scale_y_continuous(breaks = seq(0,24,4), name = "Uhrzeit", 
                     labels = c("0:00", "4:00", "8:00", "12:00", 
                                "16:00", "20:00", "23:59")) +
  xlab("Datum")

如何使用日出和日落的两条线而不是 geom_errorbar()?

谢谢你的帮助

未经测试的代码,因为您没有让我们访问d ...

ggplot(d) +
  geom_ribbon(aes(x = date(up_dt), 
                    ymin = hour(up_dt)+1/60*minute(up_dt), 
                    ymax = hour(down_dt)+1/60*minute(down_dt)),
                lwd = 3, color = "light grey")+
  geom_count(aes(x = Photo.date, y = Photo.time@hour + Photo.time@minute/60))+
  scale_y_continuous(breaks = seq(0,24,4), name = "Uhrzeit", 
                     labels = c("0:00", "4:00", "8:00", "12:00", 
                                "16:00", "20:00", "23:59")) +
  xlab("Datum")

或者

ggplot(d) +
  geom_line(aes(x = date(up_dt), 
                    y = hour(up_dt)+1/60*minute(up_dt), 
                lwd = 3, color = "light grey")+
  geom_line(aes(x = date(up_dt), 
                    y = hour(down_dt)+1/60*minute(down_dt)),
                lwd = 3, color = "light grey")+
  geom_count(aes(x = Photo.date, y = Photo.time@hour + Photo.time@minute/60))+
  scale_y_continuous(breaks = seq(0,24,4), name = "Uhrzeit", 
                     labels = c("0:00", "4:00", "8:00", "12:00", 
                                "16:00", "20:00", "23:59")) +
  xlab("Datum")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM