Question

I have looked through the previous posts for an answer to this, but have either run into code that doesn't suit my situation or similar questions that didn't provide example data. At any rate, here is a small portion of my data below:

Example Data

Long story short, I am attempting to plot smoothed binary strings from individual cases on top of a scatterplot of the means from all previous administrations ($Total), and the mean of all cases from a recent administration ($Admin). I can accomplish this with the following bit of code by selecting a single case from the list:

library(ggplot2)
vars <- select.list(names(Ex),multiple=TRUE,graphics=TRUE)
Cases<-subset(Ex,select=vars)
for(i in Cases){
  iplot<-ggplot(Ex,aes(x=Total,y=i))
} 
iplot+geom_smooth(method=loess,size=1,colour="orange",linetype=1,fill="orange",se=T)+
  geom_point(aes(y=Admin),colour="dark green",size=1.5)+ylim(0,1)+xlim(0,1)+
  labs(x="Expected",y="Admin",title=vars)

For example, if you select case A from the list you get a scatterplot of $Total & $Admin in dark green with a loess smoothing of the case A binary data in orange.

This is great if I know the cases I want to look at, or have sufficiently small numbers of cases like my example. However, I can easily approach 500+ cases in actual application of this process, so I would like to generate a faceted output of graphs like the one above that will iterate across all cases in my data (i.e. simultaneously generate the graphs for cases A-D in the example data) so I can easily identify/select cases of further interest.

I have attempted this with the following code:

for(i in Ex){
  iplot<-ggplot(Ex,aes(x=Total,y=i))
} 
iplot+geom_smooth(method=loess,size=1,colour="orange",linetype=1,fill="orange",se=T)+
  geom_point(aes(y=Admin),colour="dark green",size=1.5)+ylim(0,1)+xlim(0,1)+
  facet_wrap(~vars,ncol=3)+
  labs(x="Expected",y="Admin",title=vars)

...and many other attempts, but I always seem to come back to this error message:

Error in layout_base(data, vars, drop = drop) : At least one layer must contain all variables used for facetting

What am I missing?

Was it helpful?

Solution

If I understand you correctly, you want the loess curves to be based on the binary data (??). If so, then this seems be what you are asking for.

Ex <- read.csv("StackOverflowEx.csv")
library(ggplot2)
library(reshape2)    # for melt(...)
vars <- select.list(names(Ex),multiple=TRUE,graphics=TRUE)
Cases<-subset(Ex,select=vars)

gg <- melt(Cases,id=c("Item","Total","Admin"))
ggplot(gg, aes(x=Total,y=Admin))+
  geom_point(colour="dark green",size=1.5)+
  geom_point(aes(y=value,color=variable))+
  geom_smooth(aes(y=value,fill=variable),
              method=loess,size=1,linetype=1,se=T)+
  facet_wrap(~variable)+
  ylim(0,1)+
  labs(x="Expected",y="Admin",title=vars)

This plot is generated by selecting all the columns (Item, Total, Admin, and A-D).

Note that the error regions are "clipped". This is because you set ylim(0,1).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top