ggplot中的渐变颜色(相对简单)

时间:2017-12-18 16:13:02

标签: r ggplot2

我有一个数据帧,我通过插入一系列原始目标点(它们与曾经在西雅图运行的循环共享方案有关)构建。

我已经调用了数据帧interpolated_flows:

line_id      long      lat seg_num count
1       1 -122.3170 47.61855       1   155
2       1 -122.3170 47.61911       2   155
3       1 -122.3170 47.61967       3   155
4       1 -122.3170 47.62023       4   155
5       1 -122.3169 47.62079       5   155
6       1 -122.3169 47.62135       6   155

我想做什么(如果你知道ggplot,我认为相对简单)是用count确定的线宽和由{确定的渐变来绘制这些流(线)。 {1}}。

这是我到目前为止的尝试:

seg_num

我最终得到了附图。我花了很长时间在代码的绘图部分中使用各种参数,但没有成功,所以如果有人能指出我正确的方向,我会非常感激。enter image description here

编辑:

#Create variables to store relevant data for simplicity of code
X <- interpolated_flows$long
Y <- interpolated_flows$lat
sgn <- interpolated_flows$seg_num
ct <- interpolated_flows$count

#Create a map from flow data and include the bounded box as a base
g <- ggplot(interpolated_flows,aes(x=X, y=Y),group=interpolated_flows$line_id,color=sgn)
map <- ggmap(seattle_map,base_layer = g)

map <- map + geom_path(size=as.numeric(ct)/100,alpha=0.4)+
  scale_alpha_continuous(range = c(0.03, 0.3))+coord_fixed(ratio=1.3)+
  scale_colour_gradient(high="red",low="blue")

png(filename='Seattle_flows_gradient.png')
print(map)
dev.off()

这是我现在的情节,看起来像这样。我只有两个问题 - 1)有没有人知道改善背景地图分辨率的方法?我尝试更改base <- ggplot(interpolated_flows,aes(x=X, y=Y)) map <- ggmap(seattle_map,base_layer = g) map <- map+geom_path(aes(color=seg_num,size=as.numeric(count)))+ scale_size_continuous(name="Journey Count",range=c(0.05,0.4))+ scale_color_gradient(name="Journey Path",high="white",low="blue",breaks=c(1,10), labels=c('Origin','Destination'))+ coord_fixed(ratio=1.3)+scale_x_continuous("", breaks=NULL)+ scale_y_continuous("", breaks=NULL) png(filename='Seattle_flows_gradient.png') print(map) dev.off() 函数中的zoom参数,但它似乎没有帮助。 2)我绘制的线条看起来非常“白色”。重。它并不像我看到梯度均匀分布。任何人都有任何想法,为什么会这样,以及如何解决?

enter image description here

1 个答案:

答案 0 :(得分:1)

看看这是否适合你。我创建了一个新的数据集,以便查看差异。一旦创建了data.frame,你可以将它作为你的第一个ggplot参数和引用列,就像Mako212所说的那样。

long<-seq(-122,-123,length.out = 6)
lat<-seq(47,48,length.out = 6)
seg_num<-seq(1,6,1)
count<-seq(155,165,length.out = 6)

interpolated_flows<-data.frame(long,lat,seg_num,count,stringsAsFactors = false)

base_plot<-ggplot(interpolated_flows,aes(x=long, y=lat))


base_plot+
  geom_path(aes(color=seg_num,size=as.numeric(count/100),alpha=lat))+
  #notice that size, color and alpha are into aethetic 
  scale_size_continuous(name="Count")+
  scale_alpha_continuous(name="Latitude",range = c(0.03, 0.3))+ #you won't need it if you don't want variable transparency
  #just put the desired value into the aethteic
  scale_color_gradient(name="Seg_num",high="red",low="blue")+
  coord_fixed(ratio=1.3)

Result

希望有所帮助

相关问题