r - 条形图 dplyr 汇总值

标签 r plot dplyr

我有前 3 名排名的数据。我正在尝试创建一个图,该图在 x 轴上包含列名称(成本/产品),y 值是频率(理想情况下是相对频率,但我不确定如何在 dplyr 中获取该频率)。

我正在尝试根据 dplyr 中总结的值以情节方式创建此内容。我有一个 dplyr 数据框,看起来像这样:

likelyReasonFreq<-    LikelyRenew_Reason %>%
      filter(year==3)%>%
      filter(status==1)%>%
      summarize(costC = count(cost), 
                productsC = count(products))



   > likelyReasonFreq
          costC.x   costC.freq   productsC.x  productsC.freq
     1       1         10           1             31
     2       2         11           2             40
     3       3         17           3             30
     4      NA        149          NA             86

我正在尝试创建一个条形图,显示成本和产品的总(总和)频率。因此,成本频率将是排名 1、2 或 3 的次数的频率,即 38。本质上,我对第 1:3 行进行求和(对于产品,它将是 101(不包括 NA 值)。

我不知道该怎么做,有什么想法吗?

下面是变量 likelyReasonFreq

> dput(head(likelyReasonFreq))
 structure(list(costC = structure(list(x = c(1, 2, 3, NA), freq = c(10L, 
  11L, 17L, 149L)), .Names = c("x", "freq"), row.names = c(NA, 
  4L), class = "data.frame"), productsC = structure(list(x = c(1, 
  2, 3, NA), freq = c(31L, 40L, 30L, 86L)), .Names = c("x", "freq"
  ), row.names = c(NA, 4L), class = "data.frame")), .Names = c("costC", 
  "productsC"), row.names = c(NA, 4L), class = "data.frame")

感谢任何建议!

最佳答案

您的数据结构使用起来有点尴尬,您可以对其进行 strglimpse 来查看问题,但是您可以按如下方式修复此问题,然后可以绘制它。

> str(df)
'data.frame':   4 obs. of  2 variables:
 $ costC    :'data.frame':  4 obs. of  2 variables:
  ..$ x   : num  1 2 3 NA
  ..$ freq: int  10 11 17 149
 $ productsC:'data.frame':  4 obs. of  2 variables:
  ..$ x   : num  1 2 3 NA
  ..$ freq: int  31 40 30 86

绘图时遵循的代码:

library(ggplot2)
library(tidyverse)
df <- df %>% map(unnest) %>% bind_rows(.id="Name") %>% na.omit() #fixing the structure of column taken as a set of two separate columns

df %>% 
    ggplot(aes(x=Name, y= freq)) +
    geom_col()

我希望这是预期的,尽管我并不完全确定。

输入给定数据:

df <- structure(list(costC = structure(list(x = c(1, 2, 3, NA), freq = c(10L, 
  11L, 17L, 149L)), .Names = c("x", "freq"), row.names = c(NA, 
  4L), class = "data.frame"), productsC = structure(list(x = c(1, 
  2, 3, NA), freq = c(31L, 40L, 30L, 86L)), .Names = c("x", "freq"
  ), row.names = c(NA, 4L), class = "data.frame")), .Names = c("costC", 
  "productsC"), row.names = c(NA, 4L), class = "data.frame")

输出:

enter image description here

在 OP 请求后添加:

在这里,我没有删除 NA,而是用新值“4”替换。为了获取各组之间的相对总和,我使用了 cumsum,然后除以两个组之间的总和以获得相对频率。

df <- df %>% map(unnest) %>% bind_rows(.id="Name") 

df[is.na(df$x),"x"] <- 4

df %>% 
    group_by(Name) %>% 
    mutate(sum_Freq = sum(freq), cum_Freq = cumsum(freq)) %>% 
    filter(x == 3) %>% 
    mutate(new_x = cum_Freq*100/sum_Freq) %>% 
    ggplot(aes(x=Name, y = new_x)) +
    geom_col()

关于r - 条形图 dplyr 汇总值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50354570/

相关文章:

r - 在函数中使用 %>% 和 ifelse()

regex - 提取 R 中所有括号内的信息

r - 在 R 中使用 stringdist_join() 进行模糊连接,错误 : NAs are not allowed in subscripted assignments

r - data.table 的用户指定属性被删除

r - 在 ggplot2 中组合图例

plot - 使用 Julia-Package "Plots"的不对称带状折线图

r - 函数返回 Rmarkdown 图中并排打印的多个绘图,而不是一个在另一个之上

python - matplotlib 绘制与点的大小和颜色相对应的图例

r - R 中宽限月的 Emi 计算

r - Dplyr产生NaN,而碱基R产生NA