0

I'm trying to plot some results from models based of unmarked occupancy frames on graphs using the ggplot2 package. For a dataset of 39 transects, there was no error message and the same piece of code as I have below produced a nice set of graphs. However, I'm now running the code with models constructed from a dataset of 111 samples. Only now do I have an error message, which rather suggests the problem is due to the size of the dataset!

#THIS IS THE CODE THAT WORKS/PRODUCES GRAPH
(wf_RAN <- update(m.RAN, formula = ~ 1 ~ Rang_Dist))
preddata <- predict(wf_RAN, type = "state", appendData = TRUE)

wf_RAN=qplot(Site.Covs.Wolves1$Rang_Dist, Predicted, data = preddata, geom = "line", 
             xlab = "Distance from Ranger Post (km)", ylab = "Estimated occupancy", ylim=c(0, 1)) +
  geom_ribbon(aes(x = Site.Covs.Wolves1$Rang_Dist, ymin = lower, ymax = upper), alpha = 0.1) +
  theme_bw()+#sets the background of the plot to white
  theme(axis.text=element_text(size=13),axis.line=element_line(colour="black"),
        axis.title=element_text(size=15,vjust=0.8),
        panel.grid.major = element_blank(),#remove grid lines in background
        panel.grid.minor = element_blank(),
        legend.text=element_text(size=25),
        panel.border = element_blank())
wf_RAN

# Wolf Habitat Use and distance from ranger post (proxy for prey availability)
#Code below does not produce a graph i.e. is the larger dataset of same format
(wf_RAN2 <- update(m.RAN2, formula = ~ 1 ~ Rang_Dist))
preddata <- predict(wf_RAN2, type = "state", appendData = TRUE)

wf_RAN2=qplot(Site.Covs.Wolves1$Rang_Dist, Predicted, data = preddata, geom = "line", 
             xlab = "Distance from Ranger Post (km)", ylab = "Estimated occupancy", ylim=c(0, 1)) +
  geom_ribbon(aes(x = Site.Covs.Wolves1$Rang_Dist, ymin = lower, ymax = upper), alpha = 0.1) +
  theme_bw()+#sets the background of the plot to white
  theme(axis.text=element_text(size=13),axis.line=element_line(colour="black"),
        axis.title=element_text(size=15,vjust=0.8),
        panel.grid.major = element_blank(),#remove grid lines in background
        panel.grid.minor = element_blank(),
        legend.text=element_text(size=25),
        panel.border = element_blank())
wf_RAN2 

Error: Aesthetics must be either length 1 or the same as the data (39): x

With the dataset of 39 I produced a line graph. The datasets are of the same format just that the second one is larger.

My key question therefore is, what are 'aesthetics' - are they referring to the graph axes? How do I adjust them so that I can plot graphs with a larger dataset?

Or, have I completely misunderstood this problem?

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
chris1
  • 11
  • 3
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 07 '19 at 14:50
  • In general, do not use `$` when working within the `tidyverse` which includes `ggplot2`. For `ggplot2`, stick to : `ggplot(data_set,aes(column_name,column_name,..)) + geom_*`. – NelsonGon Oct 07 '19 at 14:59
  • 1
    `aesthetics` are how variables in the data set are mapped to aesthetic elements of the graphic. This includes horizontal (x-axis) and vertical (y-axis) positioning, as well as things like color and shape. One glaring issue in your code is that you are referring to variables not in the data set you're plotting, using the `$`. It's not clear if that's the problem, because it works in the first example, but it's possible, because this error is when the length of the variables you're using as aesthetics don't match. Try putting all your variables in the same data set and remove the `$` usage. – Aaron left Stack Overflow Oct 07 '19 at 15:03

0 Answers0