English 中文(简体)
在R.R.使用St.Binhex()和Coord_cartesian()将一个热图放在一个现有的格图上
原标题:Problems using stat_binhex() and coord_cartesian() to layer a heatmap on an existing ggmap in R

我有一份加利福尼亚地图。 奶粉编码按组群分类,从低端到高压,共分五组,并配有色。 我想做的是,在人口密度的基础上,将热图层置于这一位置上,但此时我无法做到这一点。

我知道,我需要根据人口密度来改变甲型六氯环己烷,这只是数据框架中的密度,但我不知道如何做到这一点。

任何帮助都将受到高度赞赏。 我在地图上有一层,但甲型六氯环己烷是恒定的,而不是因人口密度而异。

我的守则是:

第一,建立可使用的数据框架:

stack_reduced_lng=c(-118.2495, -118.2467, -118.2737, -118.3108, -118.3065, -118.2942, -118.2849, -118.3471, -118.3158, -118.2587, -118.2382, -118.2403, -118.2665, -118.3546, -118.2643, -118.3172, -118.3387, -118.3099, -118.1561, -118.1999)

stack_reduced_lat=c( 33.97398, 33.94901, 33.96411, 34.07621, 34.05912, 34.04801, 34.02809, 34.00956, 34.06211, 34.00714, 34.06599, 34.04481, 34.03939, 34.02872, 34.05291, 34.02887, 34.04864, 34.06639, 34.02453, 34.02276)

stack_reduced_density=c(  6595.9,  6721.1,  8016.2,  7668.7, 14151.8, 11981.7,  6640.7,  3337.9,  3226.8, 10014.5,  4361.8,  7227.4,  5112.6,  4886.0, 14737.8,  7131.7,  6408.5, 13386.6,  5905.2,  4266.7)

stack_reduced_value.level=c( "Lower.Value",     "Lower.Value",     "Lower.Value",     "Upper.Mid.Value", "Middle.Value",    "Low.Mid.Value",  "Low.Mid.Value",   "Middle.Value",    "Middle.Value",    "Lower.Value",     "Low.Mid.Value",   "Middle.Value", "Middle.Value",    "Low.Mid.Value",   "Middle.Value",    "Low.Mid.Value",   "Middle.Value",    "Low.Mid.Value", "Low.Mid.Value",   "Low.Mid.Value")

df=as.data.frame(cbind(stack_reduced_lng, stack_reduced_lat, stack_reduced_density, stack_reduced_value.level))

colnames(df)=c("lng", "lat", "density", "value.level")

接下来是制定原始清单的法典:

#### First, our center
la.center=c(lng=-118.2437, lat=34.0522)

#### We will create a vector of colors, to be names and used in all project maps.
map_colors=c("black", "yellow", "green", "blue", "red")

#### creating our map
socal_map=get_map(location=la.center, zoom=6, scale=4)
d=ggmap(socal_map)+
  geom_point(aes(x=lng, y=lat, color=as.factor(value.level)), data=df)
  #ggtitle("Map of Home Values By Zip Code For California")

#### Formatting the map
d=d+scale_color_manual(, name="Home Value Level", breaks=c("Lower.Value","Low.Mid.Value","Middle.Value", "Upper.Mid.Value", "Upper.Value"), labels=c("Lower Home Value", "Lower Middle Home Value", "Middle Home Value", "Upper Middle Home Value", "Upper Home Value"), values=map_colors)+labs(x="Longititude", y="Latitude", title="Map of Home Values by Zip Code for California")

上述法典制定了理想的地图,但我要给它增加一个热图层。

我在此试图:

d+coord_cartesian()+stat_binhex(
  aes(x=lng, y=lat, fill=density),
  size=1, bins=20, alpha=0.5,
  data=df
)

这给我的地图增加了一层,但甲型六氯环己烷的固定水平为0.5,因此与密度没有差别,这是我想要做的。 绘制的地图中,甲型六氯环己烷的浓度水平会随着密度的不同而变化。

Any help is greatly appreciated.

谢谢!

问题回答

You were almost there. The main problem is that your density values are strings. Also, you declare fill as density, but I m guessing you wanted it as value.level? Either way, this will give you a point to start from.

请注意,我没有APIC钥匙,因此这是一个普遍的解决办法。 如下评论:如果你这样做的话。

library(ggplot2)

# Convert density to integers
df$density <- as.numeric(df$density)

ggplot() +
  coord_cartesian() +
  stat_binhex(data = df,
              aes(x = lng, y = lat, 
                  fill = value.level,
                  alpha = density),
              # size = 1, # No discernible difference, may throw "deprecated" warning 
              bins = 20) +
  scale_alpha_continuous(range=c(0.1, 0.5)) +
  scale_fill_manual(values = c("#CC79A7", "#0072B2", "#E69F00", "#999999"))

“result”/





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签