0

I am trying to join these two charts into one. Ideally I would have each region be divided into two subcategories and have male and female dumbbells one top of each other for each region.

I also ideally would want to eliminate that Female and Male y axis titles and just have color define each variable.

The picture has two dumbbell charts next to each other with different x axis but the same y axis

The picture has two dumbbell charts next to each other with different x axis but the same y axis

Here is the code I currently have:

lung<- ggplot(lungz, aes(y=world_region, x=lower_95, xend=upper_95, colour=sex))+geom_dumbbell(size=1,
            size_x = 1,
            size_xend = 1)+
geom_point(lungz, mapping=aes(y=world_region, x=pir, colour=sex), size=3)+
  scale_fill_manual(values = c("#339ff2", "#f23333"))+
  facet_wrap(vars(sex), strip.position = "left", scales = "free_x")+mdh_style()+labs(title="Lung and Bronchus Cancer PIR",
   subtitle = "Stratified by Sex and World Region",
   x="Age-Adjusted Proportional Incidence Ratio")

lung<- lung + geom_vline(xintercept = 1, linetype="dotted", size=1, colour="#53565a")+geom_vline(xintercept = 0, size=0.75, colour="#000000")+theme(panel.grid.major.x = element_line(color="#cbcbcb"),
         panel.grid.major.y = element_blank())
tjebo
  • 21,977
  • 7
  • 58
  • 94
M.Frumh
  • 15
  • 2
  • Please provide a reproducible example of your dataset (see this link: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – dc37 Mar 18 '20 at 14:54

1 Answers1

1

Without a reproducible example of your dataset to try on it, it is difficult to be sure of the solution to your question.

From my understanding, you are looking to have each region in y axis subdivided for male and female. A possible solution is to reverse variables used for facetting and y axis and have world_region as the argument for facet_grid and sex as the argument for y axis. Then, with some manipulation of facet labels positions and spacing, you can mimick a graph that the one you are expected.

As you did not provide a reproducible example, I made one that maybe does not fully mimick your data. So, you will have to adjust the code provided to make it work on your data. Also, I don't know mdh_style and you did not refer any library for this function, so I plot using default parameters of ggplot2. Hopefully, it will be compatible to your theme.

library(ggalt)
library(ggplot2)

df <- data.frame(world = rep(c("West Africa", "Southeast Asia", "Europe"),2),
                 sex = rep(c("M","F"), each = 3),
                 mean = c(1,3,4,5,2,3),
                 low = c(0,0.1,0.2,0.23,0.3,0),
                 end = c(4,5,7,6,8,6))

ggplot(df, aes(x = low, y = sex, xend = end, color = sex))+
  geom_dumbbell()+
  facet_grid(world~., switch = "y", scales = "free_y")+
  theme_bw()+
  theme(strip.text.y = element_text(angle = 180),
        strip.placement = "outside",
        panel.grid.major.x = element_line(color="#cbcbcb"),
        panel.grid.major.y = element_blank(),
        legend.position = "top",
        panel.spacing = unit(0, "lines"),
        axis.text.y = element_blank())+
  labs(title="Lung and Bronchus Cancer PIR", y = "",
       subtitle = "Stratified by Sex and World Region",
       x="Age-Adjusted Proportional Incidence Ratio")

enter image description here

If this is not working, please provide a reproducible example of your dataset (How to make a great R reproducible example).

dc37
  • 15,840
  • 4
  • 15
  • 32
  • 1
    two different understanding of the same question ;).... let's see if the OP is clarifying its question and the desired output. – dc37 Mar 18 '20 at 16:02
  • 1
    Hard to be totally sure ... we'll see ;) – dc37 Mar 18 '20 at 16:13
  • Ah this really helps. This is much closer to what I was looking to do. I also came across ggstance package and the argument for position_dodgev(height=x), however R tells me that it doesn't recognize the position argument for geom_dumbbell for some reason? https://yonicd.github.io/ggalt/reference/geom_dumbbell.html This user used position argument to have his two dumbbells in row D. – M.Frumh Mar 18 '20 at 18:38
  • It worked like a charm! Thanks so much. The only thing now is I am trying to get rid of the Male and Female y axis headers (hence why I color coordinated them). I'll be playing around with that code now to try and get rid of it. But your answer worked great and is much appreciated. Is there a way to "strip" the Male and Female text? @dc37 – M.Frumh Mar 18 '20 at 18:52
  • You're welcome ;). Regarding the strip of Male and Female text, you can add `axis.text.y = element_blank()`. However, for the use of `position_dodgev` into `geom_dumbbell`, you need to have `ggalt_0.6.2` installed. The current version that I have is `ggalt_0.4.0`. – dc37 Mar 19 '20 at 01:36
  • Ahhh that is good to know. Thanks! Really appreciate all your help!! – M.Frumh Mar 19 '20 at 17:21