我有一个相对较大的数据集,我正在尝试将其绘制到R中。这就是我的数据集的片段的样子
> dat
Who Activity Duration
1 Ch Stationary 14
2 Ch Stationary 18
3 Ch Interaction 2
4 Ch Stationary 6
5 Ch Interaction 1
6 Ch Display 10
7 Ch Interaction 6
8 Ch Stationary 5
9 Ch Stationary 20
10 Ch Stationary 13
11 Ch Stationary 17
12 Co Interaction 3
13 Co Stationary 31
14 J Stationary 8
15 R OOS 1
16 R Interaction 1
17 J OOS 4
我正在尝试使用持续时间而不是原始值作为百分比来构建堆积的条形图。到目前为止,这是我想出的:
p7 <- ggplot() +
geom_bar(aes(y= dat$Duration, x= dat$Who, fill = dat$Activity),
stat ="identity")
p7 <- p7 + scale_fill_brewer(palette="Set3")
p7 <- p7 + xlab("Chimp ID") + ylab ("Total Time Spent (min)")
p7 <- p7 + labs(fill = "Behaviour")
p7 + theme_classic()
p7 #Palette graph with percentage values
p7 +
geom_bar(position = "fill", stat="identity") +
scale_y_continuous(labels = scales::percent_format())
但是,这仍然不会返回总计100%的小节。
答案 0 :(得分:0)
你好索非亚,
问题在于scales::percent_format()
仅使用百分号格式化数字。您需要自己计算百分比。
下面的代码为您计算出来。
library(tidyverse)
dat <- read.delim(text="id,Who,Activity,Duration
1,Ch,Stationary,14
2,Ch,Stationary,18
3,Ch,Interaction,2
4,Ch,Stationary,6
5,Ch,Interaction,1
6,Ch,Display,10
7,Ch,Interaction,6
8,Ch,Stationary,5
9,Ch,Stationary,20
10,Ch,Stationary,13
11,Ch,Stationary,17
12,Co,Interaction,3
13,Co,Stationary,31
14,J,Stationary,8
15,R,OOS,1
16,R,Interaction,1
17,J,OOS,4", sep=",")
dat <- dat%>%
group_by(Who)%>%
mutate(percent = Duration/sum(Duration))
p7 <- ggplot() +
geom_bar(aes(y= dat$percent, x= dat$Who, fill = dat$Activity),
stat ="identity")
p7 <- p7 + scale_fill_brewer(palette="Set3")
p7 <- p7 + xlab("Chimp ID") + ylab ("Total Time Spent (min)")
p7 <- p7 + labs(fill = "Behaviour")
p7 + theme_classic()
p7 #Palette graph with percentage values
p7 +
geom_bar(position = "fill", stat="identity") +
scale_y_continuous(labels = scales::percent_format())