Home > Mobile >  Plotting several value on 1 plot
Plotting several value on 1 plot

Time:12-26

I have a data frame, where I want to plot Head against Time for the several depths on the 1 plot. The expected result is Plot of Pressure Head vs Time. My data is:

data <- structure(
  list(
    Time = c(
      0,
      1343,
      1343,
      1343,
      1343,
      1348,
      1348,
      1348,
      1348,
      1353,
      1353,
      1353,
      1353,
      1358,
      1358,
      1358,
      1358,
      1363
    ),
    Node = c(
      231L,
      31L,
      61L,
      91L,
      231L,
      31L,
      61L,
      91L,
      231L,
      31L,
      61L,
      91L,
      231L,
      31L,
      61L,
      91L,
      231L,
      31L
    ),
    Depth = c(
      -2300,
      -300,-600,
      -900,
      -2300,
      -300,
      -600,
      -900,
      -2300,
      -300,
      -600,
      -900,-2300,
      -300,
      -600,
      -900,
      -2300,
      -300
    ),
    Head = c(
      -100,
      -203.46,-182.68,
      -199.26,
      -206.02,
      -207.96,
      -188.95,
      -203.36,
      -208.95,-159.2,
      -186.14,
      -198.17,
      -211.25,
      -45,
      -182.39,
      -191.23,
      -207.32,-31.66
    )
  ),
  row.names = 4:21,
  class = "data.frame"
)

I have already plotted it with the facet plots, in a way:

plot_node %>%
  mutate(
    facet = paste0(" Depth = -", abs(Depth), "mm")
  ) %>%
  ggplot(aes(Time))  
  geom_line(aes(y= Head, color ="Matrix")) 
  labs(y= "Pressure Head [mm]") 
  ggtitle('Pressure Heads') 
  scale_x_continuous(limits = c(1348,1403), breaks = seq(1348,1403, 10))  
  facet_wrap(vars(facet), ncol = 2, scales = "free")  
  theme_minimal()  
  theme(strip.text = element_text(size = 12, face = "bold"))

but in this way I can fit only 4 depthes on a facet plot, which is not quite usefull.

Any ideas how I can implement it in R?

PS: I now that the plots given in attched picture are done within Shell.

CodePudding user response:

I think the x axis variable is constant across the range in your given data but your scale_x_continuous ranges from 1348 to 1403. For a line to be visible, you need both x and y to be changing.

Assuming you have the right data, just use geom_line(aes(y= Head, linetype=Depth)) instead of adding a facet_wrap()

CodePudding user response:

I handled it in such way:

   plot_node<-node_data
plot_node <-filter(plot_node, Depth == -300 |
                                   Depth == -600 |
                                   Depth==-900 |
                                   Depth==-2300 )

plot_node$Time<-as.numeric(as.character(plot_node$Time))


plot_node %>%
  ggplot(aes(Time))  
  scale_y_reverse() 
  scale_x_continuous(limits = c(1343,1440), breaks = seq(1343,1440, 20))  
geom_line(aes(y= Head, colour = as.factor(Depth))) 
   ylab("Pressure Head [mm]") 
  labs(color="Depth")

But still, is there any way to label the lines on the plot itself? I want to put a number of depth on the line with the corresponding color.

  • Related