Home > Mobile >  combine plots - how to add a plot on the side of another plot, i.e. a bubbleplot?
combine plots - how to add a plot on the side of another plot, i.e. a bubbleplot?

Time:01-17

i have the feeling this questions already has answers but i could not find any keyword or the already asked questions so, if you can point me out in that direction that would be great.

if such a question is new, then how do i plot a barplot to show the frequency of each category on the y axis? basically what i would do is the following:

library("viridis")
library("ggpubr")
library("ggplot2")

mydata <- data.frame(var_one=c("txt_d_p", "txt_d_g", "txt_d_g", "txt_d_g", "txt_d_g", "txt_d_g", "txt_d_g", "txt_d_g", "txt_d_g", "txt_d_g", "txt_d_g", "txt_d_g", "txt_d_g", "txt_d_g", "txt_d_g", "txt_r_p", "txt_r_g", "txt_r_g", "txt_r_g", "txt_r_g", "txt_r_g", "txt_r_g", "txt_r_g", "txt_d_p", "txt_d_g"),
var_two=c("a", "b", "c", "d", "e", "f", "g", "h", "I", "j", "k", "l", "m", "n", "o", "p", "r", "s", "t", "u", "v", "w", "x", "y", "z"),
var_three=c(0.46188, 0.31936, 0.01650, 0.63738, 0.79451, 0.00103, 0.69808, 0.61855, 0.11798, 0.74683, 0.00936, 0.60407, 0.31955, 0.45244, 0.45167, 0.99412, 0.15399, 0.58647, 0.76172, 0.24870, 0.78882, 0.99829, 0.12593, 0.89372, 0.32502),
var_four=c(29, 0, 4, 8, 22, 48, 3, 14, 36, 49, 20, 46, 4, 29, 5, 1, 40, 31, 1, 4, 37, 50, 44, 26, 24),
var_five=c("cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_a", "cat_b", "cat_b"),
var_six=c(0.000308449413656507, 0.00495980196203507, 0.0080034708001948, 0.00863825071060852, 0.0080034708001948, 0.00357824635415942, 0.00824743070875437, 0.000709688303818724, 0.0080034708001948, 0.0080034708001948, 0.000308449413656507, 0.0080034708001948, 0.0080034708001948, 0.00360536192691972, 0.0080034708001948, 0.00070428323596321, 0.00360536192691972, 0.00070428323596321, 0.00563842190862566, 0.00357824635415942, 0.00360536192691972, 0.000384834606500262, 0.00824743070875437, 0.00213203535235097, 0.00378753301631295))

bars <- ggplot(mydata, aes(x=var_two, y=var_six))  
    geom_bar(stat="identity", fill="#f68060", alpha=.6, width=.4)  
    coord_flip()  
    xlab("")  
    theme_bw()  
    theme(text=element_text(size=15, face="bold"), legend.position="none", axis.text.y=element_blank())


# plot tXt
bubble_tXt <- ggplot(mydata, aes(x=var_four, y=var_two, fill=var_one))  
    geom_point(size=abs(log10(mydata$var_three)), alpha=0.5, shape=21, color="black")  
    scale_size(range = c(.1, 24), name="p-adjusted")  
    theme(legend.position="right")  
    ylab("")  
    xlab("var_four")  
    guides(fill=guide_legend(override.aes=list(shape=21, size=5)))  
    theme_bw()  
    theme(text=element_text(size=15, face="bold"), legend.position="right")  
    facet_wrap(~var_five)

ggarrange(bubble_tXt, bars, common.legend = TRUE, legend = "bottom")

but the axes do not match because of the strips in the faceted plot. I think is doable in a single plot, but i am unable to find a way. how do i do it?

CodePudding user response:

One option would be to switch to patchwork to glue your plots:

library(patchwork)

bubble_tXt   bars  
  plot_layout(guides = "collect") &
  theme(legend.position = "bottom")

enter image description here

  • Related