Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show all date values on ggplot x axis - R

Tags:

date

r

ggplot2

In the following little dataset, I would like to plot date values on the x axis. I want all ticks of the dates present in the dataframe to be labelled. At the moment it is only labelling 3 random ticks. I could change the column to a factor, but having as a date means the x axis is spaced according to the time interval between points. So far the only answers I can find are for questions that want to show every month, or at least equally spaced time intervals. I just want to show the values in my dataset. Thank you

Data:

dput(melt)
structure(list(Sample = structure(c(16023, 16023, 16027, 16027, 
16031, 16031, 16035, 16035, 16038, 16038, 16044, 16044, 16023, 
16023, 16027, 16027, 16031, 16031, 16035, 16035, 16038, 16038, 
16044, 16044), class = "Date"), Treatment = structure(c(1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L), .Label = c("T1", "T5"), class = "factor"), 
    variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
    ), .Label = c("20:5n-3", "20:4n-6"), class = "factor"), value = c(1.21879849498785, 
    1.88548990818282, 1.87990774775065, 1.25350826053539, 3.10352691206175, 
    1.18465888918519, 3.15176346709818, 1.82106970985234, 2.73680349074891, 
    1.58066166099405, 1.43967823968644, 1.51515884762159, 9.25194151767438, 
    9.23279864348857, 14.0535236345336, 9.06572816754405, 9.90634511302451, 
    10.0962470101343, 7.44314990623579, 12.7996706096935, 5.96573623304832, 
    8.070195431115, 2.57433899670204, 11.4388591839374)), row.names = c(NA, 
-24L), class = "data.frame")

Code:

library(ggplot2)

ggplot(melt, aes(x=Sample, y=value, colour=variable, group=variable))+
  facet_wrap(~Treatment)+
  geom_point()+
  geom_line()

Plot:

enter image description here

like image 910
J.Con Avatar asked Jul 31 '18 01:07

J.Con


People also ask

What does %>% do in ggplot?

%>% is a pipe operator reexported from the magrittr package. Start by reading the vignette. Introducing magrittr. Adding things to a ggplot changes the object that gets created. The print method of ggplot draws an appropriate plot depending upon the contents of the variable.

How do you add data point labels to ggplot?

To put labels directly in the ggplot2 plot we add data related to the label in the data frame. Then we use functions geom_text() or geom_label() to create label beside every data point. Both the functions work the same with the only difference being in appearance.

How do I change X label to R in ggplot?

To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .


1 Answers

You can supply melt$Sample as the breaks. You'll probably also want to rotate the axis labels to avoid overlap.

ggplot(melt, aes(x = Sample, y = value, colour = variable, group = variable)) +
  facet_wrap(~Treatment) +
  geom_point() +
  geom_line() + 
  scale_x_date(breaks = melt$Sample) + 
  theme(axis.text.x = element_text(angle = 90))

enter image description here

like image 106
neilfws Avatar answered Sep 27 '22 18:09

neilfws