0

I have a R data structure which has numerical values and char dates, after melting together in the field value. I am thinking how you can manage the variable Date after melting for the ggplot. I would not like to separate a list of dates from the data structure dat.m or molten before plotting.

  1. to take a list of dates before plotting (deprecated) OR
  2. to somehow apply the outputs of variable = Date (call here dates) to the parameter scale_x_discrete("Date", labels = dates) OR
  3. plot directly somehow with dat.m?

Expected output: facet plot with dates Date on the x-axis, and other values than Date in molten$value in y-axis in Fig. 1

Fig. 1 Expected output with dates Date on x-axis (sorry, here partially cut out days)

enter image description here

Point (2)

Code which output follows

dat.m <- structure(list(REM = c(160, 150), kevyt = c(380, 420), syva = c(110, 180), Date = c("1.1.2011",  "2.2.2012")), .Names = c("REM", "kevyt", "syva", "Date"), class = "data.frame", row.names = c(NA, -2L))
library("data.table")
str(dat.m)
print(dat.m)

molten <- melt(as.data.table(dat.m, keep.rownames = "Vars"), id.vars = "Vars") # https://stackoverflow.com/a/44128640/54964
print("Hello 2")
str(molten)
print(molten)

# TODO take dates from molten[, "Date"]. Here or somehow inside ggplot? Pseudocodes:
# dates <- molten[, "Date"] ... 
# OR
# somehow directly in ggplot

library("ggplot2")
p <- ggplot(molten, aes(x = Vars, y = value, fill=variable)) + 
    geom_bar(stat='identity') 

Output

'data.frame':   2 obs. of  4 variables:
 $ REM  : num  160 150
 $ kevyt: num  380 420
 $ syva : num  110 180
 $ Date : chr  "1.1.2011" "2.2.2012"
  REM kevyt syva     Date
1 160   380  110 1.1.2011
2 150   420  180 2.2.2012
Warning message:
In melt.data.table(as.data.table(dat.m, keep.rownames = "Vars"),  :
  'measure.vars' [REM, kevyt, syva, Date] are not all of the same type. By order of hierarchy, the molten data value column will be of type 'character'. All measure variables not of type 'character' will be coerced to. Check DETAILS in ?melt.data.table for more on coercion.
[1] "Hello 2"
Classes ‘data.table’ and 'data.frame':  8 obs. of  3 variables:
 $ Vars    : chr  "1" "2" "1" "2" ...
 $ variable: Factor w/ 4 levels "REM","kevyt",..: 1 1 2 2 3 3 4 4
 $ value   : chr  "160" "150" "380" "420" ...
 - attr(*, ".internal.selfref")=<externalptr> 
   Vars variable    value
1:    1      REM      160
2:    2      REM      150
3:    1    kevyt      380
4:    2    kevyt      420
5:    1     syva      110
6:    2     syva      180
7:    1     Date 1.1.2011
8:    2     Date 2.2.2012

Point (3)

I cannot simply do the proposal of the thread R: ggplot display all dates on x axis because molten does not contain only integers but character dates too. Pseudocode using directly dat.m based on the thread

# https://stackoverflow.com/a/41856325/54964
ggplot(data = dat.m) + 
    geom_point(mapping = aes(x = Date, y = value, fill = variable)) +
    geom_bar(stat='identity') + 
    scale_x_date(date_labels="%s", Dates) # TODO problem here 

See Point (2) for the rest of the code.

R: 3.4.0 (backports)
OS: Debian 8.7

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

1 Answers1

1

If you want dates on the x axis you need to exclude them from the melt...

molten <- melt(dat.m, id.vars = "Date") 

To reproduce (almost) your desired chart, you can do the following...

p <- ggplot(molten, aes(x = Date, y = value, colour=variable, group=variable)) + 
            geom_line() + 
            facet_wrap(facets="variable")

enter image description here

Andrew Gustar
  • 17,295
  • 1
  • 22
  • 32