处理因素

发布于 2024-11-27 11:57:00 字数 681 浏览 0 评论 0原文

我有一个对象,它是具有多个级别的因素:

x <- as.factor(c(rep("A",20),rep("B",10),rep("C",15)))

我想以尽可能最短的方式使用 ggplot 创建每个因素的百分比频率的条形图。

我不断发现,当我有一个因素时,在总结和绘图之间会出现很多小烦恼。以下是我所说的烦恼的几个示例:

as.data.frame(summary(x)) 

您必须重命名列,并且在最后一个示例中,第一列值现在是行名。接下来,您必须作弊才能使用强制转换,然后必须重新标记,因为它默认为“(全部)”的列名。

as.data.frame(q1$com.preferred)
dat$value <- 1
colnames(dat) <- c("pref", "value")
cast(dat, pref ~.)
colnames(dat)[2] <- "value"

这是另一个例子,稍微好一些,但不太理想。

data.frame(x=names(summary(x)),y=summary(x))

如果 ggplot 中有一种快速的方法可以做到这一点,我会非常有兴趣看到它。到目前为止,我最大的问题是将计数更改为频率。

I have an object that is a factor with a number of levels:

x <- as.factor(c(rep("A",20),rep("B",10),rep("C",15)))

In the shortest manner possible, I would like to use ggplot to create a bar graph of the % frequency of each factor.

I keep finding that there are a lot of little annoyances that get in between summarizing and plotting when I have a factor. Here are a few examples of what I mean by annoyances:

as.data.frame(summary(x)) 

You have to rename the columns and the 1st column values are now rownames in the last example. In the next, you have to cheat to use cast and then you have to relabel because it defaults to a colname of "(all)".

as.data.frame(q1$com.preferred)
dat$value <- 1
colnames(dat) <- c("pref", "value")
cast(dat, pref ~.)
colnames(dat)[2] <- "value"

Here's another example, somewhat better, but less than ideal.

data.frame(x=names(summary(x)),y=summary(x))

If there's a quick way to do this within ggplot, I'd be more than interested to see it. So far, my biggest problem is changing counts to frequencies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮倦 2024-12-04 11:57:00

跟进 @dirk 和 @joran 的建议(@joran 确实得到了赞扬。我认为 as.data.frame(),而不仅仅是 data.frame(),必要的,但事实证明@joran是对的...

x <- as.factor(c(rep("A",20),rep("B",10),rep("C",15)))
t1 <- table(x)
t2 <- data.frame(t1)
t3 <- data.frame(prop.table(t1))
qplot(x,Freq,data=t2,geom="bar",ylab="Count")
qplot(x,Freq,data=t3,geom="bar",ylab="Proportion")

编辑:稍微缩短(也合并了@Chase的prop.table

Following up on @dirk and @joran's suggestions (@joran really gets credit. I thought as.data.frame(), and not just data.frame(), was necessary, but it turns out @joran's right ...

x <- as.factor(c(rep("A",20),rep("B",10),rep("C",15)))
t1 <- table(x)
t2 <- data.frame(t1)
t3 <- data.frame(prop.table(t1))
qplot(x,Freq,data=t2,geom="bar",ylab="Count")
qplot(x,Freq,data=t3,geom="bar",ylab="Proportion")

edit: shortened slightly (incorporated @Chase's prop.table too)

梦里南柯 2024-12-04 11:57:00

您可以让 qplot 为您完成汇总工作,而无需外部计算,请尝试以下任何操作:

x <- rep(c('A','B','C'), c(20,10,15))

qplot(x, weight=1/length(x), ylab='Proportion')
qplot(x, weight=100/length(x), ylab='Percent')
qplot(x, weight=1/length(x), ylab='Percent') + scale_y_continuous(formatter='percent')

ggplot(data.frame(x=x),aes(x, weight=1/length(x))) + geom_bar() + ylab('Proportion')

可能还有一种方法可以使用 ggplot 函数内部的转换来完成此操作,但我还没有找到。

You can have qplot do the summary work for you without the outside computations, try any of the following:

x <- rep(c('A','B','C'), c(20,10,15))

qplot(x, weight=1/length(x), ylab='Proportion')
qplot(x, weight=100/length(x), ylab='Percent')
qplot(x, weight=1/length(x), ylab='Percent') + scale_y_continuous(formatter='percent')

ggplot(data.frame(x=x),aes(x, weight=1/length(x))) + geom_bar() + ylab('Proportion')

There is probably a way to do this using transformations inside the ggplot functions as well, but I have not found it yet.

帥小哥 2024-12-04 11:57:00

您是否尝试过调用 barplot(table(x)/length(x)) 的 ggplot 等效项?即

R> x <- as.factor(c(rep("A",20),rep("B",10),rep("C",15)))
R> table(x)
x
 A  B  C 
20 10 15 

我们可以轻松地将其转换为百分比

R> table(x)/length(x)*100
x
      A       B       C 
44.4444 22.2222 33.3333 

绘制

R> barplot(table(x)/length(x)*100)

,然后可以很好地

在此处输入图像描述

Did you try the ggplot-equivalent of just calling barplot(table(x)/length(x)) ? I.e.

R> x <- as.factor(c(rep("A",20),rep("B",10),rep("C",15)))
R> table(x)
x
 A  B  C 
20 10 15 

which we turn into percentages easily

R> table(x)/length(x)*100
x
      A       B       C 
44.4444 22.2222 33.3333 

and can then plot

R> barplot(table(x)/length(x)*100)

just fine:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文