[R] ggplot 3-dimensions

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sun Dec 17 14:08:45 CET 2023


Às 09:13 de 17/12/2023, SIBYLLE STÖCKLI via R-help escreveu:
> Dear R community
> 
> In the meantime I made some progress:
>    ggplot(data = Fig2b, aes(x = BFF, y = Wert, fill = Effekt))+theme_bw()+
>      geom_bar(stat = "identity", width = 0.95) +
>      scale_y_continuous(limits=c(0,13), expand=c(0,0))+
>      facet_wrap(~Aspekt, strip.position = "bottom", scales = "free_x") +
>      theme(panel.spacing = unit(0, "lines"),
>            strip.background = element_blank(),
>            strip.placement = "outside")+
>      theme(axis.title.x=element_blank())+
>      scale_fill_manual("Effekt", values = c("Neg" = "red", "Neu" =
> "darkgrey", "Pos" = "blue"), labels=c("Negativ", "Nicht sign.", "Positiv"))
>    
>    
> Question
> - Is it possible to present all the subpolots in one graph (not to "lines")?
> 
> - I tried to change the angel of the x-axis. However, I was able to change
> the first x-axis (BB...), but not the second one (Voegel....). Maybe this
> would solve the problem.
> - If not, is there another possibility to fix the number of subplots per
> line?
> 
> Kind regards
> Sibylle
> 
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of SIBYLLE STÖCKLI via
> R-help
> Sent: Saturday, December 16, 2023 12:16 PM
> To: R-help using r-project.org
> Subject: [R] ggplot 3-dimensions
> 
> 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 using 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,

You are posting the data as image once again, please don't do this.
Paste the output of

dput(Fig2b)            # if small data
dput(head(Fig2b, 20))  # if too big to fit in an e-mail


in your mails. Here it is.



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")
Aspekt <- c(Aspekt, Aspekt)
BFF <- rep(c("BB", "SA", "NE"), times = 12)
Effekt <- c(rep("Neg", times = 18), rep("Pos", times = 18))
Wert <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0,
           2, 1, 0, 0, 1, 0, 9, 4, 6, 0, 0, 3, 0, 0, 4)
Fig2b <- data.frame(Aspekt, BFF, Effekt, Wert)



As for the question, you can use facet_wrap argument nrow to have all 
plots in one row only, see the comment before facet_wrap. I don't know 
if this solves the problem.
Also, I define a custom theme to make the code clearer later.



library(ggplot2)

theme_sibylle <- function() {
   theme_bw(base_size = 10) %+replace%
     theme(
       panel.spacing = unit(0, "lines"),
       strip.background = element_blank(),
       strip.placement = "outside",
       # this line was added by me, remove if not wanted
       strip.text.x.bottom = element_text(face = "bold", size = 10),
       axis.title.x = element_blank()
     )
}

ggplot(data = Fig2b, aes(x = BFF, y = Wert, fill = Effekt)) +
   geom_bar(stat = "identity", width = 0.95) +
   scale_y_continuous(limits=c(0,13), expand=c(0,0)) +
   # here I use nrow = 1L to put everything in one row only
   facet_wrap(~ Aspekt, nrow = 1L, strip.position = "bottom", scales = 
"free_x") +
   scale_fill_manual(
     name = "Effekt",
     values = c("Neg" = "red", "Neu" = "darkgrey", "Pos" = "blue"),
     labels = c("Negativ", "Nicht sign.", "Positiv")) +
   theme_sibylle()



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



More information about the R-help mailing list