【问题标题】:Caterpilar Plot in RR中的毛毛虫图
【发布时间】:2020-07-29 21:04:03
【问题描述】:

我正在尝试根据一系列 glmm 输出的置信区间制作卡特彼勒图。 因此,我制作了一个简单的表(tabla),其中包含索引名称(8)、参数(每个索引 3 个)和间隔(每个参数 2 个值 = min_value - max_value)。我希望我的绘图按索引和 Y 轴上每个索引的 3 个参数进行分组。

我尝试使用 ggplot,但我的知识不足让我无法继续。

我的代码:

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)))

我的数据:

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)
)

如果有人能帮我一下,我将不胜感激!

【问题讨论】:

  • 通常首先是ggplot(data, aes(x, y))(注意括号关闭),然后添加(+)每个组件/层/主题元素,例如您的geom_bar。过于简化,通常看起来像ggplot(...,aes(...)) + geom_bar(...)。
  • 也许这会有所帮助:stackoverflow.com/questions/34344599/…。你确定要geom_bar?

标签: r ggplot2 plot


【解决方案1】:

您的数据需要在绘图前重新整形。范围的下端和上端应该在不同的列中,而不是在连续的行中。您可能还想使用段而不是列:

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

您可能还想在 x 轴上“放大”以更好地查看系数,同时牺牲能够看到完整的截距范围:

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

【讨论】:

  • 伟大的艾伦!非常感谢你。你澄清了我的阴谋
猜你喜欢
  • 2011-04-04
  • 2016-03-24
  • 1970-01-01
  • 1970-01-01
  • 2018-08-22
  • 1970-01-01
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多