Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to show lines when using ylim()?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading