0

I'd like to plot bar-plot with ggplot2 and face difficulties in formatting the numbers of the scale. Here is what I am doing (File to be plotted is ZW1 as grouped barplot, variable TV is y, DisIK2 (discretised with the cut-statement) is x and Ueber_RRG variable for grouping the bars):

 breaks <- c(seq(from=0, to=0.375, by=0.025))
 ZW1$DisIK2 <- cut(Fonds_T$IK2,breaks=c(breaks,Inf),labels=as.numeric(breaks))
  ...
  ggplot(ZW1[(ZW1$Groessenklasse == "Alle"),],aes(fill=Ueber_RRG,y=TV,x=DisIK2)) +
   geom_bar(position="dodge",stat="identity",na.rm=TRUE) + xlab("xlab --- tbd") +ylab("ylab --- tbd") + 
   theme(legend.position = c(.95, .95), legend.justification = c("right", "top"), legend.box.just = "right", legend.margin = margin(6, 6, 6, 6)) +
   scale_fill_discrete(name="Ueber/unter RRG")

Here is what I get:

Output of Plot

What I want is to have numbers at the x-axis to three digits after the decimal point each (ie. 0.000, 0.025, 0.050, 0.075 0.100 etc.)

Bonus question: What do I have to do to format values of Ueber_RRG in the legend to get a plot like:

Plot with formatted variable-values (see in legend)

What works is

ZW1$Ueber_RRG_Factor <- factor(ZW1$Ueber_RRG,levels=c(TRUE, FALSE), labels=c("> RRG", "< RRG"))
  ...
 ggplot(ZW1[(ZW1$Groessenklasse == "Alle"),],aes(fill=Ueber_RRG_Factor,y=TV,x=DisIK2)) +
   geom_bar(position="dodge",stat="identity",na.rm=TRUE) + xlab("xlab --- tbd") +ylab("ylab --- tbd") + 
   theme(legend.position = c(.95, .95), legend.justification = c("right", "top"), legend.box.just = "right", legend.margin = margin(6, 6, 6, 6)) +
   scale_fill_discrete(name="Ueber/unter RRG")

but at the extense of an additional variable (Ueber_RRG_Factor). Is there a way to assign these formatted values to the oiginal variable? (A similar question was asked already, but unfortunatly was not answered)

Some remarks: This question does not answer my question. The solution refers to scale_x_continuous(), my variable, however, is discrete. Subsequently an error message is delivered:

Error: Discrete value supplied to continuous scale

Thanks already for your support.

GIC
  • 3
  • 2
  • 1
    `+scale_x_continuous(labels = scales::label_number(accuracy = 0.001))` for the decimal points. Simlarly for the `fill` legend labels, use the `labels` argument: add to `scale_fill_discrete` the argument `labels = c("> RRG", "< RRG")` – Gregor Thomas Jan 10 '22 at 16:19
  • 1
    Without any of your data, we have no way of knowing why that other post would or wouldn't have you covered. [See here](https://stackoverflow.com/q/5963269/5325862) on making a reproducible example that is easier for folks to help with. – camille Jan 10 '22 at 19:59

0 Answers0