Hi there, I'm trying to plot my data but I am not able to present dates as I want (by month and with the name of the month). First of all I show you part of my data:
Ind Date Parasitemia Genera Age Year
11 15/7/17 0 Haemoproteus Adult 2017
26 23/8/16 0,000274329 Haemoproteus Juveniles 2016
10 1/2/17 0,000395499 Plasmodium Adult 2017
4 15/8/17 0 Plasmodium Adult 2017
10 1/9/16 0,002482416 Plasmodium Adult 2016
as you can see I have Individuals (the same individua) that have been analyzed over time (from February 2016 to May 2017). My goal is to plot a general graph like this one:
The code I have been using is this one:
ggplot(BigPlot, aes(x = Date, y = log(Parasitemia), col = Genera)) + geom_smooth() +
geom_point(aes(shape = Genera), size = 2) + ylab("Log10 Parasitemia") + xlab("Date") +
facet_grid(Year~Age~Genera) +
scale_size("fixed_y") +
theme(axis.text = element_text(size = 20), legend.text = element_text(size=20),
axis.title.x = element_text(size = 20), axis.title.y = element_text(size = 20),
legend.title = element_text(size = 20), strip.text.x = element_text(size = 20))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) +
theme(legend.position = "none")
BUT If I try to modify the dates in order to show every month (by using the package lubridate I got an eeror message: same code that before plus
(scale_x_date(labels=date_format("%b %y")))
Then,
Error: Invalid input: date_trans works with objects of class Date only
I have also tried with
ggplot(BigPlot, aes(x = Date, y = log(Parasitemia), col = Genera)) + geom_smooth() +
geom_point(aes(shape = Genera), size = 2) + ylab("Log10 Parasitemia") + xlab("Date") +
facet_grid(Year~Age~Genera) +
scale_size("fixed_y") +
theme(axis.text = element_text(size = 20), legend.text = element_text(size=20),
axis.title.x = element_text(size = 20), axis.title.y = element_text(size = 20),
legend.title = element_text(size = 20), strip.text.x = element_text(size = 20))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) +
(scale_x_date(breaks = date_breaks("1 month"),
labels = date_format("%m"))) +
geom_rect(data = BigPlot,
xmin = as.Date("2016-02-01", "%Y-%m-%d"),
xmax = as.Date("2017-09-15", "%Y-%m-%d"),
ymin = -Inf,
ymax = Inf),
fill = "gray",
alpha = 0.5)
but I got the same error message:
Error: Invalid input: date_trans works with objects of class Date only
I guess I need to modify my original file somehow. I have tried to to this by :
newdata$event_Date <- as.Date(newdata$event_date)
but then,
Error in as.Date.default(newdata$event_date) :
do not know how to convert 'newdata$event_date' to class “Date”
Any ideas?
Does
BigPlot %>% dplyr::mutate(Date = lubridate::dmy(Date)) %>% ggplot(.....
help?No. It gives me this error:
Error: Mapping should be created with
aes() or
aes_()`. In addition: Warning messages: 1: package ‘bindrcpp’ was built under R version 3.4.4 2: All formats failed to parse. No formats found.In the code snippet using
as.Date
you refer to the event_date column - how are the dates formatted in this column? is it the same as the "Date" column in the original data you showed us? If so, have a look at theas.Date
help file:Your date appears to be formatted as "%d/%m/%Y" - try specifying the format when running
as.Date
.