Question: error with add_theme function ggplot2 (R)
0
gravatar for tw617
2.3 years ago by
tw61740
tw61740 wrote:

I am trying to make a density plot but I keep getting this same error and can't seem to figure out what is wrong. This code produced a pdf that has 4 empty, labeled boxes and no legend.

 > #density plot, use data 3 to draw density plot
> library(reshape)
> library(ggplot2)
> library(grid)
> sdata <- melt.data.frame(data3,variable_name="sample")
Using  as id variables
> colnames(sdata) <- c("sample","log2FPKM")
> pdf("densityplot.25Kall_genes.fpkm_tracking_filtered.pdf")
> 
> sp<-ggplot(sdata, aes(log2FPKM, fill = sample))
> geom_density(aes(group=sample, colour=sample, fill=sample), alpha=0.3)
mapping: group = sample, colour = sample, fill = sample 
geom_density: na.rm = FALSE
stat_density: na.rm = FALSE
position_identity 
> 
> sp2<-sp+theme(legend.key.size = unit(0.15, "cm")) +
+ theme(legend.text = element_text(size = 4)) + facet_wrap(~sample)
> +theme(legend.key.size=unit(0.9, "cm")) + theme(legend.text = 
+ element_text(size = 10)) + theme(strip.text.x = element_text(size = 
+ 15), axis.text = element_text(size = 8))
****Error in add_theme(e1, e2, e2name) : 
  argument "e2" is missing, with no defaul**t**
> sp2
> dev.off()
R • 828 views
ADD COMMENTlink modified 2.3 years ago by aays140 • written 2.3 years ago by tw61740

I think there is a typo on line where you call ggplot first time, end of the line + is missing? sp<-ggplot(sdata, aes(log2FPKM, fill = sample)) +

ADD REPLYlink written 2.3 years ago by zx87549.7k

It looks like you have an extra plus here :

 sp2<-sp+theme(legend.key.size = unit(0.15, "cm")) **+** + theme(legend.text = element_text(size = 4)) + facet_wrap(~sample)
ADD REPLYlink modified 2.3 years ago • written 2.3 years ago by Titus910

No, extra plus is coming from R console saying they are actually all in one row of code.

ADD REPLYlink written 2.3 years ago by zx87549.7k

Just check '+' signs in your code.

ADD REPLYlink written 2.3 years ago by Dattatray Mongad350
5
gravatar for aays
2.3 years ago by
aays140
Canada
aays140 wrote:

I think the issue wrt your error is happening here:

> sp2<-sp+theme(legend.key.size = unit(0.15, "cm")) +
+ theme(legend.text = element_text(size = 4)) + facet_wrap(~sample)

> +theme(legend.key.size=unit(0.9, "cm")) + theme(legend.text = 
+ element_text(size = 10)) + theme(strip.text.x = element_text(size = 
+ 15), axis.text = element_text(size = 8))

The second command doesn't make much sense to R on its own, without sp2 at the beginning. ggplot simply doesn't know what to add the theme objects in the second command to - hence the add_theme error.

Also worth noting: a sometimes error-inducing idiosyncrasy of ggplot is that in a chain of commands, each line has to _end_ with a +, while a + at the _start_ of a line will break your code. For instance, while

ggplot(d, aes(x, y)) +
geom_point() +
theme_bw()

is valid, the following command:

ggplot(d, aes(x, y)) +
geom_point()
+ theme_bw()

is not.

Similarly, the reason you aren't seeing your data show up is because these two commands:

> sp<-ggplot(sdata, aes(log2FPKM, fill = sample))
> geom_density(aes(group=sample, colour=sample, fill=sample), alpha=0.3)

are being interpreted separately. This is why the second command returns the specified mapping information to stdout as opposed to actually adding it to your plot object. Without having a geom as part of the plot object, ggplot simply doesn't know how to plot the data - hence the empty plot.

Instead, running the above as one command should fix the issue:

> sp<-ggplot(sdata, aes(log2FPKM, fill = sample)) +
  geom_density(aes(group=sample, colour=sample, fill=sample), alpha=0.3)

Hope this helps!

ADD COMMENTlink modified 2.3 years ago • written 2.3 years ago by aays140
1

Thank you!!! These errors can be hard to localize sometimes and this had me stumped for quite some time. density plot

enter image description here2k4tewj1/

ADD REPLYlink modified 2.3 years ago • written 2.3 years ago by tw61740

Happy to help! And thanks for sharing the plot - looks fantastic :)

ADD REPLYlink written 2.3 years ago by aays140
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1874 users visited in the last hour