R:计算时间序列中列值的百分比

问题描述 投票:0回答:1

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

Connection    Account.Created.Month
Wired        12
Wired        12       
Wireless     13
Wired        13

我想创建一个图表,显示每个月有线与无线连接的百分比。例如,使用上面的数据,第12个月将为100%有线,第13个月将为50%有线。

这是我的绘图代码:

ea1wirelessUsed <- aggregate(data_ea1$Connection, list(Mon = data_ea1$Account.Created.Month), mean)

pea1 <- ggplot(ea1wirelessUsed, aes(x=Mon, y=x, fill=Mon)) + 
  geom_col() +
  ylab("Wired vs Wireless") +
  xlab("Time")
grid.arrange(pea1)

我不确定为x值传递什么以显示有线与无线连接的总百分比。

感谢您的帮助。

r aggregate
1个回答
0
投票

这里是使用tidyverse包的解决方案。我制作了一个countcomplete以正确的格式获取数据以进行绘图。

library(tidyverse)

data_ea1 %>%
  #Group by both vars
  group_by(Connection,Accoun.Created.Month) %>%
  #Count the number of connections by grouping variables
  count() %>%
  #ungroup
  ungroup() %>%
  #Complete the missing levels of "Connection" for each Month and fill
  #them with zeros
  complete(Connection,
           nesting(Accoun.Created.Month),
           fill = list(n = 0))%>%
  #Make the plot
  ggplot(aes(x=Accoun.Created.Month, 
             y=n, 
             fill=Connection)) + 
  geom_col(position = "fill") +
  scale_y_continuous(labels = scales::percent) +
  labs(y = "Percent", x = "Month")

Plot

© www.soinside.com 2019 - 2024. All rights reserved.