overlapping state codes on a faceted plot

22 views
Skip to first unread message

mil...@miamioh.edu

unread,
May 27, 2017, 9:39:45 AM5/27/17
to ggplot2
Hello,
I would like to improve readability of the state codes on the x-axis on my faceted plot by region. What is the best solution? 
Attached is my current plot. R code is below:
=======================
ggplot(data = states_map, aes(x = variable, y =value, group= variable))+
  geom_boxplot(aes(fill=sex)) +
  scale_fill_manual(values=c("red"))+
  facet_wrap(~ region0)+
  theme_bw()+
  ylab("CMF") + xlab(" ")+
  theme(plot.title = element_text())+
  theme(legend.position="none")
=====================
Thank for your suggestions!
TM
FacetPlotLabes.docx

Crump, Ron

unread,
May 30, 2017, 4:13:33 AM5/30/17
to mil...@miamioh.edu, ggplot2
HI,

>I would like to improve readability of the state codes on the x-axis on
>my faceted plot by region. What is the best solution?
>Attached is my current plot. R code is below:
>=======================
>ggplot(data = states_map, aes(x = variable, y =value, group= variable))+
> geom_boxplot(aes(fill=sex)) +
> scale_fill_manual(values=c("red"))+
> facet_wrap(~ region0)+
> theme_bw()+
> ylab("CMF") + xlab(" ")+
> theme(plot.title = element_text())+
> theme(legend.position="none")

I wouldn't facet them. This then gives you the full plot width for a
single x-axis. Fill the boxplots by region, maybe order them state within
region to enhance this.

But looking at your code you are filling by sex - but is there only one
level? Everything is filled with red (is the mandated red in the
scale_fill_manual re-used for both sexes? Or is there only one sex
present?).

Then you might try playing with the font size and/or rotating the labels,
but I suspect rotating won't help (state codes are only 2 chars?). See
?theme for both of these.

Ron.

eipi10

unread,
Jun 13, 2017, 12:51:57 AM6/13/17
to ggplot2

Listing all the states in each facet wastes space, since each state appears in only one facet. You can use `scales="free_x"` and `space="free_x"` in order to have each state appear only in its region. Here's an example with built-in data:

set.seed(2)
dat
= data.frame(state=rep(state.abb,100), region=rep(state.region,100), value=rnorm(50*100))

ggplot
(dat, aes(state, value)) +
  geom_boxplot
(width=0.5, outlier.color=NA, fill="red") +
  facet_grid
(. ~ region, scales="free_x", space="free_x") +
  theme_classic
()
Reply all
Reply to author
Forward
0 new messages