0

Here is an example of the code I'm working with

x<-as.factor(rep(c("tree_mean","tree_qmean","tree_skew"),3))
factor<-c(rep("mfn2_burned_99",3),rep("mfna_burned_5_7",3),rep("mfna_burned_5_7_10_12",3)))
y<-c(0.336457409,-0.347422910,-0.318945621,1.494109367, 0.003578698,-0.019985780,-0.484171146, 0.611589217,-0.322292664)
dat<-as.data.frame(cbind(x,factor,y))
head(dat)
       x               factor          y
tree_mean      mfn2_burned_99        -0.3364574
tree_qmean     mfn2_burned_99        -0.3474229
tree_skew      mfn2_burned_99        -0.3189456
tree_mean      mfna_burned_5_7       -0.8269814
tree_qmean     mfna_burned_5_7       -0.8088810
tree_skew      mfna_burned_5_7       -2.5429226
tree_mean      mfna_burned_5_7_10_12 -0.8601206
tree_qmean     mfna_burned_5_7_10_12 -0.8474920
tree_skew      mfna_burned_5_7_10_12 -2.9854178

I am trying to plot how much x deviates from 0, and facet it by each factor, as so:

ggplot(dat) +
  geom_point(aes(x=x,y=y),shape=1,size=3)+
  geom_linerange(aes(x=x,ymin=0,ymax=y))+
  geom_hline(yintercept=0)+
  facet_grid(factor~.)

This works fine when I have three factors (ignore the *: I had a significance column which I have since removed.

Example below:

enter image description here

However, I have 8 factors in total, and faceting obscures the plot such that the distance from zero for each x value gets very distorted.

Example below

enter image description here

So, my question is this: what would be a better way of coding/rendering this plot given my large number of x values and factors using faceting or color coding by factor in ggplot??

I would be very open to color-coding each distance for x by factor rather than faceting, but I have been beating my head against the wall trying to figure out how to even do that in ggplot (very new to ggplot), so I can't yet say if it would make the figure much more interpretable.

user20650
  • 24,654
  • 5
  • 56
  • 91
SilvanD
  • 325
  • 2
  • 14

1 Answers1

0

One option as you note is to color your point and/or linerange by a factor. You can then use position_dodge to move the points slightly on the x axis.

For example:

ggplot(dat, aes(color = factor)) +
    geom_point(aes(x=x,y=y),shape=1,size=3, position = position_dodge(width  = 0.5)+
    geom_linerange(aes(x=x,ymin=0,ymax=y), position = position_dodge(width =0.5))+
    geom_hline(yintercept=0)

I think this would still be difficult with many factors, but with 8 it might suit your purposes.

jraab
  • 413
  • 4
  • 10