Using position_dodge with geom_pointrange

I am trying to draw a graph using ggplot, geom_poitrange. I have two groups, each with two points and corresponding error values. the code i am using is below:

    group<-c("A","A","B","B")
    val<-c(1.3,1.4, 1.2,1.5)
    SD<-c(0.3,0.8,0.6,0.5)
    RX<-c("X","Z","X","Z")

    a<-data.frame(group,val,SD,RX)
    ggplot(data=a)+
    geom_pointrange(aes(x=RX, y=val, ymin=(val-SD), ymax=(val + SD), 
    group=group, color=group, position_dodge(width=4)), size=1.5)

      

With this I get a nice graph, but the groups overlap. enter image description here

I wanted to compensate for them. I tried the following:

    geom_pointrange(aes(x=RX, y=val, ymin=(val-SD), ymax=(val + SD), 
    group=group, color=group, position_dodge(width=1)), size=1.5)

      

or

    geom_pointrange(aes(x=RX, y=val, ymin=(val-SD), ymax=(val + SD), 
    group=group, color=group, position="dodge"), size=1.5)

      

and variations of the above. Can anyone suggest what I am doing wrong? Thanks to

+4


source to share


1 answer


The OP provides two possible solutions. The first solution uses a function position_dodge()

that is close. The problem is that it is in the wrong place in the argument list (not because the width is too large).

Explicitly specify position = position_dodge(width = 1)

afteraes()

ggplot(data=a) +     
geom_pointrange(aes(x=RX, y=val, ymin=(val-SD), max=(val + SD), 
                                       group=group, color=group), 
position = position_dodge(width = 1), size=1.5)

      

By checking the API in the help ?geom_pointrange()

, you can see the position comes after the display, data and statistics. The simplest thing to do here is to be explicit, as shown above. Otherwise, you will receive an error or warning, for example:

Warning: Ignoring unknown aesthetics 

      

or



Error: 'data' must be a data frame, or other object coercible by 'fortify()', not an S3 object with class PositionDodge/Position/ggproto/gg

      

Why not position="dodge"

?

If you try the second solution, you will get a warning to try the first solution:

Warning message:
Width not defined. Set with 'position_dodge(width = ?)' 

      

As far as I understand, the evasion is written for columns and rectangles and takes advantage of the width

intrinsic nature of these objects. The lines have no width, so you need to explicitly specify how many dodges should occur.

0


source







All Articles