我正在努力地用ggplot2绘制图表.下面,您可以看到我得到的输出和相关代码: enter image description here

ggplot(resu_fac_final, aes(x = reorder(species, Delta), y = Delta)) +
  geom_bar(stat = "identity",
           show.legend = FALSE,
           fill = color,     
           color = "white") +
  geom_hline(yintercept = 0, color = 1, lwd = 0.2) +
  geom_text(aes(label = round(Delta, 2), # Text with groups
                hjust = ifelse(Delta < 0, 1.5, -1),
                vjust = 0.5), size = 2.5) +
  xlab("Species") +
  ylab("Expansion and contraction of the climatic niche") +
  coord_flip() +
  theme_minimal()

我希望有一个图表,显示这些蝙蝠物种的分布在不同时期的扩张或收缩.然而,我得到的是一张图表,它确实解释了这些结果,但不是以这样一种说教的方式.有了这些 idea ,我希望它能增强读者的理解力.我在这个论坛上的各种帖子中寻找可以帮助我的东西,但我没有找到.

我想要的是设置限制,可能是使用Scale_x_Display(),但我没有使用它来管理它,以便将x轴从0分隔到2,这样我就可以在左侧使用粉色的值,在右侧使用蓝色的值,如下图所示.此外,我想通过删除下划线并将其设置为斜体来编辑y轴标签.

enter image description here

这是resu_fac_final的dput:

dput(resu_fac_final[1:10, ])
structure(list(species = c("Artibeus_cinereus", "Artibeus_concolor", 
"Artibeus_gnomus", "Artibeus_jamaicensis", "Artibeus_lituratus", 
"Artibeus_phaeotis", "Artibeus_planirostris", "Artibeus_toltecus", 
"Artibeus_watsoni", "Carollia_brevicaudum"), iniDist2 = c(98095, 
69677, 43781, 140175, 157200, 52678, 118224, 15658, 13574, 113735
), iniDist = c(111511, 71610, 61954, 129815, 132053, 50888, 127966, 
22795, 15096, 119665), Delta = c(0.87968899929155, 0.973006563329144, 
0.70666946444136, 1.07980587759504, 1.19043111477967, 1.03517528690457, 
0.923870403075817, 0.686905023031367, 0.899178590355061, 0.950444992270087
)), row.names = c(NA, 10L), class = "data.frame")

有没有可能以某种方式做呢?

预先感谢你的每一次最终帮助.

推荐答案

我认为这是您想要做的,或者只是做了一些微小的调整:

ggplot(resu_fac_final, aes(
    ## swap x and y to get rid of coord_flip
    y = reorder(species, Delta), 
    x = Delta - 1,    ## we will subtract 1 from all x values
    fill = Delta < 1  ## set fill color dynamically based on delta
  )) +
  geom_bar(stat = "identity",
           show.legend = FALSE,
           color = "white") +
  geom_vline(xintercept = 0, color = 1, lwd = 0.2) +
  geom_text(aes(label = round(Delta, 2), # Text with groups
                hjust = ifelse(Delta < 1, 1.5, -1), ## changed to `Delta < 1`
                vjust = 0.5), size = 2.5) +
  labs(
    x = "Expansion and contraction of the climatic niche",
    y = "Species"
  ) +
  ## use a label function to replace underscores with spaces
  scale_y_discrete(labels = \(x) sub(pattern = "_", replacement = " ", x, fixed = TRUE)) +
  ## set the x scale to add 1 to the labels, undoing the transform above
  ## and set the limits accordingly to the transformed x values
  scale_x_continuous(labels = \(x) x + 1, limits = c(-1, 1)) +
  ## guess at your colors
  scale_fill_manual(values = scales::alpha(c("lightskyblue", "pink"), 0.8)) +
  theme_minimal() +
  theme(
    ## italicize y labels
    axis.text.y = element_text(face = "italic"),
    ## I like to get rid of horizontal grids on plots like this
    panel.grid.major.y = element_blank()
  )

enter image description here

R相关问答推荐

如何在R中正确对齐放射状图中的文本

如何 bootstrap glm回归、估计95%置信区间并绘制它?

如何求解arg必须为NULL或deSolve包的ode函数中的字符向量错误

如何计算前一行的值,直到达到标准?

用derrr在R中查找组间的重复项

然后根据不同的列值有条件地执行函数

如何直接从R中的风险分数计算c指数?

Rplotly中的Sankey Diagram:意外连接&

在R中为马赛克图中的每个字段着色

标识R中多个列中缺少的唯一值

根据1个变量绘制 colored颜色 发散的 map ,由另一个变量绘制饱和度,ggplot2不工作

列名具有特殊字符时的循环回归

将项粘贴到向量中,并将它们分组为x的倍数,用空格分隔

从数据创建数字的命名列表.R中的框

为什么函数toTitleCase不能处理english(1),而toupper可以?

Rmarkdown::Render vs Source()

数据集上的R循环和存储模型系数

如何在R中使用因子行求和?

为什么在POSIXct-times的向量上循环会改变R中的类型?

从多行中 Select 最小值