Home > OS >  Cannot produce grouped boxplots with a logarithmic scale
Cannot produce grouped boxplots with a logarithmic scale

Time:11-09

I am having problems making a grouped boxplot with a logarithmic scale. I think it is because to can't take a log of a factor ? Can anyone see any work arounds?
I have pasted the wrong boxplot below.

ggplot(subset(LOD_nA_nzeros ,biosample=="preserved saliva"), 
     aes(x=input_copies_ul, y=Cq, color=method,group=method))  
  geom_boxplot( ) 
  geom_point(position=position_jitterdodge()) 
  scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x)))  
  theme_minimal() 
  theme(text=element_text(size = 20))

enter image description here

## dput(LOD_nA_nzeros)
LOD_nA_nzeros <- structure(list(method = c("vantage", "vantage", "vantage", "vantage",
"vantage", "vantage", "vantage", "vantage", "vantage", "vantage",
"vantage", "vantage", "vantage", "vantage", "vantage", "vantage",
"vantage", "vantage", "vantage", "vantage", "vantage", "vantage",
"vantage", "vantage", "vantage", "vantage", "vantage", "vantage",
"vantage", "vantage", "vantage", "vantage", "vantage", "vantage",
"vantage", "vantage", "vantage", "vantage", "vantage", "vantage",
"vantage", "vantage", "vantage", "QIA", "QIA", "QIA", "QIA",
"QIA", "QIA", "QIA", "QIA", "QIA", "QIA", "QIA", "QIA", "QIA",
"QIA", "QIA", "QIA", "QIA", "QIA", "QIA", "QIA", "QIA", "QIA",
"manual", "manual", "manual", "manual", "manual", "manual", "manual",
"manual", "manual", "manual", "manual", "manual", "manual", "manual",
"manual", "manual", "manual", "manual", "manual", "manual", "manual",
"manual", "manual", "manual", "manual", "manual", "manual"),
input_pg_ul = c(1000, 1000, 1000, 1000, 100, 100, 100, 100,
10, 10, 10, 10, 1, 1, 1, 0.1, 0.001, 0.001, 1000, 1000, 1000,
1000, 100, 100, 100, 100, 10, 10, 10, 10, 1, 1, 1, 1, 0.1,
0.1, 0.1, 0.01, 0.01, 0.01, 0.001, 0.001, 0.001, 1000, 1000,
1000, 1000, 100, 100, 100, 100, 10, 10, 10, 10, 1, 1000,
1000, 1000, 1000, 100, 100, 100, 100, 10, 1000, 1000, 1000,
1000, 100, 100, 100, 100, 10, 10, 10, 10, 1000, 1000, 1000,
1000, 100, 100, 100, 100, 10, 10, 10, 10, 1, 1, 1), input_copies_ul = c(6.3e 09,
6.3e 09, 6.3e 09, 6.3e 09, 6.3e 08, 6.3e 08, 6.3e 08, 6.3e 08,
6.3e 07, 6.3e 07, 6.3e 07, 6.3e 07, 6300000, 6300000, 6300000,
630000, 6300, 6300, 6.3e 09, 6.3e 09, 6.3e 09, 6.3e 09, 6.3e 08,
6.3e 08, 6.3e 08, 6.3e 08, 6.3e 07, 6.3e 07, 6.3e 07, 6.3e 07,
6300000, 6300000, 6300000, 6300000, 630000, 630000, 630000,
63000, 63000, 63000, 6300, 6300, 6300, 6.3e 09, 6.3e 09,
6.3e 09, 6.3e 09, 6.3e 08, 6.3e 08, 6.3e 08, 6.3e 08, 6.3e 07,
6.3e 07, 6.3e 07, 6.3e 07, 6300000, 6.3e 09, 6.3e 09, 6.3e 09,
6.3e 09, 6.3e 08, 6.3e 08, 6.3e 08, 6.3e 08, 6.3e 07, 6.3e 09,
6.3e 09, 6.3e 09, 6.3e 09, 6.3e 08, 6.3e 08, 6.3e 08, 6.3e 08,
6.3e 07, 6.3e 07, 6.3e 07, 6.3e 07, 6.3e 09, 6.3e 09, 6.3e 09,
6.3e 09, 6.3e 08, 6.3e 08, 6.3e 08, 6.3e 08, 6.3e 07, 6.3e 07,
6.3e 07, 6.3e 07, 6300000, 6300000, 6300000), biosample = c("fresh saliva",
"fresh saliva", "fresh saliva", "fresh saliva", "fresh saliva",
"fresh saliva", "fresh saliva", "fresh saliva", "fresh saliva",
"fresh saliva", "fresh saliva", "fresh saliva", "fresh saliva",
"fresh saliva", "fresh saliva", "fresh saliva", "fresh saliva",
"fresh saliva", "preserved saliva", "preserved saliva", "preserved saliva",
"preserved saliva", "preserved saliva", "preserved saliva",
"preserved saliva", "preserved saliva", "preserved saliva",
"preserved saliva", "preserved saliva", "preserved saliva",
"preserved saliva", "preserved saliva", "preserved saliva",
"preserved saliva", "preserved saliva", "preserved saliva",
"preserved saliva", "preserved saliva", "preserved saliva",
"preserved saliva", "preserved saliva", "preserved saliva",
"preserved saliva", "preserved saliva", "preserved saliva",
"preserved saliva", "preserved saliva", "preserved saliva",
"preserved saliva", "preserved saliva", "preserved saliva",
"preserved saliva", "preserved saliva", "preserved saliva",
"preserved saliva", "preserved saliva", "fresh saliva", "fresh saliva",
"fresh saliva", "fresh saliva", "fresh saliva", "fresh saliva",
"fresh saliva", "fresh saliva", "fresh saliva", "fresh saliva",
"fresh saliva", "fresh saliva", "fresh saliva", "fresh saliva",
"fresh saliva", "fresh saliva", "fresh saliva", "fresh saliva",
"fresh saliva", "fresh saliva", "fresh saliva", "preserved saliva",
"preserved saliva", "preserved saliva", "preserved saliva",
"preserved saliva", "preserved saliva", "preserved saliva",
"preserved saliva", "preserved saliva", "preserved saliva",
"preserved saliva", "preserved saliva", "preserved saliva",
"preserved saliva", "preserved saliva"), sample = c("Fr Saliva 1ng/ul",
"Fr Saliva 1ng/ul", "Fr Saliva 1ng/ul", "Fr Saliva 1ng/ul",
"Fr Saliva 100pg/ul", "Fr Saliva 100pg/ul", "Fr Saliva 100pg/ul",
"Fr Saliva 100pg/ul", "Fr Saliva 10pg/ul", "Fr Saliva 10pg/ul",
"Fr Saliva 10pg/ul", "Fr Saliva 10pg/ul", "Fr Saliva 1pg/ul",
"Fr Saliva 1pg/ul", "Fr Saliva 1pg/ul", "Fr Saliva 100fg/ul",
"Fr Saliva 1fg/ul", "Fr Saliva 1fg/ul", "Pr Saliva 1ng/ul",
"Pr Saliva 1ng/ul", "Pr Saliva 1ng/ul", "Pr Saliva 1ng/ul",
"Pr Saliva 100pg/ul", "Pr Saliva 100pg/ul", "Pr Saliva 100pg/ul",
"Pr Saliva 100pg/ul", "Pr Saliva 10pg/ul", "Pr Saliva 10pg/ul",
"Pr Saliva 10pg/ul", "Pr Saliva 10pg/ul", "Pr Saliva 1pg/ul",
"Pr Saliva 1pg/ul", "Pr Saliva 1pg/ul", "Pr Saliva 1pg/ul",
"Pr Saliva 100fg/ul", "Pr Saliva 100fg/ul", "Pr Saliva 100fg/ul",
"Pr Saliva 10fg/ul", "Pr Saliva 10fg/ul", "Pr Saliva 10fg/ul",
"Pr Saliva 1fg/ul", "Pr Saliva 1fg/ul", "Pr Saliva 1fg/ul",
"Pr Saliva 1ng/ul", "Pr Saliva 1ng/ul", "Pr Saliva 1ng/ul",
"Pr Saliva 1ng/ul", "Pr Saliva 100pg/ul", "Pr Saliva 100pg/ul",
"Pr Saliva 100pg/ul", "Pr Saliva 100pg/ul", "Pr Saliva 10pg/ul",
"Pr Saliva 10pg/ul", "Pr Saliva 10pg/ul", "Pr Saliva 10pg/ul",
"Pr Saliva 1pg/ul", "Fr Saliva 1ng/ul", "Fr Saliva 1ng/ul",
"Fr Saliva 1ng/ul", "Fr Saliva 1ng/ul", "Fr Saliva 100pg/ul",
"Fr Saliva 100pg/ul", "Fr Saliva 100pg/ul", "Fr Saliva 100pg/ul",
"Fr Saliva 10pg/ul", "Fr Saliva 1ng/ul", "Fr Saliva 1ng/ul",
"Fr Saliva 1ng/ul", "Fr Saliva 1ng/ul", "Fr Saliva 100pg/ul",
"Fr Saliva 100pg/ul", "Fr Saliva 100pg/ul", "Fr Saliva 100pg/ul",
"Fr Saliva 10pg/ul", "Fr Saliva 10pg/ul", "Fr Saliva 10pg/ul",
"Fr Saliva 10pg/ul", "Pr Saliva 1ng/ul", "Pr Saliva 1ng/ul",
"Pr Saliva 1ng/ul", "Pr Saliva 1ng/ul", "Pr Saliva 100pg/ul",
"Pr Saliva 100pg/ul", "Pr Saliva 100pg/ul", "Pr Saliva 100pg/ul",
"Pr Saliva 10pg/ul", "Pr Saliva 10pg/ul", "Pr Saliva 10pg/ul",
"Pr Saliva 10pg/ul", "Pr Saliva 1pg/ul", "Pr Saliva 1pg/ul",
"Pr Saliva 1pg/ul"), Cq.Values = c("15.51 ", "15.61 ", "14.46 ",
"14.79 ", "22.41 ", "22.77 ", "21.84 ", "22.34 ", "28.17 ",
"28.58 ", "27.98 ", "28.60 ", "34.26 ", "31.81 ", "31.88 ",
"34.18 ", "33.21 ", "32.89 ", "17.24 ", "17.75 ", "17.70 ",
"17.77 ", "25.01 ", "24.85 ", "24.45 ", "24.64 ", "30.94 ",
"30.58 ", "28.53 ", "28.85 ", "28.27 ", "27.53 ", "30.99 ",
"31.75 ", "33.94 ", "32.84 ", "32.97 ", "36.18 ", "29.82 ",
"29.30 ", "35.25 ", "37.97 ", "34.87 ", "14.57 ", "14.68 ",
"14.78 ", "14.83 ", "21.40 ", "21.34 ", "21.77 ", "21.59 ",
"28.04 ", "27.94 ", "28.38 ", "28.35 ", "34.53 ", "20.52 ",
"20.22 ", "20.89 ", "20.71 ", "25.84 ", "25.86 ", "26.61 ",
"26.44 ", "34.62 ", "14.53 ", "14.31 ", "14.56 ", "14.36 ",
"21.74 ", "21.64 ", "21.81 ", "22.29 ", "27.56 ", "28.74 ",
"29.52 ", "29.07 ", "13.41 ", "12.71 ", "13.79 ", "13.97 ",
"20.85 ", "20.92 ", "20.56 ", "20.58 ", "28.12 ", "28.22 ",
"28.76 ", "28.36 ", "36.65 ", "35.18 ", "39.67 "), Cq = c(15.51,
15.61, 14.46, 14.79, 22.41, 22.77, 21.84, 22.34, 28.17, 28.58,
27.98, 28.6, 34.26, 31.81, 31.88, 34.18, 33.21, 32.89, 17.24,
17.75, 17.7, 17.77, 25.01, 24.85, 24.45, 24.64, 30.94, 30.58,
28.53, 28.85, 28.27, 27.53, 30.99, 31.75, 33.94, 32.84, 32.97,
36.18, 29.82, 29.3, 35.25, 37.97, 34.87, 14.57, 14.68, 14.78,
14.83, 21.4, 21.34, 21.77, 21.59, 28.04, 27.94, 28.38, 28.35,
34.53, 20.52, 20.22, 20.89, 20.71, 25.84, 25.86, 26.61, 26.44,
34.62, 14.53, 14.31, 14.56, 14.36, 21.74, 21.64, 21.81, 22.29,
27.56, 28.74, 29.52, 29.07, 13.41, 12.71, 13.79, 13.97, 20.85,
20.92, 20.56, 20.58, 28.12, 28.22, 28.76, 28.36, 36.65, 35.18,
39.67)), class = "data.frame", row.names = c(NA, -92L))

CodePudding user response:

We don't know your goal so "cannot produce," "having problems," "wrong" don't tell us what result you're aiming for / expecting.

Maybe you wanted groups along the x axis, not just one group for each method? We might do that with the code below. group = interaction(method, floor(log10(input_copies_ul))) will make groups for each method for each power of ten along the x axis.

library(ggplot2); library(scales)
ggplot(subset(LOD_nA_nzeros ,biosample=="preserved saliva"), 
       aes(x=input_copies_ul, y=Cq, color=method,group=method))  
  geom_boxplot(aes(group = interaction(method, floor(log10(input_copies_ul)))), 
               position = position_dodge(preserve = "single")) 
  geom_point(position=position_jitterdodge()) 
  scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x)))  
  theme_minimal() 
  theme(text=element_text(size = 20))

enter image description here

  • Related