Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot facet_wrap error when using `ROW` as column name

I have a data.table I'm trying to plot facets with ggplot and I'm getting the error listed below. Thanks in advance for any help.

require(data.table, ggplot2)

dt <- as.data.table(read.table(h=T, 
text="ROW mode resbin V1
0   RD   50.0  0
1   RD   50.0  0
2   RD   50.0  0
0   RD   33.3  0
1   RD   33.3  0                  
1   PV    7.5  1
2   PV    7.5  0
0   PV    6.0  1
1   PV    6.0  1
2   PV    6.0  1"))


ggplot(dt, aes(x = factor(resbin), y = V1, group=1)) + 
geom_point(aes(color=factor(mode)), size=3) +
geom_line() +
facet_wrap(~factor(ROW), ncol=2)

#Error in layout_base(data, vars, drop = drop) : 
#At least one layer must contain all variables used for facetting
like image 408
N8TRO Avatar asked Jul 31 '26 14:07

N8TRO


1 Answers

Something funny is going on that I don't fully understand. This seems to work for me:

dt <- as.data.table(read.table(h=T, 
text="grp mode resbin V1
0   RD   50.0  0
1   RD   50.0  0
2   RD   50.0  0
0   RD   33.3  0
1   RD   33.3  0                  
1   PV    7.5  1
2   PV    7.5  0
0   PV    6.0  1
1   PV    6.0  1
2   PV    6.0  1"))


ggplot(dt, aes(x = factor(resbin), y = V1)) + 
geom_point(aes(color=factor(mode)), size=3) +
geom_line(aes(group = 1)) +
facet_wrap(~grp, ncol=2)

I seemed to need to change the column name and remove factor() from it. But I only tested this briefly.

like image 76
joran Avatar answered Aug 03 '26 05:08

joran