change NA-color in zipcode map - r

I created a map on German zip code with my data. My data did not contain every zip code, so I do have missing values. Missing values are drawn black in the map; I want them to be white. How can I change it?
ger_plz <- readOGR(dsn = ".", layer = "plz-2stellig")
gpclibPermit()
#convert the raw data to a data.frame as ggplot works on data.frames
ger_plz#data$id <- rownames(ger_plz#data)
ger_plz.point <- fortify(ger_plz, region="id")
ger_plz.df <- inner_join(ger_plz.point,ger_plz#data, by="id")
head(ger_plz.df)
ggplot(ger_plz.df, aes(long, lat, group=group )) + geom_polygon()
#data file
Ers <- table(data.plz$plz)
df<- as.data.frame(Ers)
# variable name 'region' is needed for choroplethr
ger_plz.df$region <- ger_plz.df$plz
head(ger_plz.df)
#subclass choroplethr to make a class for your my need
GERPLZChoropleth <- R6Class("GERPLZChoropleth",
inherit = choroplethr:::Choropleth,
public = list(
initialize = function(user.df) {
super$initialize(ger_plz.df, user.df)
}
)
)
#choropleth needs these two columnames - 'region' and 'value'
colnames(df) = c("region", "value")
#instantiate new class with data
c <- GERPLZChoropleth$new(df)
#plot the data
c$ggplot_polygon = geom_polygon(aes(fill = value), color = NA)
c$title = "Erstsemester Landau 2017"
c$legend= "Heimatort"
c$set_num_colors(9)
c$render()

I have kind of a solution for you, but it's not the best.
You need to plot the map, and after that you can change the colour. Choose another color palette to get the NA Data white. I tryed it with palette = "OrRd" and it works fine.
c$ggplot_polygon = geom_polygon(aes(fill = value), color = NA)
c$title = "Erstsemester Landau 2017"
c$legend= "Heimatort"
c$set_num_colors(9)
choro <- c$render()
choro
choro+scale_fill_brewer(palette = "OrRd")
You can then use the palette parameter also with other colors:
Diverging:
BrBG, PiYG, PRGn, PuOr, RdBu, RdGy, RdYlBu, RdYlGn, Spectral
Qualitative Accent:
Dark2, Paired, Pastel1, Pastel2, Set1, Set2, Set3
Sequential:
Blues, BuGn, BuPu, GnBu, Greens, Greys, Oranges, OrRd, PuBu, PuBuGn, PuRd, Purples, RdPu, Reds, YlGn, YlGnBu, YlOrBr, YlOrRd
(Source: https://www.rdocumentation.org/packages/ggplot2/versions/3.3.2/topics/scale_colour_brewer)

Since R tells you what zip codes are missing, I did the following to set my NA values to 0 thus making them white (even when using the blue palette).
missingZips <- data.frame("region" = c(LIST ZIP CODES--i.e. 12345, 34556))
missingZips$value=0
merged_df <- rbind(df, missingZips)

Related

Plotting a raster layer by setting a specific color to a non-central value

I have been trying to plot a raster layer by setting the value 1 to the color white. I looked at many examples but still couldn't construct the colors as I wish. I want contrasting colors for the values below and above 1. I would like them start with a light tone and get darker as they go further from 1. I also posted the figure that I managed to create so far. It looks a bit weird since it is based on made up data. I can't share the real data unfortunately. The best would be to create this using viridis colors, but I don't know how doable it is.
I wonder if anyone has any suggestions?
I got the idea of breakpoints and colors from the following post :https://gis.stackexchange.com/questions/17339/raster-legend-in-r-how-to-colour-specific-values .
I got the shapefile from here : https://gadm.org/download_country.html .
Here is my example:
library(rgdal)
library(raster)
library(sp)
library(sf)
set.seed(123)
proj = "+units=km +proj=utm +zone=37 +ellps=clrk80 +towgs84=-160,-6,-302,0,0,0,0 +no_defs"
map0 = readOGR(dsn = "dataFiles/gadm40_NGA_shp", #https://gadm.org/download_country.html (Nigeria/shapefile)
layer = "gadm40_NGA_0")
map0_trnsfrmd = spTransform(map0,proj)
predRaster <- raster(ncol=400, nrow=400, xmn=-3805.7869, xmx=-2222.120, ymn=562.5405, ymx=1828.165)
res(predRaster) = 5
projection(predRaster) = "+units=km +proj=utm +zone=37 +ellps=clrk80 +towgs84=-160,-6,-302,0,0,0,0 +no_defs"
idx = 1:80201 # index for the cell numbers
val = c(rnorm(50000), rep(1,10201),runif(20000,min=0, max=3))
r = setValues(predRaster, values = val, index=idx) # assign values to the cells
r = raster::mask(crop(r, extent(map0_trnsfrmd)), map0_trnsfrmd, snap = 'out')
below=val[val<1]
above=val[val>1]
min(below)
[1] -4.289319
max(above)
[1] 4.438207
max(below)
[1] 0.9998253
min(above)
[1] 1.000105
breakpoints = c(-4.289319, 0.9998253, 1.000105, 4.438207)
colors = c("red","white","blue")
par(mgp=c(4,1,0), mar=c(5,7,3,1)+0.1)
plot(r,las=1, asp = 1, xlab="Easting", ylab="Northing", axis.args = list(cex.axis=1),
legend.shrink =1,legend.width=2, cex.lab=2, cex.axis=1.5, legend.args = list("title", cex =1),breaks=breakpoints,col=colors)
exampleplot
Please look below on the colors definition. I have removed the shape for clarity.
library(rgdal)
library(raster)
library(sp)
library(sf)
set.seed(123)
predRaster <- raster(ncol=400, nrow=400, xmn=-3805.7869, xmx=-2222.120, ymn=562.5405, ymx=1828.165)
res(predRaster) = 5
projection(predRaster) = "+units=km +proj=utm +zone=37 +ellps=clrk80 +towgs84=-160,-6,-302,0,0,0,0 +no_defs"
idx = 1:80201 # index for the cell numbers
val = c(rnorm(50000), rep(1,10201),runif(20000,min=0, max=3))
r = setValues(predRaster, values = val, index=idx) # assign values to the cells
below=val[val<1]
above=val[val>1]
colors = c(colorRampPalette(c(rgb(1,0,0,1), rgb(1,0,0,0.1)), alpha = TRUE)(19),"white", colorRampPalette(c(rgb(0,0,1,0.1), rgb(0,0,1,1)), alpha = TRUE)(19))
plot(r, col=colors, xlab="Easting", ylab="Northing", axis.args = list(cex.axis=1),
legend.shrink =1,legend.width=2, cex.axis=1.5, legend.args = list("title", cex =1))
You can increase the number of colors on both sides. Please check the help for colorRampPalette() function.
Created on 2022-08-31 by the reprex package (v2.0.1)

geographical/geospatial map with maps() - R

I have been through a lot of questions and documentation, and since you need to bill to use ggmaps() (because of google cloud services) I started looking for an alternative. I found maps(), and I'm trying to adapt this solution:
data %>%
rename(x = longitud, y = latitud) %>%
ggplot() +
geom_polygon(aes(x = long, y = lat), data = map_data("world")) +
geom_point(aes(x = x, y = y))
However, I'm getting into a few problems:
If you plot the code above, you will get the right points for the plot (Chile), but the world map is wrongly printed (see the picture above).
I don't need a grey either a colorful map. I just need to plot the country Chile with kind-of-normal formatting (for example, google maps satellite). The coordinates are flows of lakes/mountains and I want to see if I can cluster them by some-visual sector.
I only need a map from Chile, but as I didn't find one I'm using this world map. Is there a way to cut it without losing the connections with the map coordinates?
This is the data:
data <- structure(list(latitud = c(-30.6833000183105, -41.4000015258789,
-43.8189010620117, -34.2731018066406, -47.0666999816895, -40.3166999816895,
-43.4491996765137, -35.7543983459473, -47.1413993835449, -36.6260986328125,
-54.0410995483398, -37.2118988037109, -33.3086013793945, -37.2792015075684,
-35.4524993896484, -36.5856018066406, -18.5832996368408, -18.2325000762939,
-36.4668998718262, -44.75, -44.6591987609863, -44.5936012268066,
-28.4647006988525, -28.6996994018555, -28.5118999481201, -28.6718997955322,
-28.7306003570557, -30.5902996063232, -30.6667003631592, -35.1730995178223,
-48.1591987609863, -48.377498626709, -45.4000015258789, -45.7832984924316,
-29.94580078125, -38.8652992248535, -30.4386005401611, -31.6646995544434,
-51.2000007629395, -51.3328018188477, -51.25, -45.5666999816895,
-45.551700592041, -45.8372001647949, -39.0144004821777, -28.9414005279541,
-28.7502994537354, -38.6081008911133, -34.9844017028809, -32.8403015136719,
-29.9953002929688, -18.3999996185303, -35.9000015258789, -35.6169013977051,
-35.9085998535156, -35.8166999816895, -33.7346992492676, -45.38330078125,
-35.4068984985352, -32.7571983337402, -32.8502998352051, -33.5938987731934,
-36.8386001586914, -33.4961013793945, -20.1119003295898, -27.8043994903564,
-37.7332992553711, -30.9986000061035, -30.8006000518799, -21.9368991851807,
-22.3652992248535, -22.273099899292, -22.0277996063232, -21.9755992889404,
-22.289400100708, -22.2791996002197, -38.4303016662598, -38.6866989135742,
-45.4057998657227, -38.7799987792969, -37.5503005981445, -37.6018981933594,
-37.8997001647949, -38.0368995666504, -37.9897003173828, -37.7047004699707,
-37.7963981628418, -37.7092018127441, -31.5835990905762, -27.3635997772217,
-27.3194007873535, -29.8931007385254, -30.9242000579834, -21.4246997833252,
-36.5703010559082, -38.2008018493652, -38.0661010742188, -38.4333000183105,
-31.7422008514404, -31.6881008148193, -31.8117008209229, -31.7714004516602,
-27.86669921875, -27.5160999298096, -27.9747009277344, -30.7047004699707,
-36.8499984741211, -36.6500015258789, -36.86669921875, -35.3736000061035,
-40.5167007446289, -33.4782981872559, -33.198299407959, -36.0499992370605,
-35.9667015075684, -36.2332992553711, -34.4921989440918, -34.6581001281738,
-32.8166999816895, -47.3499984741211, -47.5, -29.9811000823975,
-32.4413986206055, -22.3922004699707, -22.3430995941162, -21.7124996185303,
-22.4582996368408, -22.4419002532959, -22.4468994140625, -22.5060997009277,
-33.7219009399414, -33.6613998413086, -35.5574989318848), longitud = c(-71.0500030517578,
-73.2166976928711, -72.38330078125, -71.371696472168, -72.8000030517578,
-72.9666976928711, -72.1074981689453, -71.0864028930664, -72.7257995605469,
-72.4891967773438, -68.7975006103516, -72.3242034912109, -70.3572006225586,
-71.9847030639648, -71.7332992553711, -71.5255966186523, -69.0466995239258,
-69.331901550293, -72.6911010742188, -72.7166976928711, -71.8082962036133,
-71.5477981567383, -71.1782989501953, -70.5500030517578, -71.0064010620117,
-70.6464004516602, -70.5066986083984, -71.1714019775391, -71.5333023071289,
-71.0911026000977, -73.0888977050781, -72.9589004516602, -72.5999984741211,
-72.61669921875, -70.5327987670898, -71.7335968017578, -71.002197265625,
-71.2546997070312, -72.9332962036133, -73.1091995239258, -72.5167007446289,
-72.0832977294922, -72.0680999755859, -71.7769012451172, -73.0828018188477,
-70.2481002807617, -70.4828033447266, -72.8478012084961, -72.0100021362305,
-71.0255966186523, -70.5867004394531, -70.3000030517578, -71.5167007446289,
-71.7677993774414, -71.2981033325195, -71.8332977294922, -70.3007965087891,
-72.4666976928711, -72.2082977294922, -70.736701965332, -70.5093994140625,
-70.3792037963867, -73.061897277832, -70.8167037963867, -68.8407974243164,
-70.1268997192383, -72.61669921875, -71.0899963378906, -70.9697036743164,
-68.5330963134766, -68.6418991088867, -68.1438980102539, -68.6207962036133,
-68.6074981689453, -68.3447036743164, -68.2427978515625, -72.0105972290039,
-72.502799987793, -72.6231002807617, -72.9468994140625, -72.5903015136719,
-72.2782974243164, -71.6239013671875, -71.4781036376953, -71.5199966430664,
-71.7683029174805, -71.6988983154297, -71.823600769043, -71.4606018066406,
-70.3392028808594, -70.8380966186523, -71.2514038085938, -70.7731018066406,
-70.053596496582, -71.5547027587891, -71.2988967895508, -71.3497009277344,
-71.2332992553711, -71.1492004394531, -71.2658004760742, -70.9302978515625,
-71.0639038085938, -70.0667037963867, -70.2647018432617, -69.997802734375,
-70.9244003295898, -72.38330078125, -72.4499969482422, -72.3332977294922,
-71.8292007446289, -73.2833023071289, -70.7172012329102, -70.8955993652344,
-72.0832977294922, -72.0167007446289, -72, -71.3731002807617,
-71.3019027709961, -71, -72.8499984741211, -72.9749984741211,
-70.8981018066406, -71.3139038085938, -69.5299987792969, -69.5650024414062,
-69.5167007446289, -68.7363967895508, -68.8886032104492, -68.8775024414062,
-68.988899230957, -71.5550003051758, -71.3371963500977, -71.7067031860352
)), row.names = c(1L, 136L, 262L, 395L, 507L, 605L, 701L, 789L,
868L, 996L, 1094L, 1124L, 1172L, 1218L, 61387L, 61546L, 75009L,
87052L, 99246L, 110237L, 115091L, 125346L, 135758L, 135819L,
144524L, 154009L, 172251L, 185024L, 192338L, 210797L, 228781L,
228893L, 238299L, 244626L, 253673L, 274263L, 285367L, 304757L,
316768L, 328069L, 336044L, 346167L, 351691L, 363302L, 375494L,
385229L, 402720L, 422016L, 440373L, 451547L, 462674L, 483188L,
491968L, 496483L, 511332L, 530494L, 546443L, 564800L, 575215L,
586462L, 602135L, 622841L, 642834L, 657640L, 677273L, 688216L,
706550L, 724524L, 731829L, 748442L, 748489L, 754030L, 763570L,
776729L, 785860L, 799355L, 812606L, 832675L, 853030L, 860670L,
878448L, 889066L, 889167L, 889273L, 889372L, 889466L, 889499L,
889524L, 913996L, 929594L, 935459L, 953842L, 963352L, 983829L,
991810L, 1005230L, 1005341L, 1011503L, 1022492L, 1029507L, 1047978L,
1063655L, 1073799L, 1073936L, 1086040L, 1106251L, 1126146L, 1134776L,
1154269L, 1170495L, 1181431L, 1192018L, 1197439L, 1212431L, 1231028L,
1247598L, 1264197L, 1264302L, 1271900L, 1279499L, 1279618L, 1290282L,
1309415L, 1320521L, 1320606L, 1320753L, 1320827L, 1337638L, 1344817L,
1355030L, 1368899L, 1381979L, 1393175L), class = "data.frame")
The following code uses the simple features (sf) library to show a map of Chile overlaid with the provided datapoints. The bounding box is set in the parameters to st_crop and can be adjusted as needed without distorting the map. The code uses the Admin 0 - Countries shape file, which is in the public domain and free to use.
library(sf)
library(ggplot2)
library(dplyr);
library(magrittr);
# download world shapefile from
# https://www.naturalearthdata.com/downloads/
# 50m-cultural-vectors/50m-admin-0-countries-2/
# and extract zip file
world <- st_read(
# change below line to path of extracted shape file
'c:/path/to/ne_50m_admin_0_countries.shp'
);
world %<>% mutate(active = NAME_EN == 'Chile'); # used to highlight Chile
# convert the dataframe to a sf geometry object
dsf <- data %>%
rowwise %>%
mutate(geometry = list(st_point(c(longitud, latitud)))) %>%
st_as_sf(crs=st_crs(world));
# plot the map
world %>% st_crop(xmin=-90, xmax=-30, ymin=-60, ymax=-10) %>%
ggplot() +
geom_sf(aes(fill=active), show.legend=F) + # world map with Chile highlighted
geom_sf(data=dsf, color='#000000') + # point overlay
scale_fill_manual(values=c('#aaaa66', '#ffffcc')) + # country colors
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
theme_void() + # remove axis labels and gridlines
theme(panel.background=element_rect(fill='lightblue'))
The output is shown below. Note that the map does not distort regions that are cropped.
Additional explanation
The sf package provides support for simple feature (sf) geometries. Simple features provide tools for working with geometries such as polygons and points. There is a cheat sheet here that provides a good overview.
Plotting the base map
As of release 3.0, ggplot2 provides native support for visualizing simple feature geometries. This allows for us to write:
world <- st_read(
# change below line to path of extracted shape file
'c:/path/to/ne_50m_admin_0_countries.shp'
);
ggplot(world) + geom_sf()
Simple feature objects are generally stored in data frames that include a column describing the geometry. This allows us to show a map of Chile like so:
ggplot(world %>% filter(NAME_EN == 'Chile')) + geom_sf()
Or a map with Chile highlighted:
# create new geometry of world map
# cropped to (10°S, 60°S) and (90°W, 30°W)
chileregion <- world %>% st_crop(
xmin=-90,
xmax=-30,
ymin=-60,
ymax=-10)
# show region with Chile highlighted
ggplot(chileregion %>%
mutate(is.chile = factor(NAME_EN == 'Chile'))) +
geom_sf(aes(fill=is.chile), show.legend = F) +
scale_fill_manual(values=c('gray', 'red'))
Plotting the points
We are given a data frame with two columns, latitude and longitude.
To convert to a simple feature, we first use st_point to create a new point with the given coordinate values:
dsf <- data %>% #begin with data
rowwise %>% # dplyr::rowwise applies mutation to each row individually
# create a geometry column that provides the point as a geometry
mutate(geometry = list(st_point(c(longitud, latitud))))
At this point, dsf is simply a data frame with a geometry column; it is not yet a simple feature object. We can use the function st_as_sf to create a sf object from the data frame. In doing so, we will also need to provide a coordinate reference system (CRS) to allow ggplot to project the coordinates provided onto the map rendering. Here we can provide ESPG 4326 as the CRS, which maps the x and y coordinates directly to lat/long:
# call st_as_sf to convert data frame to a simple geometry object.
dsf <- st_as_sf(crs=4326);
Since dsf is now a simple geometry, you can plot as such:
> ggplot(dsf) + geom_sf()
(Note that you could also simply overlay the points on the base map with geom_point(data=data, aes(x=longitud, y=latitud)) without first converting to an sf object. This will work here because the CRS for the base map is also ESPG 4326, which maps x and y directly to longitude and latitude, respectively. Using geom_point, however, will not work in the general case when a coordinate transformation is applied to the geometry.)
Overlaying
With both points now defined as geometries, you can simply overlay:
ggplot() +
geom_sf(data=chileregion) +
geom_sf(data=dsf)
The final plot in the original answer adds some additional visual aesthetics (e.g., blue background) to produce the final map output.
This might help and you can play around with it yourself:
world_map <- map_data("world")
Wmap <- data %>%
rename(x =longitud , y = latitud) %>%
ggplot() +
geom_polygon(aes(x = long, y = lat, group = group), data = world_map, fill = "grey21", color = "grey21") +
geom_point(aes(x = x, y = y, color = 'red')) +
scale_color_identity() +
coord_fixed() +
xlab("") +
ylab("")
Wmap
Chile_map <- map_data("world", region="Chile")
Cmap <- data %>%
rename(x =longitud , y = latitud) %>%
ggplot() +
geom_polygon(aes(x = long, y = lat, group = group), data = Chile_map, fill = "grey21", color = "grey21") +
geom_point(aes(x = x, y = y, color = 'red')) +
scale_color_identity() +
coord_fixed() +
xlab("") +
ylab("")
Cmap
dev.new()
windows.options(width=10, height=6)
vp_inset <- grid::viewport(width = 0.55, height = 0.45, x = -0.1, y = 0.60, just = c("left", "top"))
print(Wmap)
print(Cmap, vp = vp_inset)
NOTE: The group aesthetic determines which cases are connected together into a polygon.

Error with plot function object not found

When I try and generate a plot with the below I receive an Object not found for the fertility rate.
I've used str and names to make sure I've got the name correct.
there is a file ("https://sds-platform-private.s3-us-east-2.amazonaws.com/uploads/P2-Section5-Homework-Data.csv) that I have to use also.
From what I can see everything should run ok. But I've obviously done something wrong as I get:
Error in plot(data = New_Data1960, x = Fertility.Rate, y =
Life_Expectancy_At_Birth_1960, : object 'Fertility.Rate' not found
Can someone explain why I'm getting this error and how I can fix it?
#Execute below code to generate three new vectors
Country_Code <- c("ABW","AFG","AGO","ALB","ARE","ARG","ARM","ATG","AUS","AUT","AZE","BDI","BEL","BEN","BFA","BGD","BGR","BHR","BHS","BIH","BLR","BLZ","BOL","BRA","BRB","BRN","BTN","BWA","CAF","CAN","CHE","CHL","CHN","CIV","CMR","COG","COL","COM","CPV","CRI","CUB","CYP","CZE","DEU","DJI","DNK","DOM","DZA","ECU","EGY","ERI","ESP","EST","ETH","FIN","FJI","FRA","FSM","GAB","GBR","GEO","GHA","GIN","GMB","GNB","GNQ","GRC","GRD","GTM","GUM","GUY","HKG","HND","HRV","HTI","HUN","IDN","IND","IRL","IRN","IRQ","ISL","ITA","JAM","JOR","JPN","KAZ","KEN","KGZ","KHM","KIR","KOR","KWT","LAO","LBN","LBR","LBY","LCA","LKA","LSO","LTU","LUX","LVA","MAC","MAR","MDA","MDG","MDV","MEX","MKD","MLI","MLT","MMR","MNE","MNG","MOZ","MRT","MUS","MWI","MYS","NAM","NCL","NER","NGA","NIC","NLD","NOR","NPL","NZL","OMN","PAK","PAN","PER","PHL","PNG","POL","PRI","PRT","PRY","PYF","QAT","ROU","RUS","RWA","SAU","SDN","SEN","SGP","SLB","SLE","SLV","SOM","SSD","STP","SUR","SVK","SVN","SWE","SWZ","SYR","TCD","TGO","THA","TJK","TKM","TLS","TON","TTO","TUN","TUR","TZA","UGA","UKR","URY","USA","UZB","VCT","VEN","VIR","VNM","VUT","WSM","YEM","ZAF","COD","ZMB","ZWE")
Life_Expectancy_At_Birth_1960 <- c(65.5693658536586,32.328512195122,32.9848292682927,62.2543658536585,52.2432195121951,65.2155365853659,65.8634634146342,61.7827317073171,70.8170731707317,68.5856097560976,60.836243902439,41.2360487804878,69.7019512195122,37.2782682926829,34.4779024390244,45.8293170731707,69.2475609756098,52.0893658536585,62.7290487804878,60.2762195121951,67.7080975609756,59.9613658536585,42.1183170731707,54.2054634146342,60.7380487804878,62.5003658536585,32.3593658536585,50.5477317073171,36.4826341463415,71.1331707317073,71.3134146341463,57.4582926829268,43.4658048780488,36.8724146341463,41.523756097561,48.5816341463415,56.716756097561,41.4424390243903,48.8564146341463,60.5761951219512,63.9046585365854,69.5939268292683,70.3487804878049,69.3129512195122,44.0212682926829,72.1765853658537,51.8452682926829,46.1351219512195,53.215,48.0137073170732,37.3629024390244,69.1092682926829,67.9059756097561,38.4057073170732,68.819756097561,55.9584878048781,69.8682926829268,57.5865853658537,39.5701219512195,71.1268292682927,63.4318536585366,45.8314634146342,34.8863902439024,32.0422195121951,37.8404390243902,36.7330487804878,68.1639024390244,59.8159268292683,45.5316341463415,61.2263414634146,60.2787317073171,66.9997073170732,46.2883170731707,64.6086585365854,42.1000975609756,68.0031707317073,48.6403170731707,41.1719512195122,69.691756097561,44.945512195122,48.0306829268293,73.4286585365854,69.1239024390244,64.1918292682927,52.6852682926829,67.6660975609756,58.3675853658537,46.3624146341463,56.1280731707317,41.2320243902439,49.2159756097561,53.0013170731707,60.3479512195122,43.2044634146342,63.2801219512195,34.7831707317073,42.6411951219512,57.303756097561,59.7471463414634,46.5107073170732,69.8473170731707,68.4463902439024,69.7868292682927,64.6609268292683,48.4466341463415,61.8127804878049,39.9746829268293,37.2686341463415,57.0656341463415,60.6228048780488,28.2116097560976,67.6017804878049,42.7363902439024,63.7056097560976,48.3688048780488,35.0037073170732,43.4830975609756,58.7452195121951,37.7736341463415,59.4753414634146,46.8803902439024,58.6390243902439,35.5150487804878,37.1829512195122,46.9988292682927,73.3926829268293,73.549756097561,35.1708292682927,71.2365853658537,42.6670731707317,45.2904634146342,60.8817073170732,47.6915853658537,57.8119268292683,38.462243902439,67.6804878048781,68.7196097560976,62.8089268292683,63.7937073170732,56.3570487804878,61.2060731707317,65.6424390243903,66.0552926829268,42.2492926829268,45.6662682926829,48.1876341463415,38.206,65.6598292682927,49.3817073170732,30.3315365853659,49.9479268292683,36.9658780487805,31.6767073170732,50.4513658536585,59.6801219512195,69.9759268292683,68.9780487804878,73.0056097560976,44.2337804878049,52.768243902439,38.0161219512195,40.2728292682927,54.6993170731707,56.1535365853659,54.4586829268293,33.7271219512195,61.3645365853659,62.6575853658537,42.009756097561,45.3844146341463,43.6538780487805,43.9835609756098,68.2995365853659,67.8963902439025,69.7707317073171,58.8855365853659,57.7238780487805,59.2851219512195,63.7302195121951,59.0670243902439,46.4874878048781,49.969512195122,34.3638048780488,49.0362926829268,41.0180487804878,45.1098048780488,51.5424634146342)
Life_Expectancy_At_Birth_2013 <- c(75.3286585365854,60.0282682926829,51.8661707317073,77.537243902439,77.1956341463415,75.9860975609756,74.5613658536585,75.7786585365854,82.1975609756098,80.890243902439,70.6931463414634,56.2516097560976,80.3853658536585,59.3120243902439,58.2406341463415,71.245243902439,74.4658536585366,76.5459512195122,75.0735365853659,76.2769268292683,72.4707317073171,69.9820487804878,67.9134390243903,74.1224390243903,75.3339512195122,78.5466585365854,69.1029268292683,64.3608048780488,49.8798780487805,81.4011219512195,82.7487804878049,81.1979268292683,75.3530243902439,51.2084634146342,55.0418048780488,61.6663902439024,73.8097317073171,62.9321707317073,72.9723658536585,79.2252195121951,79.2563902439025,79.9497804878049,78.2780487804878,81.0439024390244,61.6864634146342,80.3024390243903,73.3199024390244,74.5689512195122,75.648512195122,70.9257804878049,63.1778780487805,82.4268292682927,76.4243902439025,63.4421951219512,80.8317073170732,69.9179268292683,81.9682926829268,68.9733902439024,63.8435853658537,80.9560975609756,74.079512195122,61.1420731707317,58.216487804878,59.9992682926829,54.8384146341464,57.2908292682927,80.6341463414634,73.1935609756098,71.4863902439024,78.872512195122,66.3100243902439,83.8317073170732,72.9428536585366,77.1268292682927,62.4011463414634,75.2682926829268,68.7046097560976,67.6604146341463,81.0439024390244,75.1259756097561,69.4716829268293,83.1170731707317,82.290243902439,73.4689268292683,73.9014146341463,83.3319512195122,70.45,60.9537804878049,70.2024390243902,67.7720487804878,65.7665853658537,81.459756097561,74.462756097561,65.687243902439,80.1288780487805,60.5203902439024,71.6576829268293,74.9127073170732,74.2402926829268,49.3314634146342,74.1634146341464,81.7975609756098,73.9804878048781,80.3391463414634,73.7090487804878,68.811512195122,64.6739024390244,76.6026097560976,76.5326585365854,75.1870487804878,57.5351951219512,80.7463414634146,65.6540975609756,74.7583658536585,69.0618048780488,54.641512195122,62.8027073170732,74.46,61.466,74.567512195122,64.3438780487805,77.1219512195122,60.8281463414634,52.4421463414634,74.514756097561,81.1048780487805,81.4512195121951,69.222,81.4073170731707,76.8410487804878,65.9636829268293,77.4192195121951,74.2838536585366,68.1315609756097,62.4491707317073,76.8487804878049,78.7111951219512,80.3731707317073,72.7991707317073,76.3340731707317,78.4184878048781,74.4634146341463,71.0731707317073,63.3948292682927,74.1776341463415,63.1670487804878,65.878756097561,82.3463414634146,67.7189268292683,50.3631219512195,72.4981463414634,55.0230243902439,55.2209024390244,66.259512195122,70.99,76.2609756097561,80.2780487804878,81.7048780487805,48.9379268292683,74.7157804878049,51.1914878048781,59.1323658536585,74.2469268292683,69.4001707317073,65.4565609756098,67.5223658536585,72.6403414634147,70.3052926829268,73.6463414634147,75.1759512195122,64.2918292682927,57.7676829268293,71.159512195122,76.8361951219512,78.8414634146341,68.2275853658537,72.8108780487805,74.0744146341464,79.6243902439024,75.756487804878,71.669243902439,73.2503902439024,63.583512195122,56.7365853658537,58.2719268292683,59.2373658536585,55.633)
#(c) Kirill Eremenko, www.superdatascience.com
P2.Section5.Homework.Data <- read.csv("H:/Program Files/RStudio/P2-Section5-Homework-Data.csv")
View(P2.Section5.Homework.Data)
head(P2.Section5.Homework.Data)
#generate a data.frame from loaded values
yr1960 <- data.frame(Country_Code, Life_Expectancy_At_Birth_1960)
yr2013 <- data.frame(Country_Code, Life_Expectancy_At_Birth_2013)
#filter master data to only ncessary periods
P2_1960 <- P2.Section5.Homework.Data[P2.Section5.Homework.Data$Year == 1960,]
P2_2013 <- P2.Section5.Homework.Data[P2.Section5.Homework.Data$Year == 2013,]
head(P2_1960)
#merge data into one frame
New_Data1960 <- merge(P2_1960,yr1960, by.x = "Country.Code", by.y = "Country_Code")
head(New_Data1960)
names(New_Data1960)
New_Data2013 <- merge(P2_2013,yr2013, by.x = "Country.Code", by.y = "Country_Code")
head(New_Data1960)
#create scatter plot 1960
# plot(data=stats, x= Internet.users,y=Birth.rate,
# colour=Income.Group,size=I(4))
plot(data=New_Data1960, x= Fertility.Rate, y= Life_Expectancy_At_Birth_1960, colour = Region)
Try:
//To convert the Fertility rate to numeric for showing continuous x-scale on graph
New_Data1960$Fertility.Rate <- as.numeric(New_Data1960$Fertility.Rate)
ggplot(New_Data1960, aes(Fertility.Rate, Life_Expectancy_At_Birth_1960, group=Region, color=factor(Region))) +
geom_point(aes(color=Region))
The first line tells gives details about the plot
and the second line actually plots it into the plot.
Output is as follows:

Colors in Rcharts

I am trying to generate bar plots / columns using rCharts(v 0.4.2). My problem is that I have an year's worth of data and I need to group on Months. So in Total I have 12 bars that I need to display. However, I have only 9 unique colors after which the colors start repeating. I read this documentation and tried inserting
colors <- c('#7cb5ec','#434348', '#90ed7d', '#f7a35c','#8085e9','#f15c80', '#e4d354','#2b908f','#f45b5b','#91e8e1')
into my code and then calling it as follows :
c <- hPlot(x = 'Confi', y = 'n', data = tablefinalC, type = 'bar', group = 'Month',title = "Inccode By confi",
subtitle = "Bar Graph")
c$plotOptions(series = list(stacking = "normal",colors=paste0('colors'))
c$chart(backgroundColor = NULL)
c$set(dom = 'chart5')
However, I still get the same repetitive colors. So can someone please confirm how I can increase the amount of colors? Thanks in advance
You can create empty chart and then add series
Example
library(rCharts)
df=data.frame(x=1:10,y=-10:-1,z=letters[1:10],stringsAsFactors = F)
colors1=c( '#7cb5ec','#434348', '#90ed7d')
df$col=rep(colors1,round(nrow(df)/length(colors1),0)+1)[1:nrow(df)]
# Create new chart
a <- rCharts:::Highcharts$new()
# Set options
a$chart(type = "bar")
for(i in unique(df$z)){
a$series(name=i,stacking = "normal" ,color=df$col[df$z==i], data= rCharts::toJSONArray2(df[df$z==i,], json=F, names=T))
}
a#plot
Result
Update( re-read question)
if you want to add more colors custominze colors1 and df$col
df=data.frame(x=1:20,y=-20:-1,z=letters[1:20],stringsAsFactors = F)
colors1=c( '#0048BA','#B0BF1A','#7CB9E8','#C9FFE5','#B284BE',
'#5D8AA8','#00308F','#72A0C1','#AF002A','#F0F8FF',
'#84DE02','#E32636','#C46210','#EFDECD','#E52B50',
'#AB274F','#F19CBB','#AB274F','#D3212D','#3B7A57',
'#FFBF00','#FF7E00','#FF033E','#9966CC','#A4C639',
'#F2F3F4','#CD9575','#665D1E','#915C83','#841B2D'
)
df$col=colors1[1:nrow(df)]
Give you

R spplot diagrams on the map

Hallo everyone can anybody help me to upgrade my code with possibility of insering additional data into my map. This is the code that draw me a map with intensity of migration, and I am trying to add ehtnic information of every region (many small pie charts).
to draw a map
con <- url("http://biogeo.ucdavis.edu/data/gadm2/R/UKR_adm1.RData")
print(load(con))
close(con)
name<-gadm$VARNAME_1
value<-c(4,2,5,2,1,2,4,2,2,4,1,1,1,4,3,3,1,1,3,1,2,4,5,3,4,2,1)
gadm$VARNAME_1<-as.factor(value)
col<- colorRampPalette(c('cadetblue4','cadetblue1','mediumseagreen','tan2','tomato3'))(260)
spplot(gadm, "VARNAME_1", main="Ukraine", scales = list(draw = TRUE), col.regions=col)
sp.label <- function(x, label) {
list("sp.text", coordinates(x), label)
}
NAME.sp.label <- function(x) {
sp.label(x, x$NAME_1)
}
draw.sp.label <- function(x) {
do.call("list", NAME.sp.label(x))
}
spplot(gadm, 'VARNAME_1', sp.layout = draw.sp.label(gadm), col.regions=col,
colorkey = list(labels = list( labels = c("Very low","Low", "Average",
"High","Very high"),
width = 1, cex = 1)))
and this is a part of df, that I am trying to add to that map as pie charts or bar charts, with every latitude (lat) and longitude (long) to locate mu bar or pie charts.
df<-data.frame(region=c('Kiev oblast', 'Donezk oblast'),
rus=c(45,35), ukr=c(65,76), mold=c(11,44),long=c(50.43,48),
lat=c(30.52, 37.82))
i found one example and another but... can't figure out how to use it in ma case.
Hope for your help, thank you.
only that solution i have discovered by now, but it doesn't upgrade my code(((
mapPies( df,nameX="lat", nameY="long", nameZs=c('rus','ukr','mold'),
xlim=c(30,33), ylim=c(44,53), symbolSize = 2)
perhaps this will help:
pieSP The function provide SpatialPolygonsDataFrame depending on few attributes, ready to use for plotGoogleMaps or spplot.
library(plotGoogleMaps)
data(meuse)
coordinates(meuse)<-~x+y
proj4string(meuse) <- CRS('+init=epsg:28992')
pies <- pieSP(meuse,zcol=c('zinc','lead','copper'), max.radius=120)
pies$pie <- rep(c('zinc','lead','copper'),155)
pies$pie2 <- rep(1:3,155)
spplot(pies, 'pie2')

Resources