Reputation: 135
I am trying to plot the number of unique detections per day throughout the year. I had data that looked like this.
I summarized the number of unique detections per day using these codes
unique_day <- data %>% group_by(Day,tag) %>% filter(Date==min(Date)) %>% slice(1) %>% ungroup()
sum <- unique_day %>% group_by(Date) %>% summarise(Detections=n())
I then ended up with a dataframe like this
I then plot with this code
sum[year(sum$Date)==2016,] %>%
ggplot(aes(x=Date,y=Detections))+
theme_bw(base_size = 16,base_family = 'serif')+
theme(panel.grid.major = element_blank(),panel.grid.minor=element_blank())+
geom_line(size=1)+
scale_x_datetime(date_breaks = '1 month',date_labels = "%b",
limits = c(as.POSIXct('2016-01-01'),as.POSIXct("2016-12-01")))+
ggtitle('DIS 2016')+
theme(plot.title = element_text(hjust = 0.5))+
xlab("Date")+
scale_y_continuous(expand = c(0,0),limits = c(0,5))
I cant seem to get the plot to start at 0... I figure it always starts at 1 because there are no 0 values for detections... I only have a data frame summarizing the days when there was detections, not when there was not detections. I have tried using ylim, scale_y_continuous and coord_cartesian... any ideas?
Any ideas?
Upvotes: 0
Views: 837
Reputation: 16998
Here is a simple way to get to your problem:
df_null <- data.frame(Date = seq(as.Date("2015/01/01"), by = "day", length.out = 365),
Detections = 0)
For the year 2015 we create a data.frame containing all days with value 0. Suppose your data.frame looks like this:
df <- data.frame(Date = c(as.Date("2015/06/07"), as.Date("2015/06/08"), as.Date("2015/12/12")),
Detections = 1:3)
Using dplyr
we combine those two data.frames and summarize the values:
df %>%
bind_rows(df_null) %>%
group_by(Date) %>%
summarise(Detections = sum(Detections))
Finally you can get your plot using your ggplot2
-code.
Upvotes: 1