0

I have the following plot current plot which I want to flip 90 degrees so that it looks like this desired plot

Here is the code for the current plot:

library(tidypaleo)
library(tidyverse)
theme_set(theme_bw(8))


regforamcounts<-pivot_longer(regforamcounts,cols=c(-Sample,-SWLI,-Site), names_to="species", 
values_to = "rel_abund")


regforamcounts$Site <- as.character(regforamcounts$Site)
P1<-ggplot(regforamcounts, aes(x = SWLI,  y = rel_abund,group=1,fill=Site)) +
  geom_col(position="identity",width=0.5) +
  facet_abundance(vars(species)) +
  labs(x = "SWLI (m)", y = "Relative abundance")+
  theme(panel.grid.major = element_blank())+
  theme(panel.grid.minor = element_blank())+
  scale_fill_manual(values=c("#01216D","#5C95B8","#DAA585"))+geom_vline(xintercept =c(200,100))

P1

I have tried using coord_flip() but it doesn't work.I have also tried changing the code to :

p1 <- ggplot(regforamcounts, aes(x = rel_abund, y = SWLI,fill=Site)) +
  geom_colh(width=0.15) +
  scale_y_reverse() +
  facet_abundanceh(vars(species)) +
  labs(x = "Relative abundance (%)", y = "SWLI (m AHD)")

p1

But I get the error:

position_stackv requires non-overlapping y intervals 

I think the answer lies somewhere between the two!

Appreciate any help!

structure(list(Sample = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("LG1", 
"LG120", "LG130", "LG135", "LG160", "LG170", "LG185", "LG2", 
"LG225", "LG230", "LG240", "LG245", "LG255", "LG260", "LG275", 
"LG280", "LG285", "LG290", "LG295", "LG3", "LG305", "LG315", 
"LG32", "LG36", "LG38", "LG4", "LG48", "LG5", "LG60", "LG7", 
"LSP010", "LSP020", "LSP030", "LSP040", "LSP050", "LSP060", "LSP070", 
"LSP080", "LSP089", "LSP100", "LSP110", "LSP120", "LSP130", "LSP140", 
"LSP150", "LSP160", "LSP165", "ST-2LG0", "ST-2LG100", "ST-2LG120", 
"ST-2LG140", "ST-2LG160", "ST-2LG190", "ST-2LG40", "ST-2LG60", 
"ST-2LG80", "T3LB11.301", "T3LB12.05", "T3LB12.844", "T3LB13.87", 
"T3LB14.51", "T3LB14.63", "T3LB15.321", "T3LB15.59", "T3LB15.95", 
"T3LB16.69", "T3LB18.226", "T3LB19.762", "T3LB21.078", "T3LB26.256", 
"T3LB28.57", "T3LB28.84", "T3LB29.03", "T3LB31.056", "T3LB31.365", 
"T3LB7.008", "T3LB7.18", "T3LB7.303", "T3LB7.5", "T3LB7.9", "T3LB8.73", 
"T3LB9.45", "WAP 0 ST-2", "WAP 10 ST-2", "WAP 110 ST1", "WAP 120 ST-1", 
"WAP 122 ST-1", "WAP 125 ST1", "WAP 130 ST1", "WAP 135 ST-1", 
"WAP 140 ST-1", "WAP 144 ST-1", "WAP 150 ST-1 ", "WAP 155 ST-1", 
"WAP 159 ST1", "WAP 160 ST-1", "WAP 170 ST-1", "WAP 175 ST 1", 
"WAP 180 ST-1", "WAP 190 ST-1", "WAP 200 ST-1", "WAP 210 ST-1", 
"WAP 230 ST-1", "WAP 240 ST-1", "WAP 25 ST-2", "WAP 40 ST-2", 
"WAP 45 ST-2", "WAP 5  ST-2", "WAP 50 ST-2", "WAP 55 ST-2", "WAP 60 ST-1", 
"WAP 60 ST-2"), class = "factor"), SWLI = c(177.4585635, 177.4585635, 
177.4585635, 177.4585635, 177.4585635, 177.4585635), Site = c("1", 
"1", "1", "1", "1", "1"), species = c("AT.salsa", "BH.wilberti", 
"CT.irregularis", "DP.ipohalina", "E.macrescens", "FT.inflata"
), rel_abund = c(0, 0, 1.7, 0, 12.9, 83.6)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))
Ritchie Sacramento
  • 29,890
  • 4
  • 48
  • 56
Sophie Williams
  • 338
  • 1
  • 3
  • 14
  • Please check this: https://stackoverflow.com/questions/24312713/rotate-a-ggplot2-plot-object – Duck Sep 04 '20 at 12:33
  • Hi there, I already tried print(P1, vp=viewport(angle=-90)) but it did not work. Am I correct in using P1 rather than p as they did? – Sophie Williams Sep 04 '20 at 12:36

1 Answers1

1

It appears you need to switch all xs with ys and viceversa. Included geom_vline to geom_hline.

ggplot(regforamcounts, aes(y = SWLI,  x = rel_abund,group=1,fill=Site)) +
 geom_colh(position="identity",width=0.5) +
 facet_abundanceh(vars(species)) +
 labs(y = "SWLI (m)", x = "Relative abundance")+
 theme(panel.grid.major = element_blank())+
 theme(panel.grid.minor = element_blank())+
 scale_fill_manual(values=c("#01216D","#5C95B8","#DAA585"))+
 geom_hline(yintercept =c(200,100))

With the data you provided, this is the output:

enter image description here

Edo
  • 7,567
  • 2
  • 9
  • 19
  • Hi - yeah this works we just have to add h to geom_col - thanks :) – Sophie Williams Sep 04 '20 at 12:47
  • I corrected with `geom_colh`. Sorry, without the whole data it was difficult to see it. – Edo Sep 04 '20 at 12:49
  • Oh I just noticed the axes are the wrong way round. The code I used where it worked as I wanted was : `p1 <- ggplot(regforamcounts, aes(x = rel_abund, y = SWLI,fill=Site))+ geom_colh(width=0.5)+ facet_abundanceh(vars(species))+ labs(x = "Relative abundance (%)", y = "SWLI (m AHD)")+ geom_hline(yintercept =c(200,100))+ scale_fill_manual(values=c("#01216D","#5C95B8","#DAA585"))+ theme(panel.grid.major = element_blank())+ theme(panel.grid.minor = element_blank()) p1` – Sophie Williams Sep 04 '20 at 12:53
  • I noticed that in the plot I pasted I forgot the hline even if it was there in the code.. Now I think it's the same you did right? – Edo Sep 04 '20 at 13:01
  • Yes, it is the same now. Thank you for your help :) – Sophie Williams Sep 04 '20 at 13:23