1

I have a database including day and places, with other relevant information. I am currently using the dcast function in order to count each occurence in a day and place (that in the real database are represented by numbers). Later, I want to do a plotly grafic where day represents my x coordinate, place represents my y coordinate, and z will represent the number of ocurence.

I provide some dummy data here, to simplify:

day<-c(1,2,3)
place<-c("here", "there", "far away")
frequency<-c(1,2,4)

d<-data.frame(day,place,frequency)

z<-dcast(d, day~place, fill=0)

How can I plot this using plotly ?

L8call
  • 87
  • 1
  • 1
  • 6
  • You will have to be more specific. What would you like to have for x-axis (Day?) and y-axis (Frequency?)? – MLavoie Jun 13 '18 at 09:26
  • I wrote that: 'where day represents my x coordinate, place represents my y coordinate, and z will represent the number of ocurence. ' Basically (x,y,z)<-(day, place, frequency) – L8call Jun 13 '18 at 14:14
  • does not sound like the definition of volcano plot (for example [here](https://moderndata.plot.ly/interactive-volcano-plots-r-plotly/)). It sounds more like a 3D plot. – MLavoie Jun 13 '18 at 17:20
  • I believe I can use both, but either way, I do not understand to plot it with the data that I have. I was refering to something like the Basic 3D Surface Plot in this link: https://plot.ly/r/3d-surface-plots/ – L8call Jun 13 '18 at 22:04
  • like: `plot_ly(data = d, x = ~day, y = ~place, z = ~frequency, mode = "markers")`? – MLavoie Jun 13 '18 at 22:12
  • the shape will be that, but i want to plot z that also should give my ( for the example above) than when it is day 1, and I am in 'there' it should mark 0. Basically the output of the function dcast. – L8call Jun 13 '18 at 22:28
  • Maybe someone will have a better answer but in the meantime, you could do: `d_long <- melt(z, id.vars=c("day"))` followed with `plot_ly(data = d_long, x = ~day, y = ~variable, z = ~value, mode = "markers")`. – MLavoie Jun 13 '18 at 22:46
  • The last answer provided helped me a lot! It is what I wanted to do – L8call Jun 13 '18 at 23:05
  • I added my comment to my answer. So if it fixes your problem you could accept my answer :-) – MLavoie Jun 13 '18 at 23:09

1 Answers1

2

This really broad, but you could try this:

 place<-c("here", "there", "faraway")
 frequency<-c(1,2,4)

 d<-data.frame(day,place,frequency)

 z<-dcast(d, day~place, fill=0)
 z$day <- as.factor(z$day)
plot_ly(data = z, x = ~day, y = ~here, type = "bar", name = 'here') %>%
  add_trace(y = ~there, name = 'there') %>%
  add_trace(y = ~faraway, name = 'faraway') %>%
  layout(yaxis = list(title = 'Count'), barmode = 'group')

enter image description here

Edit: Following OP comments. You can put it back to long format and then plot.

d_long <- melt(z, id.vars=c("day"))
plot_ly(data = d_long, x = ~day, y = ~variable, z = ~value, mode = "markers") 
MLavoie
  • 9,671
  • 41
  • 36
  • 56