Hello,

I hadn't understood the problem, sorry.
The problem are the bar plots, ggplot is plotting one in the "A" facet. And since there is nothing to plot, the bars start at 0.

A hack is to plot facet "A" separately and then combine the plots with one of several ways to combine ggplot plots. Below is an example with cowplot::plot_grid


library(ggplot2)
library(dplyr)
library(cowplot)

p1 <- df %>%
  filter(nm == "A") %>%
  ggplot(aes(x = date)) +
  geom_line(aes(y = val2)) +
  facet_wrap(~ nm, scales = "free_y") +
  theme(plot.margin = unit(c(0.2, 0, 0.1, 0), "cm"))

p2 <- df %>%
  filter(nm != "A") %>%
  ggplot(aes(x = date)) +
  geom_col(aes(y = val0), na.rm = TRUE, fill = "white") +
  geom_line(aes(y = val1)) +
  ylab("") +
  facet_wrap(~ nm, scales = "free_y")

plot_grid(p1, p2, rel_widths = c(1, 2))


Hope this helps,

Rui Barradas



Às 20:10 de 01/08/2024, p...@philipsmith.ca escreveu:
Thanks for the suggestion, but this does not give me what I want. Each chart needs its own unique scale on the y-axis.

Philip


On 2024-08-01 15:08, Rui Barradas wrote:
Às 19:01 de 01/08/2024, p...@philipsmith.ca escreveu:
I am asking for help with a ggplot2 program that has facets. There are actually 100 facets in my program, but in the example below I have limited the number to 3. There are two kinds of charts among the facets. One kind is a simple line plot with all of the y-values greater than zero. The facet for "A" in my example below is this kind. The other kind is a line plot combined with a bar chart with some of the y-values being positive and others negative. The facets for "B" and "C" in my example are this kind.

The facets for "B" and "C" look the way I want them to. However the facet for "A" has a scale on the y-axis that starts at zero, whereas I would like the minimum value on this scale to be non-zero, chosen by ggplot2 to be closer to the minimum value of y for that particular facet.

My example may not be the most efficient way to achieve this, but it works except for one aspect. Chart A, for which I do not wish to show a zero line, does indeed not show a zero line but it nevertheless chooses a scale for the y-axis that has a minimum value of zero. How can I adjust the code so that it chooses a minimum value on the y-axis that is non-zero and closer to the minimum actual y-value (as would be the case for a simple line chart alone, without any facets)?

library(ggplot2)
library(dplyr)

df <- data.frame(
   date=c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6),
nm=c("A","B","C","A","B","C","A","B","C","A","B","C","A","B","C","A","B","C"),
   val0=c(NA,-5,4,NA,-3,3,NA,2,4,NA,3,3,NA,3,1,NA,-3,-4),
   val1=c(NA,-3,6,NA,-1,4,NA,5,5,NA,7,2,NA,4,3,NA,-2,-2),
   val2=c(50,NA,NA,53,NA,NA,62,NA,NA,56,NA,NA,54,NA,NA,61,NA,NA),
   zline=c(NA,0,0,NA,0,0,NA,0,0,NA,0,0,NA,0,0,NA,0,0)
)

ggplot(df)+
   geom_col(aes(x=date,y=val0),na.rm=TRUE,fill="white")+
   geom_line(aes(x=date,y=val1))+
   geom_line(aes(x=date,y=val2))+
   geom_hline(aes(yintercept=zline),na.rm=TRUE)+
   facet_wrap(~nm,scales="free_y")

Thank you for your assistance.

Philip

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Hello,

Try to remove

scales="free_y"

from facet_wrap(). With scales="free_y" each facet will have its own y limits, given by the data plotted in each of them. If you want a global y limits, don't use it.

Hope this helps,

Rui Barradas


--
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença 
de vírus.
www.avg.com

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to