Plotting coordinates of multiple points at google map in R - r

I wanted to plot the coordinates on a Google Map in the presence of point id:
Sample of data:
coordinates id
1 (7.1735, 45.8688) 2
2 (7.17254, 45.8689) 3
3 (7.17164, 45.8692) 4
4 (7.18018, 45.8716) 5
5 (7.17807, 45.8701) 6
6 (7.17723, 45.8692) 7
7 (7.17524, 45.8681) 8
8 (7.18141, 45.8718) 9
9 (7.1793, 45.8702) 10
10 (7.17836, 45.8707) 11
11 (7.17519, 45.8697) 12
12 (7.17938, 45.8708) 13
13 (7.17551, 45.8693) 14
14 (7.17684, 45.8694) 15
15 (7.18099, 45.8726) 17
16 (7.18015, 45.8725) 18
17 (7.18122, 45.8736) 19
18 (7.17491, 45.8692) 20
19 (7.15497, 45.8706) 25
20 (7.1534, 45.8695) 28
21 (7.15265, 45.8699) 29
22 (7.15442, 45.87) 31
23 (7.1561, 45.8698) 32
24 (7.184, 45.896) GSBi_1
25 (7.36, 45.901) GSBi__1
26 (7.268, 45.961) GSBj__1
27 (7.276, 45.836) GSBj_1
28 (7.272, 45.899) GSB
29 (7.16667, 45.8667) GSB_r

Rather than request a map of 'Switzerland' from google, you should request a map of a specific location by specifying a longitude/latitude and desired zoom (and maybe scale). Then you won't have to use coord_map() and blur your image.
Here are the basics, you can play around with colors and sizes as in any ggplot:
library(ggplot2)
library(ggmap)
# copying text off screen
# since the OP did not use dput()
data<-read.table("clipboard")
# reformat
data=data[,-1]
names(data)=c("lon","lat","id")
data$lon <- as.numeric(gsub('[\\(\\)\\,]', '', data$lon))
data$lat <- as.numeric(gsub('[\\(\\)\\,]', '', data$lat))
head(data)
# lon lat id
# 1 7.17350 45.8688 2
# 2 7.17254 45.8689 3
# 3 7.17164 45.8692 4
# etc
# determine a reasonable center for map,
# this could fail in some places (near poles, 180th meridian)
# also google appears to shift things slightly
center = paste(min(data$lat)+(max(data$lat)-min(data$lat))/2,
min(data$lon)+(max(data$lon)-min(data$lon))/2, sep=" ")
# get map image from google
map <- get_map(location = center, zoom = 11, maptype = "terrain",
source = "google")
# start a ggplot. it won't plot til we type p
p <- ggmap(map)
# add text labels, these will overlap
p <- p + geom_text(data=data,aes(x = lon, y = lat,
label = id),
colour="white",size=4,hjust=0, vjust=0)+
theme(legend.position = "none")
# add points last so they are on top
p <- p + geom_point(data=data,aes(x=lon, y=lat),colour="white",size=2)
# display plot
p
This naturally is described in ?get_map and ?get_googlemap.

One of the problems with plotting of your points is that if you use zoom=10 in function get_map() then your points are outside the map and they want be plotted, so I used zoom=5 instead.
library(ggmap)
map <- get_map(location = 'Switzerland', zoom = 5,
maptype = "terrain", source = "google")
For the plotting of map I used function ggmap(). To add points geom_point() can be used. For this purpose your sample data were saved as data frame df with columns x, y and id. To zoom closer to points coord_map() can be used.
p <- ggmap(map)
p <- p +geom_point(data=df,aes(x=x,y=y))+
coord_map(xlim=c(7,8),ylim=c(45.5,46))
print(p)
If you need to add labels to each point then add this line to map p
annotate("text",x=df$x,y=df$y,label=df$id)

Related

line graph with multiple variables on y axis stepwise

I need some help. Here is my data which i want to plot. I want to keep $path.ID on y axis and numerics of all other columns added stepwise. this is a subset of very large dataset so i want to pathID labels attached to each line. and also the values of the other columns with each point if possible.
head(table)
Path.ID sc st rc rt
<chr> <dbl> <dbl> <dbl> <dbl>
1 map00230 1 12 5 52
2 map00940 1 20 10 43
3 map01130 NA 15 8 34
4 map00983 NA 14 5 28
5 map00730 NA 5 3 26
6 map00982 NA 16 2 24
somewhat like this
Thank you
Here is the pseudo code.
library(tidyr)
library(dplyr)
library(ggplot2)
# convert your table into a long format - sorry I am more used to this type of data
table_long <- table %>% gather(x_axis, value, sc:rt)
# Plot with ggplot2
ggplot() +
# draw line
geom_line(data=table_long, aes(x=x_axis, y=value, group=Path.ID, color=Path.ID)) +
# draw label at the last x_axis in this case is **rt**
geom_label(data=table_long %>% filter(x_axis=="rt"),
aes(x=x_axis, y=value, label=Path.ID, fill=Path.ID),
color="#FFFFFF")
Note that with this code if a Path.ID doesn't have the rt value then it will not have any label
p<-ggplot() +
# draw line
geom_line(data=table_long, aes(x=x_axis, y=value, group=Path.ID, color=Path.ID)) +
geom_text(data=table_long %>% filter(x_axis=="rt"),
aes(x=x_axis, y=value, label=Path.ID),
color= "#050505", size = 3, check_overlap = TRUE)
p +labs(title= "title",x = "x-lable", y="y-label")
I had to use geom_text as i had large dataset and it gave me somewhat more clear graph
thank you #sinh it it helped a lot.

Stacked Bar Chart in R using ggplot2

I am trying to create a Stacked Bar Graph using ggplot2. Here is how my data looks like
Date Full.Page.Ads Total.Pages
11/10/2015 3 24
12/10/2015 10 24
13/10/2015 15 24
This is the code which I am trying to use to visualize the data
library("ggplot2")
library("reshape2")
ns <- read.csv("NS.csv")
ggplot(data=ns, aes(x = Date, y = Total.Pages, fill=Full.Page.Ads)) + geom_bar(stat = "identity")
But for some reason rather than making a proper stacked bar chart, it keeps making something like this http://imgur.com/rqxEzjF.jpg. Rather than this I would like to show the main bars as Total.Pages and inside those bars, Full.Page.Ads bar in different colours.
I am not sure what I am doing wrong out here.
This is a sample of what I am aspiring to make http://blog.visual.ly/wp-content/uploads/2012/08/StackedPercent.png
You have to melt your data before plotting. You also need to compute the difference between Total.Pages and Full.Page.Ads if you want your graph to represent the proportion of Full.Page.Ads with respect to the total number of pages. Something like:
ns <- data.frame(Date = c("11/10/2015", "12/10/2015", "13/10/2015"),
Full.Page.Ads = c(3, 10, 15),
Total.Pages = c(24, 24, 24))
# Date Full.Page.Ads Total.Pages
# 1 11/10/2015 3 24
# 2 12/10/2015 10 24
# 3 13/10/2015 15 24
ns$not.Full.Page.Ads <- ns$Total.Pages - ns$Full.Page.Ads
ns$Total.Pages <- NULL
ns2 <-melt(ns)
# Date variable value
#1 11/10/2015 Full.Page.Ads 3
#2 12/10/2015 Full.Page.Ads 10
#3 13/10/2015 Full.Page.Ads 15
#4 11/10/2015 not.Full.Page.Ads 21
#5 12/10/2015 not.Full.Page.Ads 14
#6 13/10/2015 not.Full.Page.Ads 9
ggplot(data = ns2, aes(x = Date, y = value, fill = variable)) + geom_bar(stat = "identity")

Modifying y-axis with ggplot2

I'm trying to plot the number of observations for each instance of a word, both of which are stored in a data frame.
I can generate the plot with ggplot2, but the y-axis displays "1+e05", "2+e05",...,etc...instead of numerical values.
How can I modify this code so that the y-axis displays numbers instead?
Here is my code:
> w
p.word p.freq
1 the 294571
2 and 158624
3 you 84152
4 for 77117
5 that 71672
6 with 47987
7 this 42768
8 was 41088
9 have 39835
10 are 36458
11 but 33899
12 not 30370
13 all 27079
14 your 26923
15 just 25507
16 from 24497
17 out 22578
18 like 22501
19 what 22150
20 will 21530
21 they 21435
22 about 21184
23 one 20877
24 its 20109
ggplot(w, aes(x = p.word, y = p.freq))+ geom_bar(stat = "identity")
Here is the plot that is generated:
"1e+05" etc are numerical values (scientific notation).
If you want the long notation (e.g. "100,000") use library(scales) and the comma formatter:
library(scales)
ggplot(w, aes(x = p.word, y = p.freq))+ geom_bar(stat = "identity") +
scale_y_continuous(labels=comma)

ggmap and scale_colour_brewer

I am trying to plot a frequency over a map obtained with ggmap in R. The idea is that I would have a plot of the frequency on each coordinates set. The frequency ("freq") would be mapped to six and a color scale. The data looks like this:
V7 V6 freq
1 42.1752 -71.2893 1
2 42.1754 -71.2893 1
3 42.1755 -71.2901 2
4 42.1755 -71.2893 1
5 42.1756 -71.2910 1
6 42.1756 -71.2907 1
7 42.1756 -71.2906 1
8 42.1756 -71.2905 1
9 42.1756 -71.2901 1
10 42.1756 -71.2899 2
11 42.1756 -71.2897 2
12 42.1756 -71.2894 2
13 42.1757 -71.2915 1
14 42.1757 -71.2910 1
Here is the code I am using:
ggmap(newmap2) +
geom_point(aes(x = coordfreq$V7, y = coordfreq$V6),
data = coordfreq, alpha = 1/sqrt(coordfreq$freq),
colour = coordfreq$freq, size = sqrt(coordfreq$freq)) +
scale_colour_brewer(palette = "Set1")
I only get the color mapped to "freq", but I cannot get the scale_colour_brewer to work. I have tried several arguments to scale_color_brewer to no available.
Your code created a map without data points. Here is possibly what you are after. A few things. One is that you do not have to type x = coordfreq$V7. You can just type x = V7. The same applies to other similar cases in your code. Another is that colour should be in aes() in your case. Another thing is that freq is numeric. You need it as either factor or character when you assign colours to your graphic. The other is that freq is a function. You want to void such a name. Hope this will help you.
library(ggmap)
library(ggplot2)
# This get_map code was suggested by an SO user. Sadly, the edit was rejected.
# Credit to him/her (MichaelVE).
newmap2 <- get_map(location = c(lon = -71.2893, lat = 42.1752),
zoom = 17, maptype = 'terrain')
ggmap(newmap2) +
geom_point(data = mydf2, aes(x = V6, y = V7, colour = factor(frequency), size = sqrt(frequency))) +
scale_colour_brewer(palette ="Set1")

Plot spatial coordinates with labels

I am quite new in R. I have number of coordinates and I want to plot them in a proper way in R which also presents labels. Moreover, axises should present the lat and long. I have tries ggplot but I cannot fit the data to the code.
id lon lat
1 2 7.173500 45.86880
2 3 7.172540 45.86887
3 4 7.171636 45.86924
4 5 7.180180 45.87158
5 6 7.178070 45.87014
6 7 7.177229 45.86923
7 8 7.175240 45.86808
8 9 7.181409 45.87177
9 10 7.179299 45.87020
10 11 7.178359 45.87070
11 12 7.175189 45.86974
12 13 7.179379 45.87081
13 14 7.175509 45.86932
14 15 7.176839 45.86939
15 17 7.180990 45.87262
16 18 7.180150 45.87248
17 19 7.181220 45.87355
18 20 7.174910 45.86922
19 25 7.154970 45.87058
20 28 7.153399 45.86954
21 29 7.152649 45.86992
22 31 7.154419 45.87004
23 32 7.156099 45.86983
To do this use the geom_text geometry:
ggplot(aes(x = lon, y = lat), data = df) + geom_text(aes(label = id))
This plots the text in the id column on the locations specfied by the columns lon and lat. The data is stored in the data.frame df.
or use:
ggplot(aes(x = lon, y = lat), data = df) + geom_text(aes(label = id)) +
geom_point()
if you want to add both a point and a label. Use the hjust and vjust parameters of geom_text to change the orientation of the label relative to the point. In addition, give each point a color according to the column var by using the color parameter in the geom_point aesthetics:
ggplot(aes(x = lon, y = lat), data = df) + geom_text(aes(label = id)) +
geom_point(aes(color = var))
Do note that ggplot2 cannot deal with the Spatial classes provided by the sp package. Use as.data.frame to convert point (SpatialPoints) and gridsets (SpatialPixels/SpatialGrid) to data.frame's. In addition, use fortify to convert polygon datasets (SpatialPolygons) to data.frame.

Resources