Biostar Beta. Not for public use.
error with add_theme function ggplot2 (R)
0
Entering edit mode
12 months ago
tw617 • 30

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 • 514 views
ADD COMMENTlink
0
Entering edit mode

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
0
Entering edit mode

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
0
Entering edit mode

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

ADD REPLYlink
0
Entering edit mode

Just check '+' signs in your code.

ADD REPLYlink
5
Entering edit mode
14 months ago
aays • 140
Canada

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
1
Entering edit mode

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
0
Entering edit mode

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

ADD REPLYlink

Login before adding your answer.

Similar Posts
Loading Similar Posts
Powered by the version 2.1