2

I am not very good in R, and need some help.

My ggplot has a lot of dates(in the x-axis) so you can't actually see the dates, and I want to change it to months to give a better overview of the plot.

For example to something like this in the link: Display the x-axis on ggplot as month only in R

This is the script I'm using:

r <- read.csv("xxdive.csv", header = T, sep = ";")
names(r) <- c("Date", "Number")
r <- data.frame(r)
r$Date <- factor(r$Date, ordered = T)

r[1:2, ]
Date         Number
16.02.2015   97
17.02.2015   47

library(tidyverse)

ggplot(r, aes(Date, Number)) + 
  theme_light() + 
  ggtitle("16.02.15-10.02.16") + 
  ylab("Dives") + 
  geom_line(aes(group = 1), color = "blue")

This shows what kind of data I have.

I have tried using scale etc, but I can't make it work.. I hope this was understandable, and that someone can help me!! :)

Adela
  • 1,757
  • 19
  • 37
felix93
  • 39
  • 3

2 Answers2

1

I would convert column Date to data type Date

r$Date <- as.Date(r$Date, "%d.%m.%Y");

instead of converting it to data type factor.

r$Date <- factor(r$Date, ordered = T);
Franziska W.
  • 346
  • 2
  • 4
0

It's a little tricky without a working example, but try this.

install.packages("tidyverse")
library(tidyverse)
r <- read_delim("xxdive.csv", ";", col_types = list(col_date(), col_integer()))
names(r) <- c("Date", "Number")
ggplot(r, aes(Date, Number)) + 
    geom_line(aes(group = 1), color = "blue") +
    scale_x_date(date_breaks = "1 month") +
    ylab("Dives") +     
    ggtitle("16.02.15-10.02.16") + 
    theme_light()
Ewen
  • 1,276
  • 8
  • 13