亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

R中的堆積條形圖

R中的堆積條形圖

吃雞游戲 2019-12-04 10:00:23
我已經看過關于R中堆積條形圖的類似問題,但是我仍然沒有運氣。我創建了以下數據框:        A   B   C   D   E   F    G     1 480 780 431 295 670 360  190     2 720 350 377 255 340 615  345     3 460 480 179 560  60 735 1260     4 220 240 876 789 820 100   75A:G代表x軸,y軸為持續時間(秒)。我將如何在R中堆疊以下數據?非常感謝您的時間和幫助。
查看完整描述

3 回答

?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

數據集:


dat <- read.table(text = "A   B   C   D   E   F    G

1 480 780 431 295 670 360  190

2 720 350 377 255 340 615  345

3 460 480 179 560  60 735 1260

4 220 240 876 789 820 100   75", header = TRUE)

現在,您可以將數據框轉換為矩陣并使用該barplot函數。


barplot(as.matrix(dat))


查看完整回答
反對 回復 2019-12-04
?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

使用ggplot2的方法有些不同:


dat <- read.table(text = "A   B   C   D   E   F    G

1 480 780 431 295 670 360  190

2 720 350 377 255 340 615  345

3 460 480 179 560  60 735 1260

4 220 240 876 789 820 100   75", header = TRUE)


library(reshape2)


dat$row <- seq_len(nrow(dat))

dat2 <- melt(dat, id.vars = "row")


library(ggplot2)


ggplot(dat2, aes(x = variable, y = value, fill = row)) + 

  geom_bar(stat = "identity") +

  xlab("\nType") +

  ylab("Time\n") +

  guides(fill = FALSE) +

  theme_bw()

這給出了:


如果要包含圖例,請刪除該guides(fill = FALSE)行。


查看完整回答
反對 回復 2019-12-04
?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

我顯然不是一個很好的R編碼器,但是如果您想用ggplot2做到這一點:


data<- rbind(c(480, 780, 431, 295, 670, 360,  190),

             c(720, 350, 377, 255, 340, 615,  345),

             c(460, 480, 179, 560,  60, 735, 1260),

             c(220, 240, 876, 789, 820, 100,   75))


a <- cbind(data[, 1], 1, c(1:4))

b <- cbind(data[, 2], 2, c(1:4))

c <- cbind(data[, 3], 3, c(1:4))

d <- cbind(data[, 4], 4, c(1:4))

e <- cbind(data[, 5], 5, c(1:4))

f <- cbind(data[, 6], 6, c(1:4))

g <- cbind(data[, 7], 7, c(1:4))


data           <- as.data.frame(rbind(a, b, c, d, e, f, g))

colnames(data) <-c("Time", "Type", "Group")

data$Type      <- factor(data$Type, labels = c("A", "B", "C", "D", "E", "F", "G"))


library(ggplot2)


ggplot(data = data, aes(x = Type, y = Time, fill = Group)) + 

       geom_bar(stat = "identity") +

       opts(legend.position = "none")


查看完整回答
反對 回復 2019-12-04
  • 3 回答
  • 0 關注
  • 905 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號