带对数 Y 轴的条形图

问题描述 投票:0回答:1

我有一个数据框:

tibble [56 x 3] (S3: tbl_df/tbl/data.frame)
$ Species    : Factor w/ 56 levels "OC1","OC2","OC3",..: 1 2 3 4 5 6 7 8 9 10 ...
$ Residential: num [1:56] 0 0.077 0.0997 0.1023 0.2268 ...
$ FW_AVG     : num [1:56] 0.022 0.0944 0.1121 0.0641 0.0659 ...

我需要绘制一个条形图,将 X 轴上的物种和 Y 轴上的 FW_AVG 显示为条形图,将 Y 轴上的住宅值显示为散点图,反之亦然。然而,我正在尝试绘制条形图,但我得到的条形方向是颠倒的。

ggplot(data, aes(x = Species, y = FW_AVG)) +
geom_bar(stat = "identity", fill = "steelblue", position = position_dodge(width = 0.7)) +
scale_y_log10() +
labs(x = "Species", y = "FW_AVG (log scale)") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))'

图表是(带有蓝色条形图的图表)

但是我需要该图作为参考图

任何人都可以帮我找到代码中的错误吗?

ggplot2 bar-chart scale point
1个回答
0
投票

本质上,问题在于

ggplot
在绘制数字方面在技术上是正确的。由于 10^0 = 1,从技术上讲,您的最高值 1 现在是绘图上的 0,而低于 1 的值是负数(10 的负数次方)。您想要通过在任意点添加底线来重现“作弊”的图表。

这个作弊可以在 R 中重现。使用

scales::pseudo_log_trans()
使其看起来像对数刻度:

library(tidyverse)
library(scales)

data <- tibble(Species = letters,
               FW_AVG = (runif(26)) ^ 4)


ggplot(data, aes(x = Species, y = FW_AVG)) + geom_bar(
  stat = "identity",
  fill = "steelblue",
  position = position_dodge(width = 0.7)
) +
  scale_y_continuous(
    trans = pseudo_log_trans(sigma = 10^-3, base = 10),
    breaks = 10 ^ -seq(3, 0),
    labels = label_scientific()
  ) +
  labs(x = "Species", y = "FW_AVG (log scale)") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

© www.soinside.com 2019 - 2024. All rights reserved.