dynamic y-scale

Hi everybody

i am new in R studi and have a problem with the y-scale:
I have made a graphic (geom_bar, geom_line). However, I cannot set the scaling of the y-axis dynamically (as in Excel). Depending on the filter setting, the output has other values, instead of millions only 100'000. Unfortunately, this y-scaling is then not displayed. Can anyone help me further on how I can set the y-axis dynamically - depending on the filtered data base.

Here my Code:

# dataset <- data.frame(Datum, Region (manuell eingefügte Spalte), Betrag, Betrag in, Betrag out, Diff Betrag IN OUT)
# dataset <- unique(dataset)

# Fügen oder geben Sie hier Ihren Skriptcode ein:

library(ggplot2)
names(dataset) <- c("Datum", "Region", "Betrag", "Betragin", "Betragout", "Diff")
dataset$group <- c(1)
dataset[is.na(dataset)] = 0

ggplot(dataset)+
 geom_bar(mapping = aes(x=substr(Datum,start=1,stop=8), y=Betragin/1000000, fill="Betragin/1000000"), stat="identity",, width=0.8)+
  geom_bar(mapping= aes(x=substr(Datum,start=1,stop=8), y=Betragout/1000000, fill="Betragout/1000000"), stat="identity", width=0.8)+
  geom_point(mapping= aes(x=substr(Datum,start=1,stop=8), y=Diff/1000000, group=group, fill="Diff/1000000"), color="NA", size=1) +
  geom_line(mapping= aes(x=substr(Datum,start=1,stop=8), y=Diff/1000000, group=group, fill="Diff/1000000"), color="#666666", size=1)+
  theme_gray(base_size=12)+
    labs(x="",y="CHF in Mio.")+
    scale_y_continuous(limits=c(min(dataset$Betragout/1000000),max(dataset$Betragin/1000000)), breaks = seq(-100000,100000, by=0.5), minor_breaks=seq(-100000,100000, by=0.1))+
     theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
      scale_color_manual(values=c("#37A794","#dd667f", "#666666"),
                     name="", labels=c("Betrag In", "Betrag Out", "Differenz In Out"))+
      scale_fill_manual(values=c("#37A794","#dd667f", "#666666"),
                     name="", labels=c("Betrag In", "Betrag Out", "Differenz In Out"))+
      theme(legend.position = "bottom")+
facet_grid(~Region)

Thank you for your help
best regard
kaha

I'm not sure I understand your issue but if it is what I think it is if you don't hard code the axis limits, the limits are going to be set "dynamically" as you are expecting

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

.

Hi

Thank you for your response. I already have this statement:

scale_y_continuous(limits=c(min(dataset$Betragout/1000000),max(dataset$Betragin/1000000)), breaks = seq(-100000,100000, by=0.5), minor_breaks=seq(-100000,100000, by=0.1))

but it doesn't work.
With actual R Script Code, Scale Interval is missing, when a subset of data is filtered, nobody knows the interval…When the whole dataset is selected, the y-scale interval is ok (interval by=0.5), the minor breaks is not showed


in Excel or PowerBi the scaling of the Y-axis adapts dynamically to the selected data / subset. I am looking for such a dynamic scaling

Thanks for helping me.

As I said, please provide a proper reproducible example as explained in the link I gave you before.

Hi

i solved it with this if condition:
{
if((max(dataset$Betragin/1000000)<1))
# || (min(dataset$Betragout/1000000)>-1))
{
scale_y_continuous(breaks = seq(-100000,100000, by=0.05))
} else {
scale_y_continuous(breaks = seq(-100000,100000, by=0.5), minor_breaks=seq(-100000,100000, by=0.1))
}
}+

But now, it is veeeeeery slowly. is there a possibilitiy to optimize the code? I tried the ifelse condition:
ifelse(dataset$Betragin/1000000<1|| dataset$Betragout/1000000>-1,
scale_y_continuous(breaks = seq(-100000,100000, by=0.05)),
scale_y_continuous(breaks = seq(-100000,100000, by=0.5), minor_breaks=seq(-100000,100000, by=0.1)))

but then the following error applied, which i don't understand:
Fehler in rep(yes, length.out = len) :
attempt to replicate an object of type 'environment'

Can anybody help with the optimization?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.