0

Update: It looks like this issue might be due to a bug. From my pretty basic understanding of the github issues threads, it looks like they are aware of it and looking to fix it in the next update.

The geom_text function is placing incorrect labels when I use a variable for facet_wrap that I have re-ordered into levels. This graph is supposed to be labeled with values from the x-axis. This graph is supposed to be labeled with values from the x-axis But obviously the labels are wrong and when I compared them to my data (see below) it was obvious that the faceting was not ordering the geom_text into the levels I wanted. So the values for the "1 to 5 years" group are used to label the "Under a year" facet.

data frame is TenQ1 (short for Tenure, Question 1)

Groups:   Question, Tenure [5]
Question Tenure         Value          Value_Mean     n
 <chr>    <fct>          <chr>               <dbl> <int>
  1 Q1       1 to 5 years   Communications       2.47    34
  2 Q1       1 to 5 years   Community            2.41    22
  3 Q1       1 to 5 years   ContImp              2.68    37
  4 Q1       1 to 5 years   Customers            1.74    50
 5 Q1       1 to 5 years   Employees            2.02    45
 6 Q1       1 to 5 years   Integrity            2.09    57
 7 Q1       1 to 5 years   Preparedness         2.5     24
 8 Q1       1 to 5 years   Teamwork             2.40    43
 9 Q1       10 to 20 years Communications       2.76    34
10 Q1       10 to 20 years Community            2.67    18
# ... with 30 more rows 

Here is the code I am using for my plot:

ggplot(TenQ1, aes(x = Value,  fill = n))
+geom_point(aes( y = Value_Mean,fill = n), size = 5,shape = 21)
+facet_wrap(~Tenure, ncol = 3)
+scale_fill_gradientn(colors = rainbow(5))+scale_y_reverse(limits = c(4,1)) 
+theme(axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 0)), axis.text.x=element_text(size = rel(1.3), angle=45, hjust=1))
+geom_text(aes(y = Value_Mean), nudge_y = .3, size = 3, label = round(TenQ1$Value_Mean, digits=1))
 + labs(fill = "Number of staff \n that ranked this \n value in top 4",y = "Average rank from 1 to 4 for each value")

I thought about removing the TenQ1$ from my label in geom_text but then I get the error "object 'Value_Mean' not found".

Lauren
  • 110
  • 1
  • 10
  • 2
    It's because `facet_wrap` will order plots based on the factor's levels (`Tenure`), but then you add the labels using the order of your data frame :) Use `TenQ1$Tenure = factor(TenQ1$Tenure, levels = unique(TenQ1$Tenure))` to recode the levels based on the order of your dataset and then run your `ggplot` series of commands..... – AntoniosK Aug 20 '18 at 22:45
  • 1
    @Lauren: Next time please share sample of your data using `dput()` (not `str` or `head` or picture/screenshot) so others can help. See more here https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1 – Tung Aug 20 '18 at 23:28
  • @AntoniosK That undoes the ordering and yes it fixes the labels but then my facets are not sequential. – Lauren Aug 21 '18 at 15:23
  • Then you can specify the order that makes sense for you and apply it to your dataset. Then your dataset will follow your desired order and the facets will be plotted as you want. – AntoniosK Aug 21 '18 at 15:28
  • That is what I did originally. That is how I got the facets ordered in the first place. For some reason the geom_text is not following the order of the factored facets. I am starting to think it is a bug. I used : TenQ1$Tenure = factor(TenQ1$Tenure, levels = TenureLevels) – Lauren Aug 21 '18 at 16:35
  • https://github.com/tidyverse/ggplot2/issues/2831 according to this it looks like a bug. "Data is no longer internally reordered when faceting" Bummer. – Lauren Aug 21 '18 at 17:08

0 Answers0