0

I am trying to plot how different predictors associate with stroke and underlying phenotypes (i.e. cholesterol). In my data, I originally had working ggplot code in which shapes denoted the different variables (stroke, HDL cholesterol and total cholesterol) and colour denoted type (i.e. disease (stroke) or phenotype (HDL/total cholesterol). To make it more intuitive, I want to swap shape and colour around but now that I do this, I am having issues with position dodge and the alignment of geom_point and geom_error

stroke_graph <- ggplot(stroke,aes(y=as.numeric(stroke$test), 
                                  x=Clock, 
                                  shape = Type, 
                                  colour = Variable)) + 
  geom_point(data=stroke, aes(shape=Type, colour=Variable), show.legend=TRUE, 
              position=position_dodge(width=0.5), size = 3) +
  geom_errorbar(aes(ymin = as.numeric(stroke$LCI), ymax= as.numeric(stroke$UCI)),
                position = position_dodge(0.5), width = 0.05,
                colour  ="black")+
  ylab("standardised beta/log odds")+ xlab ("")+
  geom_hline(yintercept = 0, linetype = "dotted")+
  theme(axis.text.x = element_text(size = 10, vjust = 0.5), legend.position = "none",
        plot.title = element_text(size = 12))+
  scale_y_continuous(limit = c(-0.402, 0.7))+ scale_shape_manual(values=c(15, 17, 18))+
  theme(legend.position="right") + labs(shape = "Variable") + guides(shape = guide_legend(reverse=TRUE)) + 
  coord_flip() 
stroke_graph + ggtitle("Stroke and Associated Phenotypes") + theme(plot.title = element_text(hjust = 0.5))

Graph now: 1 Previously working graph - only difference in code is swapping "Type" and "Variable": 2

Rob
  • 1
  • 1
  • Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Jul 15 '19 at 03:11
  • I guess there is some grouping issue going on. Try to remove all those instances with `stroke$` as this likely also causes confusion. Within ggplot2 there is usually a setting of the data frame `stroke` directly in the beginning of your ggplot command. There are reasons when this type of syntax can make sense, but this shouldn't be the case here. Also probably recoding the variables to the proper types might help to spot errors/unexpected things. – TobiO Jul 15 '19 at 20:41

0 Answers0