Note that there are some explanatory texts on larger screens.

plurals
  1. POggplot2 throws error inside function, but not outside of function
    primarykey
    data
    text
    <p><strong>UPDATE:</strong> In response to a very reasonable comment about code readability, I have restructured the code blocks.</p> <p>I am attempting to map Census data for each tract in Colorado and I have encountered something that I don't know how to explain. (The shapefile can be found <a href="http://www.census.gov/geo/maps-data/data/tiger-line.html" rel="nofollow noreferrer">here</a>, and the Summary File 1 data was pulled via the <a href="http://www.census.gov/developers/" rel="nofollow noreferrer">Census API</a>.) After wrestling with the data, I successfully plotted total population with the following code:</p> <pre><code>#Set theme for histograms theme_hist &lt;- list(theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank(), panel.background = element_blank(), plot.background = element_blank(), panel.border = element_blank(), plot.title = element_text(size=22))) #Plot map pop_dist&lt;-ggplot(aes(x=long,y=lat,group=group,fill=as.numeric(tot_pop)),data=co_mapd) + geom_polygon(colour='white',size=.2) + coord_equal() + theme_opts + labs(title='Distribution of Population') + scale_fill_gradient2(low='#ECE7F2',mid='#A6BDDB',high='#3300CC', midpoint=median(as.numeric(co_mapd$tot_pop))) #ggsave('co_tract_pop_2010_map.png') #Plot histogram pop_hist&lt;-ggplot(aes(x=as.numeric(tot_pop),group=group,fill=as.numeric(tot_pop)),data=co_mapd) + geom_histogram() + theme_hist + xlab('Population Bins') + scale_fill_gradient2(low='#ECE7F2',mid='#A6BDDB',high='#3300CC', midpoint=median(as.numeric(co_mapd$tot_pop))) #ggsave('co_tract_pop_2010_hist.png') #Throw plots on a single canvas grid.arrange(pop_dist,pop_hist) </code></pre> <p>This generated the map and histogram plots below: <img src="https://i.stack.imgur.com/V05P9.jpg" alt="First Output"> <img src="https://i.stack.imgur.com/f30R4.png" alt="enter image description here"></p> <p>I thought this was perfect for what I was trying to do, but I have many more variables. A function would be useful. So, here is my function:</p> <pre><code> map_var&lt;-function(data,var,ttl){ dist&lt;-ggplot(aes(x=long,y=lat,group=group,fill=var),data=data) + geom_polygon(colour='white',size=.2) + coord_equal() + theme_opts + labs(title=ttl) + scale_fill_gradient2(low='#ECE7F2',mid='#A6BDDB',high='#3300CC', midpoint=median(data$var)) hist&lt;-ggplot(aes(x=var,group=group,fill=var),data=data) + geom_histogram() + theme_hist + xlab('Bins') + scale_fill_gradient2(low='#ECE7F2',mid='#A6BDDB',high='#3300CC', midpoint=median(data$var)) grid.arrange(dist,hist) } map_var(co_mapd,tot_pop,'Distribution of Population') </code></pre> <p>Seems like it should work because I tried to mirror exactly what I had done outside of my function wrapper. I literally copy and pasted, and just changed the elements where function parameters came into play. However, it ends up throwing the following error instead:</p> <pre><code>Error in data.frame(x = c(-107.48212, -107.482115, -107.482062, -107.48206, : arguments imply differing number of rows: 748351, 0 </code></pre> <p>I should also mention that prior to running it through the function, I converted all relevant columns to numeric because as.numeric() within the function was giving me some issues.</p> <p>In any event, I am not clear on why the arguments would imply a differing number of rows within the function, but not when the plotting code stands alone. It makes me think that something funky is going on with access to appropriate environments, but I am not sure what. Any help would be greatly appreciated.</p> <p><strong>UPDATE:</strong> I also tried to go the aes_string route (which I was previously unaware of so thank you). Either that wasn't the issue, or I have misapplied the technique.</p> <pre><code>map_var&lt;-function(data,var,ttl,coord_x,coord_y,group='group'){ dist&lt;-ggplot(aes_string(x=coord_x,y=coord_y,group=group,fill=var),data=data) + geom_polygon(colour='white',size=.2) + coord_equal() + theme_opts + labs(title=ttl) + scale_fill_gradient2(low='#ECE7F2',mid='#A6BDDB',high='#3300CC') hist&lt;-ggplot(aes(x=var,group=group,fill=var),data=data) + geom_histogram() + theme_hist + xlab('Bins') + scale_fill_gradient2(low='#ECE7F2',mid='#A6BDDB',high='#3300CC') grid.arrange(dist,hist) } map_var(co_mapd,var='tot_pop',ttl='Distribution of Population',coord_x='long',coord_y='lat') </code></pre> <p>This version throws the following exception...</p> <pre><code>Don't know how to automatically pick scale for object of type function. Defaulting to continuous Error in data.frame(x = function (x, y = NULL, na.rm = FALSE, use) : arguments imply differing number of rows: 0, 748351 </code></pre> <p>I am still not clear on what argument I am using that implies anything other than the full data.frame (in addition to being unclear why it only matters inside of a function).</p> <p><strong>UPDATE (8/20)</strong>: I am adding an updated code block to incorporate the eval() suggestion.</p> <pre><code>map_var&lt;-function(data,var,ttl,coord_x,coord_y,group='group'){ dist&lt;-ggplot(aes_string(x=eval(coord_x),y=eval(coord_y),group=eval(group),fill=eval(var)),data=data) + geom_polygon(colour='white',size=.2) + coord_equal() + theme_opts + labs(title=eval(ttl)) + scale_fill_gradient2(low='#ECE7F2',mid='#A6BDDB',high='#3300CC') hist&lt;-ggplot(aes(x=eval(var),group=eval(group),fill=eval(var)),data=data) + geom_histogram() + theme_hist + xlab('Bins') + scale_fill_gradient2(low='#ECE7F2',mid='#A6BDDB',high='#3300CC') grid.arrange(dist,hist) } map_var(co_mapd,var='tot_pop',ttl='Distribution of Population',coord_x='long',coord_y='lat') </code></pre> <p>I wanted to share explicitly in case I executed poorly. I am afraid, however, that it has not changed the exception.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload