Às 10:29 de 24/11/2023, sibylle.stoec...@gmx.ch escreveu:
Dear R-user

Does anybody now, if ggplot allows to use two x-axis including two
dimensions (similar to excel plot (picture 1 in the pdf attachmet). If yes,
how should I adapt my code? The parameters are presented in the input file
(attachment: Input).

Fig2b = read.delim("BFF_Fig-2b.txt", na.strings="NA")
names(Fig2b)
head(Fig2b)
summary(Fig2b)
str(Fig2b)
Fig2b$Aspekt<-factor(Fig2b$Aspekt, levels=(c("Voegel", "Kleinsaeuger",
"Schnecken", "Regenwuermer_Asseln", "Pilze")))

### Figure 2b
   ggplot(Fig2b,aes(Aspekt,Wert,fill=Effekt))+
     geom_bar(stat="identity",position='fill')+
     scale_y_continuous(limits=c(0,14), expand=c(0,0))+
     labs(x="", y="Anzahl Studien pro Effekt")

Kind regards
Sibylle


______________________________________________
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,

The first attached file does not match the data in the second file but here is an answer to both this question and to your other question [1].

The trick to have a secondary axis is to compute a ratio of axis lenghts. The lengths of the main and secondary axis can be computed by functions range() and diff(), like in the code below. Then use it to scale the secondary axis.



Fig2b <-
  structure(list(
    Aspekt = c("Flora", "Flora", "Flora", "Tagfalter",
"Tagfalter", "Tagfalter", "Heuschre", "Heuschre", "Heuschre", "Kaefer_Sp", "Kaefer_Sp", "Kaefer_Sp", "Schwebfli", "Schwebfli", "Schwebfli", "Bienen_F", "Bienen_F", "Bienen_F", "Flora", "Flora", "Flora", "Tagfalter", "Tagfalter", "Tagfalter", "Heuschre", "Heuschre", "Heuschre", "Kaefer_Sp", "Kaefer_Sp", "Kaefer_Sp", "Schwebfli", "Schwebfli", "Schwebfli", "Bienen_F", "Bienen_F", "Bienen_F"),
    BFF = c("BB", "SA", "NE", "BB", "SA", "NE", "BB", "SA", "NE",
            "BB", "SA", "NE", "BB", "SA", "NE", "BB", "SA", "NE", "BB",
            "SA", "NE", "BB", "SA", "NE", "BB", "SA", "NE", "BB", "SA",
            "NE", "BB", "SA", "NE", "BB", "SA", "NE"),
    Effekt = c("Neu",
"Neu", "Neu", "Neu", "Neu", "Neu", "Neu", "Neu", "Neu", "Neu", "Neu", "Neu", "Neu", "Neu", "Neu", "Neu", "Neu", "Neu", "Pos", "Pos", "Pos", "Pos", "Pos", "Pos", "Pos", "Pos", "Pos", "Pos",
               "Pos", "Pos", "Pos", "Pos", "Pos", "Pos", "Pos", "Pos"),
    Wert = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 3L, 1L, 1L,
             0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 2L, 1L, 0L, 0L, 1L, 0L,
             9L, 4L, 6L, 0L, 0L, 3L, 0L, 0L, 4L)),
    row.names = c(NA, -36L), class = "data.frame")


library(ggplot2)

# First y axis (0-9)
# Second y axis (0-2500)
# fac <- diff(range( sec axis ))/diff(range( 1st axis ))
fac <- diff(range(0, 2500))/diff(range(0, 9))

ggplot(Fig2b, aes(Aspekt, Wert, fill = Effekt)) +
  geom_col(position = position_dodge()) +
  scale_y_continuous(
    breaks = seq(0, 12, 2L),
    sec.axis = sec_axis(~ . * fac)
  ) +
  labs(x = "", y = "Anzahl Studien pro Effekt")




[1] https://stat.ethz.ch/pipermail/r-help/2023-November/478605.html

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