ggplot2 problem with Date format
1
0
Entering edit mode
6.1 years ago
luzglongoria ▴ 50

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:

Plot

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?

ggplot R lubridate datesinR • 7.1k views
ADD COMMENT
0
Entering edit mode

Does BigPlot %>% dplyr::mutate(Date = lubridate::dmy(Date)) %>% ggplot(..... help?

ADD REPLY
0
Entering edit mode

No. It gives me this error:

Error: Mapping should be created with aes() oraes_()`. In addition: Warning messages: 1: package ‘bindrcpp’ was built under R version 3.4.4 2: All formats failed to parse. No formats found.

ADD REPLY
0
Entering edit mode

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 the as.Date help file:

"A character string. If not specified, it will try "%Y-%m-%d" then "%Y/%m/%d" on the first non-NA element, and give an error if neither works. Otherwise, the processing is via strptime"

Your date appears to be formatted as "%d/%m/%Y" - try specifying the format when running as.Date.

ADD REPLY
0
Entering edit mode
6.1 years ago

Data frame in OP is BigPlot for ggplot. However, as.Data is on some other data as I see in OP. Is this a typo? Code you are using, didn't throw any errors in producing the plot (except some minor errors): I didn't modify your code except for as.Date(Date) instead of Date.

library(dplyr)
test=read.csv("test.txt", sep = "\t", strip.white = T, stringsAsFactors = F) 
library(ggplot2)
ggplot(test, aes(x = as.Date(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")

Rplot

To the values, i added some random numbers to get points.

> test
  Ind     Date Parasitemia       Genera       Age Year
1  11 15-07-17    20.00000 Haemoproteus     Adult 2017
2  26 23-08-16    10.00027 Haemoproteus Juveniles 2016
3  10  1-02-17    20.00040   Plasmodium     Adult 2017
4   4 15-08-17    30.00000   Plasmodium     Adult 2017
5  10  1-09-16    40.00248   Plasmodium     Adult 2016
ADD COMMENT
0
Entering edit mode

Yes, but my real data is bigger, I have more than 20 different dates and when I try to plot them I got only 3 or 4 different dates (and I want all the month to appear). that's why I am trying to include other commands from the package lubridate ("scale_x_date"). And when I include them I got the error message.

ADD REPLY

Login before adding your answer.

Traffic: 2279 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6