Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何对ggplot2中的分类数据使用stat()-命令?_R_Ggplot2 - Fatal编程技术网

R 如何对ggplot2中的分类数据使用stat()-命令?

R 如何对ggplot2中的分类数据使用stat()-命令?,r,ggplot2,R,Ggplot2,这个问题是关于将绘图中的Y值转换为百分比的,非常类似于。但是,答案似乎不再适用,因为必须使用stat()-函数 我的情节是这样的: 它是用以下代码创建的。X变量是一个分类变量(城市),y变量统计每个城市的观察次数: ggplot(fulldata, aes(x=fct_rev(fct_infreq(CITY_LADOK)))) +geom_bar() +coord_flip() 我想将y贵重物品转换为百分比,最好不必创建参考表。计算美学的帮助页…不是特别有用。它没有说明是否可以计算百分比,也

这个问题是关于将绘图中的Y值转换为百分比的,非常类似于。但是,答案似乎不再适用,因为必须使用stat()-函数

我的情节是这样的:

它是用以下代码创建的。X变量是一个分类变量(城市),y变量统计每个城市的观察次数:

ggplot(fulldata, aes(x=fct_rev(fct_infreq(CITY_LADOK)))) +geom_bar() +coord_flip()
我想将y贵重物品转换为百分比,最好不必创建参考表。计算美学的帮助页…不是特别有用。它没有说明是否可以计算百分比,也没有说明如何计算百分比。但是,如果我从这些例子中推断,我应该能够写一些大致如下的内容:

    ggplot(fulldata, aes(x=fct_rev(fct_infreq(CITY_LADOK)))) 
+geom_bar(y=stat(count/sum(count)))+coord_flip()
…至少在理论上,现在我收到一条错误消息,声称:

Error in sum(count) : invalid 'type' (closure) of argument
但是如果我缩小这个比例,然后简单地使用stat()来计算原始绘图,会怎么样

ggplot(fulldata, aes(x=fct_rev(fct_infreq(CITY_LADOK)))) 
    +geom_bar(y=stat(count))+coord_flip()
我们收到另一条错误消息

Error in rep(value[[k]], length.out = n) : 
  attempt to replicate an object of type 'closure'
它不适用于y=stat(bin),而且似乎也不适用于y=stat(identity)。stat()函数是否可以用于分类值?如果可以,是否可以用于计算百分比

数据摘录:

structure(list(start_date = structure(c(17776, 17776, 17776, 
17776, 17776, 17776, 17776, 17776, 17776, 17776, 17776, 17776, 
17776, 17776, 17776, 17776, 17776, 17776, 17776, 17776), class = "Date"), 
    CITY_LADOK = c("GÖTEBORG", "LILLA_EDET", "GÖTEBORG", "GÖTEBORG", 
    "UDDEVALLA", "SKÖVDE", "VÄSTERÅS", "TROLLHÄTTAN", "ALE", 
    "GÖTEBORG", "GÖTEBORG", "GÖTEBORG", "UPPSALA", "TJÖRN", "TROLLHÄTTAN", 
    "UDDEVALLA", "UDDEVALLA", "KUNGSBACKA", "VÄNERSBORG", "UDDEVALLA"
    )), row.names = c(NA, -20L), groups = structure(list(start_date = structure(17776, class = "Date"), 
    .rows = list(1:20)), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

Magnus,您已经非常接近了,但是在将变量映射到函数时,您需要确保仔细使用
aes()
函数。基本上,任何时候当您向ggplot函数提供动态值时,
aes()
都是必需的。下面是小样本

library(tidyverse)
df <- tibble(
  city = c(rep("A", 5), rep("B", 2), "C", "D", "E")
)

# Simplified count will work, but make sure to use aes()
df %>%
  ggplot(aes(x = fct_rev(fct_infreq(city)))) +
  geom_bar(aes(y = stat(count))) +
  coord_flip()

# Percentage will work as well, but take care with aes() and parentheses
df %>%
  ggplot(aes(x = fct_rev(fct_infreq(city)))) +
  geom_bar(aes(y = stat(count) / sum(stat(count)))) +
  coord_flip()

# Can also request the proportion directly, but then need to ensure 
# proportion grouping isn't the x variable by default.
df %>%
  ggplot(aes(x = fct_rev(fct_infreq(city)))) +
  geom_bar(aes(y = stat(prop), group = NA)) +
  coord_flip()
库(tidyverse)
df%
ggplot(aes(x=fct\U版本(fct\U信息(城市)))+
geom_bar(aes(y=统计(计数)))+
coord_flip()
#百分比也可以,但要注意aes()和括号
df%>%
ggplot(aes(x=fct\U版本(fct\U信息(城市)))+
geom_bar(aes(y=统计(计数)/总和(统计(计数)))+
coord_flip()
#也可以直接要求比例,但需要保证
#默认情况下,比例分组不是x变量。
df%>%
ggplot(aes(x=fct\U版本(fct\U信息(城市)))+
geom_bar(aes(y=stat(prop),group=NA))+
coord_flip()

您可能还需要注意的是,
stat()
访问计算变量,例如
count
prop
geom\u bar()、geom\u col()的情况下。
stat\u identity()、stat\u count()、stat\u bin()
系列不同,它们描述了ggplot聚合数据的不同方法。

感谢您的教学(这是一个表达式吗?)答案。我有点不确定“动态值”意味着什么,但我从中得出的结论是:(1)为了访问Geom中的变量,我首先需要使用aes()-函数;(2)我不能直接使用计算值,但需要在每次单独使用之前使用stat()-函数。是的。相反,如果您提供一些简单的静态参数,如
color=“blue”
,作为
geom\u bar
的参数,则无需将其包装在
aes()
中。