R ggplot和geom_text()标签
我不熟悉R和ggplot2。我在这里使用了很多很好的例子,但仍然找不到解决方案。 我的数据框如下所示:R ggplot和geom_text()标签,r,ggplot2,R,Ggplot2,我不熟悉R和ggplot2。我在这里使用了很多很好的例子,但仍然找不到解决方案。 我的数据框如下所示: Thermal.sensation Acceptance Votes 1 -3 Clearly acceptable 0.00 2 -3 Just acceptable 0.33 3 -3 Just unacceptable 0.53 4
Thermal.sensation Acceptance Votes
1 -3 Clearly acceptable 0.00
2 -3 Just acceptable 0.33
3 -3 Just unacceptable 0.53
4 -3 Clearly unacceptable 0.13
5 -2 Clearly acceptable 0.05
6 -2 Just acceptable 0.80
7 -2 Just unacceptable 0.15
8 -2 Clearly unacceptable 0.00
9 -1 Clearly acceptable 0.25
10 -1 Just acceptable 0.73
11 -1 Just unacceptable 0.02
12 -1 Clearly unacceptable 0.00
13 0 Clearly acceptable 0.06
14 0 Just acceptable 0.90
15 0 Just unacceptable 0.03
16 0 Clearly unacceptable 0.00
17 1 Clearly acceptable 0.00
18 1 Just acceptable 0.63
19 1 Just unacceptable 0.38
20 1 Clearly unacceptable 0.00
21 2 Clearly acceptable 0.00
22 2 Just acceptable 0.00
23 2 Just unacceptable 1.00
24 2 Clearly unacceptable 0.00
25 3 Clearly acceptable 0.00
26 3 Just acceptable 0.00
27 3 Just unacceptable 0.00
28 3 Clearly unacceptable 0.00
geom_text(data=subset(dfQ3, Votes!=0), aes(label=Votes),position = position_stack(vjust = 0.5))
打印绘图时,每列中都会出现一个零值。我想知道如何在绘图中隐藏或使这些零不可见
这是我用来制作情节的代码
dfQ3 <- data.frame(Thermal.sensation,Acceptance,Votes)
dfQ3$Thermal.sensation <- factor(dfQ3$Thermal.sensation,
levels = c(-3,-2,-1,0,+1,+2,+3))
dfQ3$Acceptance <- factor(dfQ3$Acceptance,
levels = c("Clearly acceptable","Just acceptable",
"Just unacceptable","Clearly unacceptable"))
p3 <- ggplot(dfQ3,
aes(fill=Acceptance, y=Votes, x=Thermal.sensation)) +
geom_bar(position="fill", stat="identity") +
scale_fill_brewer(palette = "Blues", direction = -1) +
geom_text(aes(label=Votes),
position = position_stack(vjust = 0.5))
dfQ3您可以设置geom_text()
的data=
参数,专门排除任何零值。一种方法是使用subset()
,这样就可以将代码的geom_text
行更改为如下所示:
Thermal.sensation Acceptance Votes
1 -3 Clearly acceptable 0.00
2 -3 Just acceptable 0.33
3 -3 Just unacceptable 0.53
4 -3 Clearly unacceptable 0.13
5 -2 Clearly acceptable 0.05
6 -2 Just acceptable 0.80
7 -2 Just unacceptable 0.15
8 -2 Clearly unacceptable 0.00
9 -1 Clearly acceptable 0.25
10 -1 Just acceptable 0.73
11 -1 Just unacceptable 0.02
12 -1 Clearly unacceptable 0.00
13 0 Clearly acceptable 0.06
14 0 Just acceptable 0.90
15 0 Just unacceptable 0.03
16 0 Clearly unacceptable 0.00
17 1 Clearly acceptable 0.00
18 1 Just acceptable 0.63
19 1 Just unacceptable 0.38
20 1 Clearly unacceptable 0.00
21 2 Clearly acceptable 0.00
22 2 Just acceptable 0.00
23 2 Just unacceptable 1.00
24 2 Clearly unacceptable 0.00
25 3 Clearly acceptable 0.00
26 3 Just acceptable 0.00
27 3 Just unacceptable 0.00
28 3 Clearly unacceptable 0.00
geom_text(data=subset(dfQ3, Votes!=0), aes(label=Votes),position = position_stack(vjust = 0.5))
为您提供以下绘图(注意,我必须添加下划线,以便于导入数据):
这是我使用dplyr的解决方案
library(dplyr)#加载一个名为dplyr的包,该包具有“filter”功能
dfQ3=dfQ3%>%筛选(投票数!=0)#这将筛选出投票数为0的任何行
#现在,您可以运行常规代码:
p3我发现有人在使用“过滤器”,但当我尝试时,没有成功。现在多亏了你,我知道为什么了。你真可怕。