3 回答

TA貢獻(xiàn)1873條經(jīng)驗 獲得超9個贊
ggplot除了你到目前為止,還有一個使用該軟件包(版本3.x)的解決方案。
我們使用set 的position參數(shù)。如果要使用(和)的參數(shù),也可以使用。geom_barposition = "fill"position = position_fill()position_fill()vjustreverse
請注意,您的數(shù)據(jù)采用“寬”格式,但ggplot2要求采用“長”格式。因此,我們首先需要gather數(shù)據(jù)。
library(ggplot2)
library(dplyr)
library(tidyr)
dat <- read.table(text = " ONE TWO THREE
1 23 234 324
2 34 534 12
3 56 324 124
4 34 234 124
5 123 534 654",sep = "",header = TRUE)
# Add an id variable for the filled regions and reshape
datm <- dat %>%
mutate(ind = factor(row_number()) %>%
gather(variable, value, -ind)
ggplot(datm, aes(x = variable, y = value, fill = ind)) +
geom_bar(position = "fill",stat = "identity") +
# or:
# geom_bar(position = position_fill(), stat = "identity")
scale_y_continuous(labels = scales::percent_format())

TA貢獻(xiàn)1830條經(jīng)驗 獲得超3個贊
克里斯比利是嚴(yán)格的,你只需要列的比例。使用您的數(shù)據(jù)是:
your_matrix<-( rbind( c(23,234,324), c(34,534,12), c(56,324,124), c(34,234,124), c(123,534,654) ) ) barplot(prop.table(your_matrix, 2) )

TA貢獻(xiàn)1895條經(jīng)驗 獲得超7個贊
prop.table是獲得表格比例的友好方式。
m <- matrix(1:4,2) m [,1] [,2][1,] 1 3[2,] 2 4
保證金空白為您提供整個表格的比例
prop.table(m, margin=NULL) [,1] [,2][1,] 0.1 0.3[2,] 0.2 0.4
給它1給你行比例
prop.table(m, 1) [,1] [,2][1,] 0.2500000 0.7500000[2,] 0.3333333 0.6666667
2是列比例
prop.table(m, 2) [,1] [,2][1,] 0.3333333 0.4285714[2,] 0.6666667 0.5714286
- 3 回答
- 0 關(guān)注
- 837 瀏覽
添加回答
舉報