Ggplot2: geom_text() 添加额外的、不需要的值来绘制
Ggplot2: geom_text() adds additional, unwanted values to plot
我有一个看似简单但无法解决的问题:plot
上出现了太多值。我只想查看总计数(tot_q
,即 n
)一次,以及相关的 pc
(质量为 1 的类别的百分比).这是我的示例代码:
category <- as.factor(c(1, 2, 3, 3, 2, 2, 1, 2, 4, 4, 1, 3, 2, 2, 2, 1))
quality <- as.factor(c(0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1))
mydata <- data.frame(category, quality)
mydata2<- mydata %>% group_by(category,quality) %>% mutate(count_q = n()) %>%
group_by(category) %>% mutate(tot_q=n(),pc=count_q*100/tot_q)
myplot <- ggplot(mydata2, aes(x= category, y = pc)) +
geom_bar(position = 'dodge', stat='identity', fill="lightblue") +
geom_text(aes(label=round(pc)), position=position_dodge(0.9), vjust=-0.5) +
geom_text(aes(label=round(tot_q)), nudge_y = 15, col="red")
myplot
问题:为什么我得到两次 tot_q
值(红色数字)?此外,我如何隐藏较低的百分比(例如,在类别 1 中我只想看到 75%)?我想这与我对数据的预处理有关,但我不知道该怎么做。
使用 geom_text()
的子集数据 (quality = 1
)
library(ggplot2)
library(dplyr)
category <- as.factor(c(1, 2, 3, 3, 2, 2, 1, 2, 4, 4, 1, 3, 2, 2, 2, 1))
quality <- as.factor(c(0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1))
mydata <- data.frame(category, quality)
mydata2<- mydata %>% group_by(category,quality) %>% mutate(count_q = n()) %>%
group_by(category) %>% mutate(tot_q=n(),pc=count_q*100/tot_q)
myplot <- ggplot(mydata2, aes(x= category, y = pc)) +
geom_bar(position = 'dodge', stat='identity', fill="lightblue") +
geom_text(data = filter(mydata2, quality == 1),
aes(label=round(pc)), position=position_dodge(0.9), vjust=-0.5) +
geom_text(data = filter(mydata2, quality == 1),
aes(label=round(tot_q)), nudge_y = 15, col="red")
myplot
由 reprex package (v0.3.0)
于 2020-04-21 创建
这就是你想要的吗? : filter(quality == 1)
在您的数据操作结束时。
library(ggplot2)
library(dplyr)
mydata2<-
mydata %>%
group_by(category, quality) %>%
mutate(count_q = n()) %>%
group_by(category) %>%
mutate(tot_q = n(),
pc = count_q * 100 / tot_q)%>%
filter(quality == 1)
myplot <-
ggplot(mydata2, aes(x= category, y = pc)) +
geom_bar(position = 'dodge', stat='identity', fill="lightblue") +
geom_text(aes(label = round(pc)), position=position_dodge(0.9), vjust = -0.5) +
geom_text(aes(label = round(tot_q)), nudge_y = 15, col = "red")
myplot
我有一个看似简单但无法解决的问题:plot
上出现了太多值。我只想查看总计数(tot_q
,即 n
)一次,以及相关的 pc
(质量为 1 的类别的百分比).这是我的示例代码:
category <- as.factor(c(1, 2, 3, 3, 2, 2, 1, 2, 4, 4, 1, 3, 2, 2, 2, 1))
quality <- as.factor(c(0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1))
mydata <- data.frame(category, quality)
mydata2<- mydata %>% group_by(category,quality) %>% mutate(count_q = n()) %>%
group_by(category) %>% mutate(tot_q=n(),pc=count_q*100/tot_q)
myplot <- ggplot(mydata2, aes(x= category, y = pc)) +
geom_bar(position = 'dodge', stat='identity', fill="lightblue") +
geom_text(aes(label=round(pc)), position=position_dodge(0.9), vjust=-0.5) +
geom_text(aes(label=round(tot_q)), nudge_y = 15, col="red")
myplot
问题:为什么我得到两次 tot_q
值(红色数字)?此外,我如何隐藏较低的百分比(例如,在类别 1 中我只想看到 75%)?我想这与我对数据的预处理有关,但我不知道该怎么做。
使用 geom_text()
quality = 1
)
library(ggplot2)
library(dplyr)
category <- as.factor(c(1, 2, 3, 3, 2, 2, 1, 2, 4, 4, 1, 3, 2, 2, 2, 1))
quality <- as.factor(c(0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1))
mydata <- data.frame(category, quality)
mydata2<- mydata %>% group_by(category,quality) %>% mutate(count_q = n()) %>%
group_by(category) %>% mutate(tot_q=n(),pc=count_q*100/tot_q)
myplot <- ggplot(mydata2, aes(x= category, y = pc)) +
geom_bar(position = 'dodge', stat='identity', fill="lightblue") +
geom_text(data = filter(mydata2, quality == 1),
aes(label=round(pc)), position=position_dodge(0.9), vjust=-0.5) +
geom_text(data = filter(mydata2, quality == 1),
aes(label=round(tot_q)), nudge_y = 15, col="red")
myplot
由 reprex package (v0.3.0)
于 2020-04-21 创建这就是你想要的吗? : filter(quality == 1)
在您的数据操作结束时。
library(ggplot2)
library(dplyr)
mydata2<-
mydata %>%
group_by(category, quality) %>%
mutate(count_q = n()) %>%
group_by(category) %>%
mutate(tot_q = n(),
pc = count_q * 100 / tot_q)%>%
filter(quality == 1)
myplot <-
ggplot(mydata2, aes(x= category, y = pc)) +
geom_bar(position = 'dodge', stat='identity', fill="lightblue") +
geom_text(aes(label = round(pc)), position=position_dodge(0.9), vjust = -0.5) +
geom_text(aes(label = round(tot_q)), nudge_y = 15, col = "red")
myplot