Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot 2 different dataframe into 1 plot and set custom x-axis with ggplot2

Tags:

r

ggplot2

I would like to create a plot from 2 different df

my code:

ggplot() +
  geom_boxplot(data=df.temp1, aes(x = sample2, y=delta_rn)) +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5), axis.title = element_text(size=20),axis.text=element_text(size=15),
        axis.text.x=element_text(angle = -40, hjust = 0))+
  geom_boxplot(data=df.temp2, aes(x = 'rn',y=delta_rn))

df1:

structure(list(sample2 = c("p10_065", "p10_065", "p10_065", "p10_065", 
"p10_065", "p10_065", "p10_065", "p10_065", "p10_065", "p39_080", 
"p39_080", "p39_080", "p39_080", "p39_080", "p39_080", "p39_080", 
"p39_080", "p39_080", "s36_068", "s36_068", "s36_068", "s36_068", 
"s36_068", "s36_068", "s36_068", "s36_068", "s36_068", "s32_066", 
"s32_066", "s32_066", "s32_066", "s32_066", "s32_066", "s32_066", 
"s32_066", "s32_066", "s43_078", "s43_078", "s43_078", "s43_078", 
"s43_078", "s43_078", "s43_078", "s43_078", "s43_078", "s29_083", 
"s29_083", "s29_083", "s29_083", "s29_083", "s29_083", "s29_083", 
"s29_083", "s29_083", "p1_065", "p1_065", "p1_065", "p1_065", 
"p1_065", "p1_065", "p1_065", "p1_065", "p1_065", "p2_080", "p2_080", 
"p2_080", "p2_080", "p2_080", "p2_080", "p2_080", "p2_080", "p2_080", 
"s21_077", "s21_077", "s21_077", "s21_077", "s21_077", "s21_077", 
"s21_077", "s21_077", "s21_077", "p41_072", "p41_072", "p41_072", 
"p41_072", "p41_072", "p41_072", "p41_072", "p41_072", "p41_072", 
"s22_066", "s22_066", "s22_066", "s22_066", "s22_066", "s22_066", 
"s22_066", "s22_066", "s22_066"), delta_rn = c(7588L, 15153L, 
22729L, 30292L, 37867L, 45444L, 53017L, 60585L, 68154L, 6319L, 
12628L, 18929L, 25238L, 31545L, 37853L, 44159L, 50463L, 56773L, 
9066L, 18123L, 27179L, 36233L, 45292L, 54341L, 63398L, 72453L, 
81513L, 11135L, 22266L, 33392L, 44514L, 55641L, 66770L, 77898L, 
89018L, 100148L, 8014L, 16020L, 24018L, 32021L, 40029L, 48030L, 
56032L, 64029L, 72036L, 8175L, 16331L, 24492L, 32649L, 40808L, 
48959L, 57123L, 65275L, 73440L, 18334L, 36667L, 54985L, 73309L, 
91636L, 109959L, 128290L, 146607L, 164931L, 18969L, 37923L, 56882L, 
75830L, 94789L, 113745L, 132695L, 151652L, 170608L, 14462L, 28915L, 
43369L, 57820L, 72276L, 86727L, 101185L, 115632L, 130091L, 18758L, 
37518L, 56268L, 75018L, 93772L, 112529L, 131280L, 150029L, 168782L, 
18311L, 36609L, 54908L, 73206L, 91505L, 109800L, 128099L, 146395L, 
164695L)), class = "data.frame", row.names = c(102L, 103L, 104L, 
105L, 106L, 107L, 108L, 109L, 110L, 72L, 73L, 74L, 75L, 76L, 
77L, 78L, 79L, 80L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 
40L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 82L, 83L, 
84L, 85L, 86L, 87L, 88L, 89L, 90L, 52L, 53L, 54L, 55L, 56L, 57L, 
58L, 59L, 60L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 42L, 
43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L))

and df2:

structure(list(sample2 = c("p10_065", "p39_080", "s36_068", "s32_066", 
"s43_078", "s29_083", "p1_065", "p2_080", "s21_077", "p41_072", 
"s22_066"), delta_rn = c(5121066L, 6585801L, 2955395L, 0L, 4636924L, 
4271671L, 2699432L, 1884355L, 4851650L, 1846467L, 1588973L)), class = "data.frame", row.names = c(101L, 
71L, 11L, 21L, 1L, 31L, 91L, 81L, 51L, 61L, 41L))

and here is my plot: enter image description here

The problem is that my "rn" label is in the middle and I'm unable to custom this x-axis. It's sorting it alphabetically, but I would like this label which is coming from my second df to be either at the extreme left or the extreme right of my plot. So to be more clear I would like to order my label by df...

like image 698
D Prat Avatar asked Nov 26 '25 02:11

D Prat


1 Answers

You can simply create the labels and do a manual annotation of the axis using scale_x_discrete(labels= xlabs):

xlabs <- c("rn", levels(factor(df1$sample2)))

ggplot() +
  geom_boxplot(data=df1, aes(x = sample2, y=delta_rn)) +
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5), axis.title = element_text(size=20),axis.text=element_text(size=15),
        axis.text.x=element_text(angle = -40, hjust = 0))+
  geom_boxplot(data=df2, aes(x = '',y=delta_rn)) +  scale_x_discrete(labels= xlabs)

Output

sample_out_manual

like image 71
deepseefan Avatar answered Nov 27 '25 19:11

deepseefan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!