Gabriel Guevara
Gabriel Guevara

Reputation: 13

ggplot and the geom_text() label

I am new to R and ggplot2. I have used a lot of great examples here but still could not find the solution. My dataframe looks like this:

   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

and when the plot is printed every zero value appears in each column. I want to know how can I hide or make invisible those zeros in the plot.

Problem reference picture

Here is the code that I have used to make my plot.

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))

Upvotes: 1

Views: 3456

Answers (2)

anakar
anakar

Reputation: 356

Here's my solution with dplyr

library(dplyr)                      # This loads up a package called dplyr which has the function "filter"
dfQ3 = dfQ3 %>% filter(Votes != 0)  # This filters out any rows where Votes = 0

# Now you can run your regular code: 

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))

Keep in mind that you will lose the data so it might be worth it to run it this way:

# Notice that I added the filter in the ggplot line

p3 <- ggplot(dfQ3 %>% filter(Votes != 0),  

             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))

p3 after filtering

Upvotes: 0

chemdork123
chemdork123

Reputation: 13863

You can set the data= argument for geom_text() to specifically exclude any zero values. One way to do this is to use subset(), so the geom_text line of your code can be changed to look like this:

geom_text(data=subset(dfQ3, Votes!=0), aes(label=Votes),position = position_stack(vjust = 0.5))

Giving you the following plot (note I had to add underscores to make it easier to import your data):

enter image description here

Upvotes: 2

Related Questions