在ggplot2中使用等号并得到真假图,但我不想看到假图

问题描述 投票:0回答:1

我正在制作一个图表,显示社交媒体的每日平均使用情况,并根据人们的分心程度绘制,但我只想显示平均超过 5 小时的图表。

我尝试添加facet_wrap(~DailyAverage ==“超过5小时”),但我在输出中得到了一个TRUE和FALSE图表。我只想看到“真实”的图表,而不是其余的。

这是我的代码:

df %>%
ggplot(aes(Distracted, Frequency, fill = DailyAverage)) + 
geom_bar(stat="identity", position = position_dodge()) +
geom_text(aes(label=Frequency), vjust=1.6, color="Black", 
position = position_dodge(0.9),  size=3.5) + 
theme_bw() + facet_wrap(~DailyAverage == "More than 5 hours")

在此输入图片描述

我也尝试过这个:

df %>%
ggplot(aes(Distracted, Frequency, 
fill = DailyAverage == "More than 5  hours")) + 
geom_bar(stat="identity", position = position_dodge()) +
geom_text(aes(label=Frequency), vjust=1.6, color="Black", 
position = position_dodge(0.9),  size=3.5) + 
theme_bw() 

在此输入图片描述

但它仍然给了我正确与错误。

我不知道如何只展示“超过5小时”

r ggplot2
1个回答
0
投票

正如其他人提到的,您可以对数据进行子集化,并将该子集通过管道传输到 ggplot 中。您还可以使用基本 r 功能在 ggplot 函数中进行子集化。类似于: ggplot(df[df$DailyAverage == "超过 5 小时",]) + geom_bar(ect)

© www.soinside.com 2019 - 2024. All rights reserved.