ggplot2 adding space between bars by group within group

user2543095

Please see code below. I am trying to introduce spacing in the graph such that a square, a triangle, and a circle lines are grouped by name, but I need additional space between square and triangle (so triangle and circle would be another smaller "group" within the name group). Any suggestions on how to do this? I'm thinking add another "level", or do it by custom dodging, but not sure how to do this.

    ## Data

    names <- c("bio", "bio", "bio", "coal", "coal", "coal")
    upper <- c(1.02, 1.08, 1.20, 1.03, 1.04, 1.05)
    lower <- c(0.96, 1.02, 1.0, 1.01, 1.02, 1.03)
    coef <- c(0.99, 1.05, 1.11, 1.02, 1.03, 1.04)
    level <- c(1,2,3,1,2,3)
    level2 <- c(1,2,3,4,5,6)

    overall <- data.frame(names,upper,lower,coef,level,level2)
    overall$level <- as.factor(overall$level)
    overall$level2 <- as.factor(overall$level2)

## Graph

    graph <- ggplot(overall, aes(x=reorder(names, level2), y=coef, ymin=lower, ymax=upper, shape=level)) + geom_pointrange(position=position_dodge(width=.7), size=1) 

## Lines & Labels

    graph <- graph + geom_hline(aes(yintercept=1), linetype='dashed') + ylab("HR") + xlab("") 
    graph <- graph + theme(panel.background = element_rect(fill='white', colour='black')) + theme(axis.text.x = element_text(angle=40, hjust=1, size=11, colour='black'), axis.text.y=element_text(size=11, colour='black')) 

## Legend

    graph <- graph + coord_flip() 
    graph <- graph + scale_colour_discrete(guide = FALSE) + theme(legend.text.align = 0) + theme(legend.title=element_blank())
    graph + theme(legend.position="right") 
eipi10

You can do this by providing different dodge widths between each pair of symbols. For example:

geom_pointrange(position=position_dodge(width=c(0.7, 1.4)), size=1)

Per your comment, here's the code and the resulting graph. I've condensed your code into a single chain of commands, but it's the same code as in your question, except for the change to geom_pointrange:

ggplot(overall, aes(x=reorder(names, level2), 
                             y=coef, ymin=lower, ymax=upper, shape=level)) + 
  geom_pointrange(position=position_dodge(width=c(0.7, 1.4)), size=1) +
  geom_hline(aes(yintercept=1), linetype='dashed') + ylab("HR") + xlab("") + 
  theme(panel.background = element_rect(fill='white', colour='black')) + 
  theme(axis.text.x = element_text(angle=40, hjust=1, size=11, colour='black'), 
        axis.text.y=element_text(size=11, colour='black')) +
  coord_flip() +
  scale_colour_discrete(guide = FALSE) + 
  theme(legend.text.align = 0,
        legend.title=element_blank(),
        legend.position="right") 

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

R, ggplot2: How do I increase space between specific geom_tile bars to group them in pairs?

From Dev

Remove space between bars ggplot2

From Dev

ggplot can not group bars

From Dev

Making error bars on one group in bar chart in ggplot2

From Dev

Making error bars on one group in bar chart in ggplot2

From Dev

Increase space between bars in ggplot

From Dev

How to group bars arbitrarily in ggplot

From Dev

ggplot2: how to reduce space between narrow width bars, after coord_flip, and panel border

From Dev

How to draw stacked bars in ggplot2 that show percentages based on group?

From Dev

Adding error bars to ggplot2

From Dev

Adding a space between words in mathjax within rshiny

From Dev

ggplot bars with percentage based on group and the same length

From Dev

ggplot bars with percentage based on group and the same length

From Dev

ggplot2 with fill and group

From Dev

How to Increase the Space Between Group Lables in Bokeh

From Dev

LISTAGG: group within group

From Dev

LibGDX group within group

From Dev

Reduce space between ticks in ggplot2

From Dev

ggplot2 - Overlay Mean of Each Group

From Dev

Group similar factors - fills ggplot2

From Dev

ggplot2 color gradient per group

From Dev

Using group argument in aes() in ggplot2

From Dev

ggplot2 - Overlay Mean of Each Group

From Dev

ggplot2: Stack barcharts with group means

From Dev

Labeling in ggplot2 / group factor

From Dev

Placing tick marks between bars in ggplot2

From Dev

Adjusting distance between groups of bars in ggplot2

From Dev

Specific spaces between bars in a barplot - ggplot2 - R

From Dev

Set space between 2 bars using JQuery Flot

Related Related

HotTag

Archive