Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2在函数内部抛出错误,但不在函数外部抛出错误_R_Function_Map_Ggplot2 - Fatal编程技术网

R ggplot2在函数内部抛出错误,但不在函数外部抛出错误

R ggplot2在函数内部抛出错误,但不在函数外部抛出错误,r,function,map,ggplot2,R,Function,Map,Ggplot2,更新:为了回应关于代码可读性的非常合理的评论,我重新构造了代码块 我正试图绘制科罗拉多州每个地区的人口普查数据,我遇到了一些我不知道如何解释的问题。(可以找到shapefile,并通过提取摘要文件1数据。)在处理数据后,我用以下代码成功地绘制了总人口: #Set theme for histograms theme_hist <- list(theme(panel.grid.minor = element_blank(), panel.gri

更新:为了回应关于代码可读性的非常合理的评论,我重新构造了代码块

我正试图绘制科罗拉多州每个地区的人口普查数据,我遇到了一些我不知道如何解释的问题。(可以找到shapefile,并通过提取摘要文件1数据。)在处理数据后,我用以下代码成功地绘制了总人口:

#Set theme for histograms
theme_hist <- 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<-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<-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)
我还应该提到,在通过函数运行它之前,我将所有相关列转换为numeric,因为函数中的as.numeric()给了我一些问题

在任何情况下,我都不清楚为什么参数会暗示函数中的行数不同,但当绘图代码独立时就不清楚了。这让我觉得在访问合适的环境方面正在发生一些奇怪的事情,但我不确定是什么。任何帮助都将不胜感激

更新:我也尝试过使用aes_字符串路径(我以前不知道,所以谢谢)。要么这不是问题,要么我误用了技术

map_var<-function(data,var,ttl,coord_x,coord_y,group='group'){
    dist<-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<-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')
我仍然不清楚我使用的是什么参数,它暗示了除full data.frame以外的任何内容(此外,我还不清楚为什么它只在函数内部起作用)

更新(8/20):我正在添加一个更新的代码块以合并eval()建议

map_var<-function(data,var,ttl,coord_x,coord_y,group='group'){
    dist<-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<-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')

map\u var对于那些以后可能遇到此问题的人来说,这非常有效:

map_var<-function(data,var,ttl,coord_x,coord_y,group='group'){
    dist<-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<-ggplot(aes_string(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')

map\u var对于那些以后可能遇到此问题的人来说,这非常有效:

map_var<-function(data,var,ttl,coord_x,coord_y,group='group'){
    dist<-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<-ggplot(aes_string(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')

map\u varTry在函数中使用
aes\u string
(当然要传递字符串给它)。代码不错,但很难阅读。有关如何编写更清晰的代码的一些指南,请参见,这在征求反馈时非常重要。考虑在
+
符号后断行,以确保易读性。有时,我甚至会在每个新参数处换行(在
之后)。您仍然需要传递
aes\u string()
字符。您发布的代码有,例如,
aes\u字符串(x=coord\u x)
。这需要更改为
aes_string(x='coord_x')
。我是否通过函数调用中的字符串(最后一行)来满足这一要求?是和否。如果你想像这样传递字符串,你需要在eval中包装coord_x等。因此,例如,
map\u var
的第一行应该是
aes\u字符串(x=eval(coord\u x),…
尝试在函数中使用
aes\u字符串
(当然要将字符串传递给它)。代码很好,但很难阅读。有关如何编写更易读的代码的一些指南,请参阅,这在征求反馈时非常重要。请考虑在
+
符号后换行,以确保易读性。我甚至会在每个新参数(在
之后)换行,有时。您仍然需要传递
aes\u string()
字符。您发布的代码具有,例如,
aes\u string(x=coord\u x)
。这需要更改为
aes\u string(x='coord\u x')
。我是否通过函数调用中的字符串(最后一行)来满足此要求?是和否。如果您想传递这样的字符串,您需要在eval中包装coord_x等。因此,例如,
map_var
的第一行应该是
aes_字符串(x=eval(coord_x),…
map_var<-function(data,var,ttl,coord_x,coord_y,group='group'){
    dist<-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<-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')
map_var<-function(data,var,ttl,coord_x,coord_y,group='group'){
    dist<-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<-ggplot(aes_string(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')