How to plot the graph using all the dates in x axis using R?

57 views
Skip to first unread message

Gokul Anand

unread,
Apr 9, 2020, 11:10:21 AM4/9/20
to ggplot2
I want to plot a graph with all the dates in x axis.
Here is the code I used

```
library(tidyverse)
library(readxl)


data <- readxl::read_excel("Sentinel-1 Metadata info.xls")
centroid <- slice(data,1)

data %>% 
  ggplot(aes(`Date of acquisition`, `Bperp(m)`)) +
  geom_point() +
  geom_segment(aes(x = centroid$`Date of acquisition`, y = centroid$`Bperp(m)`, 
                   xend = `Date of acquisition`, yend = `Bperp(m)`)) +
  theme_minimal()
```
I got the graph like this


Screenshot from 2020-04-09 12-29-38.png



But I want to display all the dates in the format DDMMYYYY.

Brandon Hurr

unread,
Apr 9, 2020, 11:25:50 AM4/9/20
to Gokul Anand, ggplot2
Check out the docs for ggplot2::scale_x_date()

Should be something like
base + scale_x_date(date_labels = "%d%m%Y")

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ggplot2/c2bd9166-1ffd-483c-8986-53082c7b8913%40googlegroups.com.

Gokul Anand

unread,
Apr 10, 2020, 1:44:30 AM4/10/20
to ggplot2
The error comes like
Error in data %>% ggplot(aes(`Date of acquisition`, `Bperp(m)`)) : 
  could not find function "%>%"

Roman Luštrik

unread,
Apr 10, 2020, 3:06:14 AM4/10/20
to Gokul Anand, ggplot2
If I search the internet for your error message (could not find function "%>%"), even on ecosia, this is the first hit:


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.


--
In God we trust, all others bring data.

Gokul Anand

unread,
Apr 10, 2020, 4:21:09 AM4/10/20
to ggplot2
Still not working sir


On Thursday, April 9, 2020 at 8:40:21 PM UTC+5:30, Gokul Anand wrote:

Roman Luštrik

unread,
Apr 10, 2020, 4:23:59 AM4/10/20
to Gokul Anand, ggplot2
When claiming that something is not working, it would help if you show what you tried (a reproducible example goes a long way here) and what is not working. Are there any errors?

Cheers,
Roman

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.

Gokul Anand

unread,
Apr 10, 2020, 4:31:06 AM4/10/20
to ggplot2
According to the stackoverflow I tried and got output like this. I am very new to R but I could understand the concept if I get a direct example which is equivalent to what I am trying.

> library(tidyverse)
> library(readxl)
> library(magrittr) # needs to be run every time you start R and want to use %>%
> library(dplyr)    # alternatively, this also loads %>%
> 
> data <- read_excel("Desktop/Sentinel-1 Metadata info.xls")
> centroid <- slice(data,1)                                                                             
> 
> 
> data %>% 
+   ggplot(aes(`Date of acquisition`, `Bperp(m)`)) +
+   geom_point() +
+   geom_segment(aes(x = centroid$`Date of acquisition`, y = centroid$`Bperp(m)`, 
+                    xend = `Date of acquisition`, yend = `Bperp(m)`)) +
+   scale_x_date(date_labels = "%d/%m/%Y") +
+   theme_minimal()
Error: Invalid input: date_trans works with objects of class Date only
>

On Thursday, April 9, 2020 at 8:40:21 PM UTC+5:30, Gokul Anand wrote:

Roman Luštrik

unread,
Apr 10, 2020, 4:43:51 AM4/10/20
to Gokul Anand, ggplot2
Two things,

you should not pass values to arguments within `aes`. For example, you do

aes(x = centroid$`Date of acquisition`...)

but should be

aes(x = `Date of acquisition`...)

The error message says that your variable is (Date of acquisition) is not in the proper Date format. You will have to use `as.Date` to properly convert what you have now to a Date format. If help file for as.Date is not clear enough, please search the internet. This question has been answered many times.

Cheers,
Roman 


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages