0

I have a dataset which is a function of time and frequency. When I melt it I want to retain actual time (date) and frequency values as I want a 2d plot with y axis as frequency and x axis as time.

I tried to retain a column of desired axis values but melting makes it factor and stat_contour throws error. My data is some thing like the following

   a = data.frame(date=time,power=power)
   names(a) = c('date',period)

where period is

  [1]   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8
 [23]   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8
 [45]   8   8   8   8   8   8  16  16  16  16  16  16  16  16  16  16  16  16  16  16  16  16
 [67]  16  16  16  16  16  16  16  16  16  16  16  16  16  16  16  16  16  16  16  16  16  16
 [89]  16  16  16  16  16  16  16  16  16  16  16  16  32  32  32  32  32  32  32  32  32  32
[111]  32  32  32  32  32  32  32  32  32  32  32  32  32  32  32  32  32  32  32  32  32  32
[133]  32  32  32  32  32  32  32  32  32  32  32  32  32  32  32  32  32  32  64  64  64  64
[155]  64  64  64  64  64  64  64  64  64  64  64  64  64  64  64  64  64  64  64  64  64  64
[177]  64  64  64  64  64  64  64  64  64  64  64  64  64  64  64  64  64  64  64  64  64  64
[199]  64  64 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128
[221] 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128
[243] 128 128 128 128 128 128 128 128 256
power = melt(a,id.vars = 'date')

       date period      power
1 850-01-01      8 0.05106766
2 851-01-01      8 0.05926821
3 852-01-01      8 0.06783015
4 853-01-01      8 0.07681627
5 854-01-01      8 0.08636516
6 855-01-01      8 0.09667054

ggplot(power, aes(x = date, y = period, z = power)) +
  stat_contour()

this gives an error as period column is a factor; if I make it numeric I loose the exact Y axis labels. Is there any workaround? thanks

1 Answers1

0

You did not provide a reproducible example, but in principle you have to change the axis labels manually:

library(ggplot2)
d <- expand.grid(x = 1:100, y = paste("P", 1:100))
d$z <- rnorm(10000)

ggplot(d, aes(x, as.numeric(y), z = z)) + 
   geom_contour() +
   scale_y_continuous(breaks = seq_along(levels(d$y)), breaks = levels(d$y))

However, I find it a bit difficult to really understand what a contour plot with one axis being a qualitative should represent. My guess is that your y-axis is a factor because of the melt operation and that there is some column wrongly specified as factor and hence the overall column is a factor when in fact it should be a a numeric (that is it shows 8, should represent 8 but is actually a factor with level 8).

If this is the case, a normal as.numeric does not work as expected b/c it maps simply the first level to 1 the second level to 2 and so on. But you want it to be mapped to the numeric of the string representation. In this case something like this should work:

library(ggplot2)
d <- expand.grid(x = 1:100, y = sample(1000, 100)) # y should be a numeric but is a factor
d$z <- rnorm(10000)

ggplot(d, aes(x, as.numeric(as.character(y)), z = z)) + 
   geom_contour()

This all is, however, guesswork, because you did not provide us with a full reproducible example. Please read and follow these gudieline in the future when posting questions here how to ask a good question and how to give a reproducible example

thothal
  • 16,690
  • 3
  • 36
  • 71