0

I am having trouble getting my plots to work, I have multiple categorical variables by which I want to color by one, and facet by another. However, R keeps adding the "values" (I used melt) for the same variables together instead. It works when I only have one variable.

Here is my plot with one variable Here is my plot with one variable

Here is my plot with two variables, you can see the adding that is happening Here is my plot with two variables, you can see the adding that is happening:

simple dataframe

Here is my code:

library(reshape2)
library(ggplot2)

test2 <- structure(list(SampleID = c(12.19, 12.22, 13.1, 12.19, 12.22, 
13.1, 12.19, 12.22, 13.1, 12.19, 12.22, 13.1), patient = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), type = structure(c(1L, 
1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L), .Label = c("L", 
"T"), class = "factor"), timepoint = structure(c(1L, 2L, 2L, 
1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L), .Label = c("1", "2"), class = "factor"), 
    Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L), .Label = "D", class = "factor"), variable = structure(c(1L, 
    1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("A", 
    "B", "C", "D", "E", "F", "G", "H", "I"), class = "factor"), 
    value = c(2L, 5L, 6L, 25L, 18L, 12L, 6L, 10L, 15L, 21L, 23L, 
    33L)), .Names = c("SampleID", "patient", "type", "timepoint", 
"Group", "variable", "value"), row.names = c(NA, 12L), class = "data.frame")

ggplot(test2, aes(test2$variable, test2$value, fill=test2$timepoint)) +
  geom_bar(stat="identity", position = "dodge") +
  scale_fill_manual(values=c("rosybrown1", "steelblue2", "gray")) +
  labs(x="Category", y="Count", title = paste0("Sample ", as.character(unique(test2$patient)) , " - " , as.character(unique(test2$Group)))) +
  facet_wrap(~test2$type) +
  theme(text = element_text(size=15),
    axis.text.x = element_text(angle = 90, hjust = 1, vjust=.5, size = 7))
trhood
  • 13
  • 4
  • Which variable(s) do you want to colour by?! Also please add sample data using `dput(head(df,12))` rather than images. – NelsonGon Mar 07 '19 at 03:23
  • @NelsonGon, i added the structure. I wanted to color by timepoint, and facet by type. However, for this sample, there are timepoints 1 & 2 for type L, and only timepoint 2 for type T – trhood Mar 07 '19 at 03:27
  • 3
    No need for `$` here: `aes(test2$variable, test2$value, fill=test2$timepoint)` – NelsonGon Mar 07 '19 at 03:29
  • 1
    wow, that was it? thank you! – trhood Mar 07 '19 at 03:31
  • I'm not sure if it was the problem as I was still looking at it but that came to mind first. – NelsonGon Mar 07 '19 at 03:32
  • 2
    I'd go one step further from what @NelsonGon already pointed out: Don't ever use `$` indexing inside `aes`, see [Issue when passing variable with dollar sign notation ($) to aes() in combination with facet_grid() or facet_wrap()](https://stackoverflow.com/questions/32543340/issue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combinatio). Concerning the x axis range, are you perhaps after `... + facet_wrap(~ type, scales = "free_x") + ...`? – Maurits Evers Mar 07 '19 at 04:37

1 Answers1

0

If I am understanding right, it looks like you just need to give the scales option to facet_wrap like so:

facet_wrap(~type, scales = "free_x")
cdtip
  • 153
  • 7