在 y 轴中使用计数,但将百分比和计数用作标签
Use count in the y-axis but percentages and counts as labels
我正在尝试在 R 中从一个数据框创建一个条形图,该数据框在 y 轴上有计数,但显示为百分比和计数的串联标签。
我的数据框如下所示:
ID Response
1 No
2 Yes
3 No
.. ..
我想要的最终结果是如下图
我会尝试下面的方法。您使用 summarize
和 mutate
真是太棒了;我想出于习惯,我有时会使用像 table
这样的基本函数。
library(tidyverse)
resps<-sample(c("yes", "no"), 850, replace=T)
percents<-round(100*table(resps)/length(resps),2)
counts<-as.numeric(table(resps))
plotdat<-data.frame(percents, counts=counts, response=rownames(percents))
plotdat %>% ggplot(aes(response, counts)) +
geom_col()+
geom_text(aes(y=counts+10), label=paste(percents,"% ", counts))
labs(y="respondents")+
theme_classic()
这是 another question 在 SO:
上提供的有用解决方案
library(ggplot2)
library(scales)
data.frame(response = sample(c("Yes", "No"), size = 100, replace = T, prob = c(0.4, 0.6))) %>%
ggplot(aes(x = response)) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
geom_text(aes(y = ((..count..)/sum(..count..)),
label = scales::percent((..count..)/sum(..count..))), stat = "count", vjust = -0.25) +
scale_y_continuous(labels = percent) +
labs(title = "Proportion of Responses", y = "Percent", x = "Response")
这应该让你继续:
library(tidyverse)
df %>%
group_by(Response) %>%
summarise(count = n()) %>%
mutate(Label = paste0(count, " - ", round(count / sum(count) * 100, 2), "%")) %>%
ggplot(aes(x = Response, y = count)) +
geom_bar(stat = 'identity', fill = 'lightblue') +
geom_text(aes(label = Label)) +
theme_minimal()
上述解决方案可以是创建一个 Label
列,然后您可以根据需要将其传递给 geom_text
。
一个虚拟数据框:
df <- data.frame(
ID = c(1:100),
Response = c(rep("Yes", 60), rep("No", 40))
)
我正在尝试在 R 中从一个数据框创建一个条形图,该数据框在 y 轴上有计数,但显示为百分比和计数的串联标签。
我的数据框如下所示:
ID Response
1 No
2 Yes
3 No
.. ..
我想要的最终结果是如下图
我会尝试下面的方法。您使用 summarize
和 mutate
真是太棒了;我想出于习惯,我有时会使用像 table
这样的基本函数。
library(tidyverse)
resps<-sample(c("yes", "no"), 850, replace=T)
percents<-round(100*table(resps)/length(resps),2)
counts<-as.numeric(table(resps))
plotdat<-data.frame(percents, counts=counts, response=rownames(percents))
plotdat %>% ggplot(aes(response, counts)) +
geom_col()+
geom_text(aes(y=counts+10), label=paste(percents,"% ", counts))
labs(y="respondents")+
theme_classic()
这是 another question 在 SO:
上提供的有用解决方案library(ggplot2)
library(scales)
data.frame(response = sample(c("Yes", "No"), size = 100, replace = T, prob = c(0.4, 0.6))) %>%
ggplot(aes(x = response)) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
geom_text(aes(y = ((..count..)/sum(..count..)),
label = scales::percent((..count..)/sum(..count..))), stat = "count", vjust = -0.25) +
scale_y_continuous(labels = percent) +
labs(title = "Proportion of Responses", y = "Percent", x = "Response")
这应该让你继续:
library(tidyverse)
df %>%
group_by(Response) %>%
summarise(count = n()) %>%
mutate(Label = paste0(count, " - ", round(count / sum(count) * 100, 2), "%")) %>%
ggplot(aes(x = Response, y = count)) +
geom_bar(stat = 'identity', fill = 'lightblue') +
geom_text(aes(label = Label)) +
theme_minimal()
上述解决方案可以是创建一个 Label
列,然后您可以根据需要将其传递给 geom_text
。
一个虚拟数据框:
df <- data.frame(
ID = c(1:100),
Response = c(rep("Yes", 60), rep("No", 40))
)