ggplot geom_col:根据数据自动定义 y?

ggplot geom_col: automatically defining y from data?

我有一个看起来像这样的数据框:

A           B           C
0,868385346 0,628248588 0,468926554
0,074626866 0,277966102 0,271186441
0,024423338 0,057627119 0,203389831
0,017639077 0,007909605 0,011299435
0,004070556 0,007909605 0,011299435
0,004070556 0,005649718 0,011299435
0,002713704 0,003389831 0,005649718
0,001356852 0,001129944 0,005649718
0,001356852 0,001129944 0,005649718
0,001356852 0,001129944 0,005649718
            0,001129944 
            0,001129944 
            0,001129944 
            0,001129944 
            0,001129944 
            0,001129944 
            0,001129944 

这些是A、B、C的组成比例(数字相加为1,数字最大的在最上面)

我想制作一个在 x 轴上带有 A、B、C 的条形图(或多面体,但我稍后会看到),并为每个条形图显示实际数据(因此对于 A,十个显示比例的条,第一个是 0.86,第二个是 0.07,等等)以比较构图中的不同分布。

ggplot 文档指出:"If you want the heights of the bars to represent values in the data, use geom_col instead" 这正是我想要的。

我运行以下na.omit因为不同的列有不同的行数

ggplot(na.omit(data)) + geom_col()

我收到以下错误: pmin(y, 0) 错误:未找到对象 'y'

我看到我必须分配一个 y(在 geom_bar 文档中,因为它似乎 geom_col 没有自己的文档)。我尝试了各种方法来获得从 0 到 1 的比例,例如 y=c(0:1),但似乎没有任何效果。

我仍然不明白如何分配 y 轴,而函数 geom_col 说它根据数据生成条形高度...

我显然遗漏了一些基本的东西,所以任何指点都将不胜感激。

我将你的数据整理成整齐的格式,然后使用 geom_col()。我必须将 y 轴转换为 factor 变量,以便条形图显示值的实际身份。您也可以使用 geom_bar(stat = "identity").

# double check that these values are correct, I wrote this quickly
A <- c(0.868385346
       ,0.07626866
       ,0.024423338
       ,0.017639077
       ,0.004070556
       ,0.004070556
       ,0.002713704
       ,0.001356852
       ,0.001356852
       ,0.001356852
       ,NA
       ,NA
       ,NA
       ,NA
       ,NA
       ,NA
       ,NA)


B <- c(0.628248588
       ,0.277966102
       ,0.057627119
       ,0.007909605
       ,0.007909605
       ,0.005649718
       ,0.003389831
       ,0.001129944
       ,0.001129944
       ,0.001129944
       ,0.001129944
       ,0.001129944
       ,0.001129944
       ,0.001129944
       ,0.001129944
       ,0.001129944
       ,0.001129944)


C <- c(0.468926554
       ,0.271186441
       ,0.203389831
       ,0.011299435
       ,0.011299435
       ,0.011299435
       ,0.005649718
       ,0.005649718
       ,0.005649718
       ,0.005649718
       ,NA
       ,NA
       ,NA
       ,NA
       ,NA
       ,NA
       ,NA)


# combine all three vectors into a dataframe
df_wide <- data.frame(A,B,C)

# convert to tidy format
df <- gather(df_wide, id, value) %>% na.omit()


# create our plot
ggplot(df, aes(x = as.factor(id), y = as.factor(value), fill = id)) + 
  geom_bar(position = "dodge", stat = "identity")

您必须将数据从宽格式转换为长格式,例如我示例中的 dat2。您还需要创建一个 ID 列。之后,您可以使用 geom_col 绘制条形图。在下面的代码示例中,我还展示了如何在 y 轴上设置限制并使用 facet_grid

library(tidyverse)

dat2 <- dat %>% 
  mutate(ID = 1:n()) %>%
  gather(Column, Value, -ID)

ggplot(dat2, aes(x = ID, y = Value)) +
  geom_col() +
  scale_y_continuous(limits = c(0, 1)) +
  facet_grid(Column ~ .) +
  theme_bw()

数据

dat <- read.table(text = "A           B           C
0.868385346 0.628248588 0.468926554
0.074626866 0.277966102 0.271186441
0.024423338 0.057627119 0.203389831
0.017639077 0.007909605 0.011299435
0.004070556 0.007909605 0.011299435
0.004070556 0.005649718 0.011299435
0.002713704 0.003389831 0.005649718
0.001356852 0.001129944 0.005649718
0.001356852 0.001129944 0.005649718
0.001356852 0.001129944 0.005649718
NA          0.001129944 NA 
NA          0.001129944 NA
NA          0.001129944 NA
NA          0.001129944 NA
NA          0.001129944 NA
NA          0.001129944 NA
NA          0.001129944 NA"
                  , header = TRUE)