0

I have data like:

2L      A=>C    6.2
2L      A=>G    13.6
2L      A=>T    6.7
2L      C=>A    5.3
2L      C=>G    3.8
2L      C=>T    12.6
2L      G=>A    14.1
2L      G=>C    4.3
2L      G=>T    5.5
2L      T=>A    10.3
2L      T=>C    12.6
2L      T=>G    5
2R      A=>C    5.1
2R      A=>G    11.2
2R      A=>T    9.4
2R      C=>A    4
2R      C=>G    4
2R      C=>T    11.6
2R      G=>A    17
2R      G=>C    4
2R      G=>T    6.9
2R      T=>A    9.1
2R      T=>C    12
2R      T=>G    5.8

And I'm trying to make separate bar plots for each chrom (data[,1]). I can plot it on the same plot:

library(ggplot2)

snps<-read.table("new.txt", header = FALSE)
chroms<-snps[,1]
trans<-snps[,2]
freq<-snps[,3]


ggplot(snps, aes(x = trans, y = freq, group = chroms, fill = chroms)) + geom_bar(position="dodge",stat="identity")

enter image description here

However I would like to be able to plot each chrom separately. When i try to use facet_grid I get the following error message:

ggplot(snps, aes(x = trans, y = freq, group = chroms, fill = chroms)) + 
geom_bar(position="dodge",stat="identity") + facet_grid(chroms ~ .)

---

Error in combine_vars(data, params$plot_env, rows, drop = params$drop) : 
At least one layer must contain all variables used for facetting

How can I correctly use facetting in this example?

2 Answers 2

1

I don't think you should create additional dataframes for your variables. This should work:

snps<-read.table("new.txt", header = FALSE)
colnames(snps)=c("chroms","trans","freq")
ggplot(snps, aes(x = trans, y = freq, group = chroms, fill = chroms)) + geom_bar(position="dodge",stat="identity")+facet_grid(chroms~.)

It gives me: enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

A facet_wrap() alternative:

df <- read.table(text="chroms trans freq
2L      A=>C    6.2
2L      A=>G    13.6
2L      A=>T    6.7
2L      C=>A    5.3
2L      C=>G    3.8
2L      C=>T    12.6
2L      G=>A    14.1
2L      G=>C    4.3
2L      G=>T    5.5
2L      T=>A    10.3
2L      T=>C    12.6
2L      T=>G    5
2R      A=>C    5.1
2R      A=>G    11.2
2R      A=>T    9.4
2R      C=>A    4
2R      C=>G    4
2R      C=>T    11.6
2R      G=>A    17
2R      G=>C    4
2R      G=>T    6.9
2R      T=>A    9.1
2R      T=>C    12
2R      T=>G    5.8", header=TRUE, stringsAsFactors=FALSE)


ggplot(df, aes(trans, freq, group=chroms, fill=chroms)) + 
  geom_bar(position="dodge", stat="identity") +
  facet_wrap(~chroms) +
  scale_y_continuous(expand=c(0,0)) +
  ggthemes::scale_fill_tableau() +
  theme_minimal() +
  theme(panel.grid.major.x=element_blank()) +
  theme(panel.spacing=unit(2, "lines")) +
  theme(legend.position="none")

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.