0
votes

I have a dataframe which I have constructed by interpolating a series of origin destination points (they relate to a cycle share scheme that used to run in Seattle).

I've called the dataframe 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

What I would like to do (and I think is relatively simple if you know ggplot) is to plot these flows (lines) with the width of a line determined by the count and the gradient determined by the seg_num.

This is my attempt so far:

#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()

And I end up with the image attached. I have spent a long time playing around with various parameters in the plotting part of the code but without success so would really appreciate if someone could point me in the right direction.enter image description here

Edit:

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()

This is the plot I have now got to which looks like this. I have only two questions - 1) does anyone know a way to improve the resolution of the background map? I tried changing the zoom parameter in the get_map function but it didn't seem to help. 2) The lines I have plotted seem very 'white' heavy. It doesn't look to me like the gradient is evenly distributed. Anyone have any ideas why this would be and how to fix?

enter image description here

1
You shouldn't remap things out of your original data frame, that's not in line with the ggplot methodology, and that's probably why your color mapping isn't working. When you pass interpolated_flows as your first ggplot argument, that means you can reference columns by their names without using interpolated_flows$column, you can just use column. So fix that and see if the gradient maps correctly. - Mako212
@Mako212 thanks, got you. Today is day 1 of ggplot :D - soundofsilence

1 Answers

1
votes

See if this suits you. I have create a new dataset so as to see diffencies. Once the data.frame is created you can use it as your first ggplot argument and reference columns by their names as Mako212 say.

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

Hope it helps