Plot spatial coordinates with labels - r

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.

Related

how to make a dot plot based on each column and highlight the beginning and the end

I have a data like this
df<- structure(list(Number = 1:23, Value1 = c(0.054830335, 1.19531842,
3.27820329, 1.03530176, 5.77430976, 3.72944, -0.683513395, 0.029550239,
2.487922644, 0.533448117, 0.098825565, -1.089022938, 2.301631235,
-0.095666867, -1.359480317, -1.359480317, 1.089441628, 3.307589929,
4.67838434, 3.562761178, 2.630726653, 1.795107015, 2.616255192
), Value2 = c(-0.296874921, 1.491747294, 2.951219257, 1.258677675,
-8.68096591, 3.361029751, -1.824459195, -1.445827538, 1.889631269,
-15.47774216, 3.085461276, -1.078286963, 0.948056999, -2.109354753,
-1.36703068, -1.36703068, 1.074642842, 2.945589842, 3.757911793,
2.765225717, 2.44452491, 1.784451022, 1.158493893)), class = "data.frame", row.names = c(NA,
-23L))
I am trying to make a dot plot (one color for the Value1 vrsus number) and one with Value2 versus Number. Then show the first 5 values in bigger size and the bottom 5 in bigger size
I tried to plot it like this
df$Number <- factor(df$Number, levels = paste0("D", 1:23), ordered = TRUE)
ggplot(df, aes(x=Value1, y=Value2, color= Number)) +
geom_text()+
theme_classic()
I can plot one of them like this
ggplot(data = df, aes(x = Number, y = Value1))+
geom_point()
when it comes to have the second one on the same plot, kinda fuzzy.
I can put them together in this way
# wide to long format
plotDf <- gather(df, Group, Myvalue, -1)
# plot
ggplot(plotDf, aes(Number, Myvalue, col = Group)) +
geom_point()
I still don't know how to show the first 5 values in bigger size and last 5 values in bigger size
The first 5 and the last 5 I mean these ones
df
Number Value1 Value2
1 1 0.05483034 -0.2968749
2 2 1.19531842 1.4917473
3 3 3.27820329 2.9512193
4 4 1.03530176 1.2586777
5 5 5.77430976 -8.6809659
6 6 3.72944000 3.3610298
7 7 -0.68351339 -1.8244592
8 8 0.02955024 -1.4458275
9 9 2.48792264 1.8896313
10 10 0.53344812 -15.4777422
11 11 0.09882557 3.0854613
12 12 -1.08902294 -1.0782870
13 13 2.30163123 0.9480570
14 14 -0.09566687 -2.1093548
15 15 -1.35948032 -1.3670307
16 16 -1.35948032 -1.3670307
17 17 1.08944163 1.0746428
18 18 3.30758993 2.9455898
19 19 4.67838434 3.7579118
20 20 3.56276118 2.7652257
21 21 2.63072665 2.4445249
22 22 1.79510701 1.7844510
23 23 2.61625519 1.1584939
These are the first 5
1 1 0.05483034 -0.2968749
2 2 1.19531842 1.4917473
3 3 3.27820329 2.9512193
4 4 1.03530176 1.2586777
5 5 5.77430976 -8.6809659
and these are the last 5
19 19 4.67838434 3.7579118
20 20 3.56276118 2.7652257
21 21 2.63072665 2.4445249
22 22 1.79510701 1.7844510
23 23 2.61625519 1.1584939
Using the original data (without factor):
ggplot(df, aes(Number, Value1, size = (Number <= 5 | Number > 18))) +
geom_point() +
geom_point(aes(y=Value2)) +
scale_size_manual(name = NULL, values = c("TRUE" = 2, "FALSE" = 0.5)) +
scale_x_continuous(breaks = function(z) do.call(seq, as.list(round(z,0))))
Because using a logical condition to determine size=, the manual values assigned to it need to correspond to character versions of the various values observed, which are of course TRUE and FALSE logicals into "TRUE" and "FALSE". My choice of 2 and 0.5 is arbitrary.
Feel free to name the legend better with name="some name" if desired. If you want no legend (which makes sense), you can use
... +
scale_size_manual(guide = "none", values = c("TRUE" = 2, "FALSE" = 0.5))
instead.
Another alternative, in case you want to make distinct the dots by which Value# they are, is to melt the data into a long format before plotting.
ggplot(reshape2::melt(df, "Number"),
aes(Number, value, color = variable,
size = (Number <= 5 | Number >= 18))) +
geom_point() +
scale_size_manual(guide = "none", values = c("TRUE" = 2, "FALSE" = 0.5))
One can use tidyr::pivot_longer or data.table::melt with similar results, see Reshaping data.frame from wide to long format.

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)

ggplot2: arranging multiple boxplots as a time series

I would like to create a multivariate boxplot time series with ggplot2 and I need to have an x axis that positions the boxplots based on their associated dates.
I found two posts about this question: one is Time series plot with groups using ggplot2 but the x axis is not a scale_x_axis so graph is biased in my case. The other one is ggplot2 : multiple factors boxplot with scale_x_date axis in R but the person uses an interaction function which i don't use in my case.
Here is an example file and my code:
dtm <- read.table(text="date ruche mortes trmt
03.10.2013 1 8 P+
04.10.2013 1 7 P+
07.10.2013 1 34 P+
03.10.2013 7 16 P+
04.10.2013 7 68 P+
07.10.2013 7 170 P+
03.10.2013 2 7 P-
04.10.2013 2 7 P-
07.10.2013 2 21 P-
03.10.2013 5 8 P-
04.10.2013 5 27 P-
07.10.2013 5 24 P-
03.10.2013 3 15 T
04.10.2013 3 6 T
07.10.2013 3 13 T
03.10.2013 4 6 T
04.10.2013 4 18 T
07.10.2013 4 19 T ", h=T)
require(ggplot2)
require(visreg)
require(MASS)
require(reshape2)
library(scales)
dtm$asDate = as.Date(dtm[,1], "%d.%m.%Y")
## Plot 1: Nearly what I want but is biased by the x-axis format where date should not be a factor##
p2<-ggplot(data = dtm, aes(x = factor(asDate), y = mortes))
p2 + geom_boxplot(aes(fill = factor(dtm$trmt)))
## Plot 2: Doesn't show me what I need, ggplot apparently needs a factor as x##
p<-ggplot(data = dtm, aes(x = asDate, y = mortes))
p + geom_boxplot(aes( group = asDate, fill=trmt) ) `
Can anyone help me with this issue, please?
Is this what you want?
Code:
p <- ggplot(data = dtm, aes(x = asDate, y = mortes, group=interaction(date, trmt)))
p + geom_boxplot(aes(fill = factor(dtm$trmt)))
The key is to group by interaction(date, trmt) so that you get all of the boxes, and not cast asDate to a factor, so that ggplot treats it as a date. If you want to add anything more to the x axis, be sure to do it with + scale_x_date().

How to melt R data.frame and plot group by bar plot

I have following R data.frame:
group match unmatch unmatch_active match_active
1 A 10 4 0 0
2 B 116 20 0 3
3 c 160 27 1 4
4 D 79 17 0 3
5 E 309 84 4 14
6 F 643 244 10 23
...
My goal is to plot a group by bar plot (http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_(ggplot2)/ section-Graphs with more variables) as shown in the link.
I realize that before getting to that I need to get the data in to following format
group variable value
1 A match 10
2 B match 116
3 C match 160
4 D match 79
5 E match 309
6 F match 643
7 A unmatch 4
8 B unmatch 20
...
I used the melt function:
groups.df.melt <- melt(groups.df[,c('group','match','unmatch', 'unmatch_active', 'match_active')],id.vars = 1)
I don't think I am doing the melt correctly because after I execute above groups.df.melt has 1000+ lines which doesn't make sense to me.
I looked at how Draw histograms per row over multiple columns in R and tried to follow the same yet I don't get the graph I want.
In addition I get following error: When I try to do the plotting:
ggplot(groups.df.melt, aes(x='group', y=value)) + geom_bar(aes(fill = variable), position="dodge") + scale_y_log10()
Mapping a variable to y and also using stat="bin".
With stat="bin", it will attempt to set the y value to the count of cases in each group.
This can result in unexpected behavior and will not be allowed in a future version of ggplot2.
If you want y to represent counts of cases, use stat="bin" and don't map a variable to y.
If you want y to represent values in the data, use stat="identity".
See ?geom_bar for examples. (Deprecated; last used in version 0.9.2)
Error in pmin(y, 0) : object 'y' not found
Try:
mm <- melt(ddf, id='group')
ggplot(data = mm, aes(x = group, y = value, fill = variable)) +
geom_bar(stat = 'identity', position = 'dodge')
or
ggplot(data = mm, aes(x = group, y = value, fill = variable)) +
# `geom_col()` uses `stat_identity()`: it leaves the data as is.
geom_col(position = 'dodge')

Plotting coordinates of multiple points at google map in 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)

Resources