我有一个类似的数据框:
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文档说明:"如果您希望条形的高度代表数据中的值,请使用geom_col而不是#34;这正是我想要的。
我使用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表示它从数据中获得条形的高度......
我显然缺少一些基本的东西,所以任何指针都会受到赞赏。
答案 0 :(得分:1)
我将您的数据整理成整齐的格式,然后使用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")
答案 1 :(得分:1)
您必须将数据从宽格式转换为长格式,例如我的示例中的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)