0

I'm generating a plot with this code:

BPL2.0.5op <- subset(ambiental2021, dependencia=="BPL2.sala" & tipo.control=="NV")
BPL2.0.5op <- BPL2.0.5op[!is.na(BPL2.0.5op[,"part.0.5"]),]
BPL2.0.5op$punto <- naturalsort::naturalfactor(paste("Punto", BPL2.0.5op$punto, sep="."))

ggplot(BPL2.0.5op, 
       aes(x=fecha, y=part.0.5, group = punto, fill = punto)) +
  geom_point(aes(colour = punto), size = 1.5) +
  geom_line(aes(colour = punto), linetype = 1, size = 1) +
  theme(plot.title = element_text(size = 20, face = "bold")) +
  ggtitle("PARTÍCULAS DE 0,5 micras SALA BPL2") +
  ylab("Partículas 0,5 micras/m3") +
  xlab("fecha del control") +
  geom_hline(yintercept = 3520000, colour = "red", size = 1, linetype = 6) +
  scale_x_date(breaks = unique(BPL2.0.5op$fecha), labels = date_format("%d/%m/%Y")) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 7), legend.position = "none") +
  facet_wrap(~ punto, ncol = 5, strip.position = "left")

And I get this type of graph:

enter image description here

Due to "Punto.17" peaks I lose resolution in the rest of the data. Mi intention is to add a "ylim()" argument to place a limit in the y axis. I used this code:

BPL2.0.5op <- subset(ambiental2021, dependencia=="BPL2.sala" & tipo.control=="NV")
BPL2.0.5op <- BPL2.0.5op[!is.na(BPL2.0.5op[,"part.0.5"]),]
BPL2.0.5op$punto <- naturalsort::naturalfactor(paste("Punto", BPL2.0.5op$punto, sep="."))

ggplot(BPL2.0.5op, 
       aes(x=fecha, y=part.0.5, group = punto, fill = punto)) +
  geom_point(aes(colour = punto), size = 1.5) +
  geom_line(aes(colour = punto), linetype = 1, size = 1) +
  theme(plot.title = element_text(size = 20, face = "bold")) +
  ggtitle("PARTÍCULAS DE 0,5 micras SALA BPL2") +
  ylab("Partículas 0,5 micras/m3") +
  ylim(0,4000000) +
  xlab("fecha del control") +
  geom_hline(yintercept = 3520000, colour = "red", size = 1, linetype = 6) +
  scale_x_date(breaks = unique(BPL2.0.5op$fecha), labels = date_format("%d/%m/%Y")) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 7), legend.position = "none") +
  facet_wrap(~ punto, ncol = 5, strip.position = "left")

And I get this graph:

enter image description here

It's seems as if there are no data... I would like the line to go up even though the point is far from the specified ylim(). I'd like a graph like this:

enter image description here

Please, could you show me how to do it?

Dani
  • 153
  • 8
  • What is your specific problem with the second version. Where do you want the line to go to? – IRTFM Jan 23 '23 at 11:20
  • 1
    The gap indicates omitted observations. If you "just" want to "zoom" in you could probably use 'coord_cartesian' or similar to limit the shown area. Anyhow, if you provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) with just enough data and code to reproduce the issue you are having it would be much easier for others to find and test an answer to your question. – dario Jan 23 '23 at 11:28
  • I'd like the line to go "out of the graph". I mean, I don't mind the exact value of the peaks, I only want to show that these values are above the limit (red line). I edit the image to show waht my intention is. – Dani Jan 23 '23 at 11:30
  • "coord_cartesian" is definitely the option that fits my graph. – Dani Jan 23 '23 at 11:40
  • 1
    @Quinten Are you sure? I think that 'scale_y_continuous' will draw the line only up to the largest **plotted** value for y... – dario Jan 23 '23 at 11:44

1 Answers1

2
library(ggplot2)
ggplot(data.frame(a = 1:10, b = c(1:6,4:1)), aes(x = a, y = b)) +
  geom_point() +
  geom_line() +
  coord_cartesian(ylim = c(1,4))

Will create the following "zoomed in" plot:

enter image description here

dario
  • 6,415
  • 2
  • 12
  • 26