0

I'm trying to make a Caterpillar plot from the confident intervals of a series of glmm outputs. So, I made a simple table (tabla) with the Index names (8) the Parameters (3 for each index) and the Intervals (2 values for each Parameter = min_value - max_value). I 'd like my plot to be grouped by Index and by the 3 parameters of each index in the Y-axis.

I tried out with ggplot but my poor knowledge did not let me continue.

my code :

library(ggplot2)
read.csv("ggplot_modelos.csv", header = TRUE, sep = ";", dec = ".")-> tabla

View(tabla)

names(tabla)

ggplot(tabla, aes(Intervalls, Index) , geom_bar(aes(Intervals, Parameter)))

My data:

tabla <-
data.frame(
  stringsAsFactors = FALSE,
             Index = c("H","H","H","H","H","H",
                       "ADI","ADI","ADI","ADI","ADI","ADI","AEI","AEI",
                       "AEI","AEI","AEI","AEI","BIO","BIO","BIO","BIO",
                       "BIO","BIO","ACI","ACI","ACI","ACI","ACI","ACI","M",
                       "M","M","M","M","M","Ht","Ht","Ht","Ht","Ht",
                       "Ht","AR","AR","AR","AR","AR","AR"),
         Parameter = c("Intercept","Intercept",
                       "Species Richness","Species Richness","Relative Abundance",
                       "Relative Abundance","Intercept","Intercept",
                       "Species Richness","Species Richness","Relative Abundance",
                       "Relative Abundance","(Intercept)","(Intercept)",
                       "Species Richness","Species Richness","Relative Abundance",
                       "Relative Abundance","(Intercept)","(Intercept)",
                       "Species Richness","Species Richness","Relative Abundance",
                       "Relative Abundance","(Intercept)","(Intercept)",
                       "Species Richness","Species Richness","Relative Abundance",
                       "Relative Abundance","(Intercept)","(Intercept)",
                       "Species Richness","Species Richness","Relative Abundance",
                       "Relative Abundance","(Intercept)","(Intercept)",
                       "Species Richness","Species Richness","Relative Abundance",
                       "Relative Abundance","(Intercept)","(Intercept)",
                       "Species Richness","Species Richness","Relative Abundance",
                       "Relative Abundance"),
        Intervalls = c(-0.63399094,0.10088021,
                       0.05111166,0.11374545,0.01356644,0.03568559,-0.63399094,
                       0.10088021,0.05111166,0.11374545,0.01356644,0.03568559,
                       -0.08921982,0.69843374,-0.11735036,-0.05706148,
                       -0.03492262,-0.01363224,-0.387777431,0.8894038,-0.01656506,
                       0.04067182,-0.004208021,0.01583599,-1.42348399,
                       0.78640078,0.13746715,0.21376064,0.06880861,0.09518447,
                       -0.44656228,0.46793251,0.03039887,0.05458163,
                       0.05991785,0.12952663,-0.384061,1.50738256,-0.2298068,
                       -0.1467239,-0.106336,-0.07786533,-0.54823458,1.23599168,
                       -0.04350877,-0.01813159,-0.09811032,-0.02568451)
)

I will appreciate if anyone can give me a hand here!

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Martin
  • 13
  • 5
  • Typically it's `ggplot(data, aes(x, y))` first (notice the parens close), and then add (`+`) each component/layer/theme element, such as your `geom_bar`. Over-simplified, it often looks like `ggplot(...,aes(...)) + geom_bar(...)`. – r2evans Jul 29 '20 at 22:35
  • 1
    Maybe this can help: https://stackoverflow.com/questions/34344599/a-caterpillar-plot-of-just-the-significant-random-effects-from-a-mixed-effects/34345877#34345877. And are you sure you want `geom_bar`? – Edward Jul 29 '20 at 22:40

1 Answers1

0

Your data needs reshaped before plotting. The lower and upper end of the ranges should be in different columns rather than consecutive rows. You probably also want to use segments instead of columns:

library(tidyr)
library(dplyr)
library(ggplot2)

p <- tabla %>%
  mutate(type = rep(c("min", "max"), nrow(tabla)/2)) %>%
  pivot_wider(names_from = type, values_from = Intervalls) %>%
  mutate(Parameter = gsub("\\(Intercept\\)", "Intercept", Parameter)) %>%
  ggplot() + 
  geom_segment(aes(y = Parameter, yend = Parameter, x = min, xend = max,
                   colour = Parameter), size = 2) +
  scale_color_manual(values = c("gray90", "deepskyblue4", "orange")) +
  facet_grid(Index~., switch = "y") +
  scale_y_discrete(expand = c(0.5, 0.5)) +
  theme_classic() +
  labs(x = "Coefficient") +
  theme(panel.grid.major.x = element_line(),
        strip.text.y.left = element_text(angle = 0, size = 14, face = 2),
        strip.placement = "outside",
        strip.background = element_blank(),
        axis.text.y = element_blank())

p

enter image description here

You might also want to "zoom in" on the x axis to see the coefficients better while sacrificing being able to see the full intercept ranges:

p + coord_cartesian(xlim = c(-0.25, 0.25))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87