0

I'm trying to create a series of boxplots between 3 groups which are graded on severeity of infection. This is the code I'm using:

pl <- ggplot(df.humann2.sub.all4, aes(x=PEDIS, y=PathAbundance, fill=PEDIS, group=PEDIS)) 
pl <- pl + geom_boxplot()
pl <- pl + facet_wrap(~ variable)
pl + scale_x_discrete()
pl + scale_x_discrete(limits=2:4)

Sample image

However as you can see the legend is a continous scale instead of discrete groupings, how can I change this? Also question 2, you can see that one of the pathway titles is cut off, can I change the size of the plots or the size of the font titles to make sure the whole pathway name is visible?

Output of

dput(head)df.humann2.sub.all4,10))

structure(list(PEDIS = c(2L, 2L, 2L, 3L, 4L, 3L, 4L, 2L, 3L, 
2L), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = c("PWY.6385..peptidoglycan.biosynthesis.III..mycobacteria.", 
"PWY.7221..guanosine.ribonucleotides.de.novo.biosynthesis", "PWY.7219..adenosine.ribonucleotides.de.novo.biosynthesis", 
"PWY.7228..superpathway.of.guanosine.nucleotides.de.novo.biosynthesis.I", 
"COA.PWY..coenzyme.A.biosynthesis.I"), class = "factor"), PathAbundance = c(0.239458, 
1.3588, 6.00215, 0.371471, 1179.06, 0, 334.182, 96.3111, 55.3418, 
8.63144)), row.names = c(NA, 10L), class = "data.frame")

Updated plot

mrad
  • 173
  • 1
  • 11
  • Change PEDIS to a factor and you should be all good. – cardinal40 Feb 11 '20 at 21:22
  • 2
    Does this answer your question? [R ggplot2: legend should be discrete and not continuous](https://stackoverflow.com/questions/20301922/r-ggplot2-legend-should-be-discrete-and-not-continuous) – cardinal40 Feb 11 '20 at 21:23
  • @cardinal40 what do you mean by change it to a factor? Could you give me an example of how to write that? – mrad Feb 11 '20 at 21:24
  • I guess what @cardinal40 was telling you to do is: `aes(x = as.factor(PEDIS), y=PathAbundance, fill= as.factor(PEDIS))`. BTW, if you are doing boxplot, I don't think you need to pass `group` in your `aes`. – dc37 Feb 11 '20 at 21:29
  • @cardinal40 that doesn't seem to do the trick, in aes I've changed group to group=factor(PEDIS) but the resulting plot is no different – mrad Feb 11 '20 at 21:30
  • @dc37 trying that code I'm still getting the same continuous legend. – mrad Feb 11 '20 at 21:32
  • 1
    That does not make sense. Please provide a reproducible example of your dataset (see this link: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by posting in your question the output of `dput(head(df.humann2.sub.all4,10))`. – dc37 Feb 11 '20 at 21:34
  • @dc37 I've added the output to the original question – mrad Feb 11 '20 at 21:37
  • It works if you write: `ggplot(df, aes(x = as.factor(PEDIS), y = PathAbundance, fill = as.factor(PEDIS)))+ geom_boxplot()`. Here `df` is the name of your example on my session, so replace `df` by `df.humann2.sub.all4`. – dc37 Feb 11 '20 at 21:41
  • @dc37 that seemed to fix the legend but now I am having issues with the x axis, as you can see in the updated image in the original question above – mrad Feb 11 '20 at 22:04
  • remove all `scale_x_discrete`, you don't need them anymore. because basically with `scale_x_discrete(limits = 2:4)`, you are telling R to set limits for position 2 to 4 whereas the position of your factor is from 1 to 3 that's why you have this gap. Does it make sense ? – dc37 Feb 11 '20 at 22:11
  • @dc37 That did it! Just two more things, the x axis title and the legend tile is now "as.factor(PEDIS)", can I change those to "PEDIS" only? Also the pathway title in the 4th plot is slightly cutoff, is there anyway to fix that? – mrad Feb 11 '20 at 23:13
  • It is a different question. Take a look if this question was not previously address on SO and if not, you can create a new post – dc37 Feb 12 '20 at 00:07

0 Answers0