Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

r - Inconsistent Column width in geom_bar() + facet_wrap()

I promise I have looked everywhere for the answer to this and tried every solution I could find, and nothing has worked. Here is the code for a faceted geom_bar() plot:

Here is a glimpse of the data set piDF:

Rows: 20,093
Columns: 6
Groups: student_id, domain [20,060]
$ student_id                <dbl> 222484, 222484, 220337, 220337, 220337, 220337, 221451, 221451, 221451, 221451, 221451, 222725, 222725, 222725, 22...
$ student_grade             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
$ subject                   <chr> "Math", "Reading", "Math", "Math", "Reading", "Reading", "Math", "Math", "Reading", "Reading", "Reading", "Math", ...
$ domain                    <chr> "Number and Operations", "Phonological Awareness", "Algebra and Algebraic Thinking", "Number and Operations", "Pho...
$ lesson_grade              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
$ relative_lesson_placement <fct> Mid on Level, Early on Level, Mid on Level, Mid on Level, 1 Level Below, Early on Level, Mid on Level, Early on Le...

Here is the ggplot code:

plot1 <- piDF %>%
  filter(student_grade <= 8) %>%
  ggplot(aes(x = student_grade, fill = relative_lesson_placement)) + 
  geom_bar(position = "fill") +
  scale_x_discrete("Grade", limits = c(0:8), labels = c("K", "1", "2", "3", "4", "5", "6", "7", "8")) + 
  scale_y_continuous("Proportion relative placement", expand = c(0,0)) +
  scale_fill_manual("Relative Placement", 
                    values = rev(c("#e63939", "#de7e7e", "#ebeb4d", "#70e65e", "#37c44f", "#29993c", "#48addb")), 
                    labels = rev(c("3+ Levels Below", 
                                   "2 Levels Below", 
                                   "1 Level Below", 
                                   "Early on Level", 
                                   "Mid on Level", 
                                   "Late on Level", 
                                   "Above Level"))
                ) + 
   theme(axis.title.y = element_blank(), 
         axis.text.y = element_blank()) +
   facet_wrap(vars(subject, factor(domain)), scales = "free_x")

And this is a screen shot of the result:

enter image description here

I cannot figure out how to:
a) get rid of the extra column on the far right of most graphs, and
b) why the Geometry graph is not scaled the same as all the others.

Each graph should have only 9 columns of equal width.

You help is greatly appreciated.

question from:https://stackoverflow.com/questions/65853925/inconsistent-column-width-in-geom-bar-facet-wrap

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The suggestion by yh6 to change student_grade to char did the trick.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...