Add 3d column values in hoverinfo when using ggplot2 object in ggplotly - r

my data frame:
structure(list(CNT = c("Albania", "Algeria", "Argentina (Ciudad Autónoma de Buenos)",
"Australia", "Austria", "B-S-J-G (China)", "Belgium", "Brazil",
"Bulgaria", "Canada"), Female = c(417.75, 363.07, 446.03, 490.99,
483.13, 528.19, 499.74, 369.55, 442.16, 511.14), Male = c(408.55,
356.5, 467.31, 496.76, 510.1, 534.01, 514, 385.04, 440.32, 520.17
)), .Names = c("CNT", "Female", "Male"), row.names = c(NA, 10L
), class = "data.frame")
I want to plot the data using a ggplot2 object and not plot_ly so that on hovering the values of all three variables are shown.
If I do the way I want only Male and Female appears on hovering:
p<-ggplot(df, aes(Female, Male)) +
geom_point()
ggplotly(p)
I want the country name to appear as well. Looking on the web I figure it out how to do in plot_ly:
plot_ly( df, x=df$Female , y=df$Male, text=df$CNT,
hoverinfo="text+x+y")
I want to achieve the same result but using a ggplot2 object as in the example above when using object p.

I think you are looking for this:
ggplot(df, aes(x = Female, y = Male, text = paste("CNT:", CNT))) + geom_point()
ggplotly()

Related

Inputting Indefinite Categorical Variables into an R function

I have a panel dataset that I am working with in the current format.
Country
Date
Value
Austria
1956-01-01
1.5
Sweden
1956-01-01
1.2
UnitedKingdom
1956-01-01
1.3
Austria
1957-01-01
1.6
Sweden
1957-01-01
1.0
UnitedKingdom
1957-01-01
1.8
I am currently writing a function to allow the value of one country to act as a "baseline" for a comparitor country on a ggplot graph
PerCap <- function(Data, Nation, Comparitor) {
Nation <- ensym(Nation)
Comparitor <- ensym(Comparitor)
p <- Data %>%
filter(Country %in% c(as_string(Nation), as_string(Comparitor))) %>%
select(c("Country", "Date", "PerCapHouse")) %>%
pivot_wider(names_from = Country, values_from = PerCapHouse) %>%
mutate(Base = !!Nation) %>%
mutate(across(where(is.numeric), ~ .x * 100 / Base)) %>%
select(-c(as_string(Nation), "Base")) %>%
pivot_longer(-c("Date"), names_to = "Country", values_to = "Relative Stock") %>%
ggplot(aes(x = Date, y = `Relative Stock`, color = Country)) + geom_line(size = 1.2) +
geom_hline(yintercept = 100, size = 1.2) +
ggtitle(paste("International Comparison of", as_label(enquo(Nation)), "Housing Stock")) +
labs(y = "Stock Per Capita")
}
Ideally I would like to use the function on an indefinite number of comparitor countries. I understand that I will need to pass something like
function(Data, Nation, ...)
but in this case how would I change the
filter(Country %in% c(as_string(Nation), as_string(Comparitor)))
part to turn every input of the (...) part of he function into a separate string component?
Please find the reproducible Data Below:
Combined <- structure(list(Country = c("Austria", "Sweden", "UnitedKingdom",
"Austria", "Sweden", "UnitedKingdom"), Date = structure(c(-5114,
-5114, -5114, -4748, -4748, -4748), class = "Date"), Value = c(1.5,
1.2, 1.3, 1.6, 1, 1.8)), row.names = c(NA, -6L), class = "data.frame")

R - Print specific country names in a map using rworldmap

I am creating a heatmap using the map of Europe in rworldmap package in R (since I don't know how to do this with ggmap or ggplot2).
I need to plot the country names of the countries that are present in my dataframe only, not all european countries. How can I do this?
My code:
library(RColorBrewer)
#getting colours
colourPalette <- brewer.pal(5,'RdPu')
library(rworldmap)
europe <- data.frame(
"country" = c("Greece",
"France",
"Spain",
"Italy",
"UK",
"Finland","Norway","Sweden",
"Germany",
"Romania"),
"x" = c(2.5, 3, 2.2, 1.8,2.32, 1.99, 2.01, 2.34, 1.88, 2.45))
matched <- joinCountryData2Map(europe, joinCode="NAME", nameJoinColumn="country")
mapParams <- mapCountryData(matched,
nameColumnToPlot="x",
mapTitle="my Titley",
addLegend=FALSE,
mapRegion="Europe"
,colourPalette=colourPalette,
oceanCol="#404040", missingCountryCol="#A0A0A0")
#adding legend
do.call(addMapLegend
,c(mapParams
,legendLabels="all"
,legendWidth=0.5
,legendIntervals="data"
,legendMar = 2))
labelCountries()
Using labelCountries() prints all country names and it's not readable.
Thanks
With a little bit help of this previously answer:
# get the coordinates for each country
country_coord<-data.frame(coordinates(matched),stringsAsFactors=F)
# label the countries
country = c("Greece",
"France",
"Spain",
"Italy",
"UK",
"Finland","Norway","Sweden",
"Germany",
"Romania")
#filter your wanted countrys
country_coord = country_coord[country,]
#insert your labels in plot
text(x=country_coord$X1,y=country_coord$X2,labels=row.names(country_coord))
you can add the country labels with text but you must extract the coordinates before from your matched coordinates.
Output:
You might play a bit with col = "color" in text, since some country can barely been read. Or maybe change the color scale in your map

R: How to plot partially a raster on top of a map using ggplot2?

I want to plot parts of a raster (highest values) on top of a previously plotted map. Here my code:
ggplot() + geom_map(data = GERblue_iran, aes(map_id = state, fill = veggies), map = iran_states) +
expand_limits(x = iran_states$long, y = iran_states$lat) +
geom_polygon(data=iran_states, aes(x= long, y= lat, group=group), color='grey40', lwd=0.3, fill=NA) +
scale_fill_distiller(palette="PuBu")
resulting in this plot:
And the code for the raster overlay:
ggplot() + geom_tile(data=nut_iran.df, aes(x = x, y = y, fill = layer)) + scale_fill_distiller(palette = "Reds")
My expected result should look (more or less) like this (result made in photoshop!!):
showing only values (of raster) above +/-1000.
Does anyone know how to plot like this in ggplot2?
For reproducibility, here a bit of my data:
library(ggplot2)
# polygons
iran <- getData("GADM", country = "IRN", level = 1)
iran_states <- fortify(iran, region = "NAME_1")
# data for choropleth map:
GERblue_iran <- structure(list(veggies = c(NA, 1135142.7169744, 1064475.14405642,
579007.139090945, 2291173.06203667, 1609487.86612194, 5514745.42173307,
210033.193615536, NA, 1082275.82518455, 395053.664034339, 833546.886449334,
1350410.79594876, 2030498.45168616, 5018327.9046678, 413119.296060151,
853322.135586823, 2136776.14200603, 581494.047168068, 535593.624579909,
414310.523642145, NA, NA, 2156369.86690811, 274390.590608389,
546804.909031463, 144406.95766963, 285002.432443622, 1605244.30546598,
307546.827903725, 589330.238261654), fruits = c(NA, 19645300.9396573,
39318516.6693754, 15154130.3351692, 38374281.8287458, 29164989.9985857,
125240289.719822, 6419392.00424945, NA, 23342736.5294504, 9223806.43987587,
19008972.7788205, 62709223.291618, 41703691.9392781, 164306013.73773,
13518682.0729514, 17420595.3934969, 44462391.2814304, 11715807.4374495,
13475005.0070146, 12228946.6624824, NA, NA, 63708363.0757236,
9221772.9477743, 13545791.4738047, 4268610.12809181, 6496251.74039526,
31651316.7352119, 8570276.47057257, 10288282.5059752), nuts = c(NA,
108771666.285736, 188713516.14938, 84626256.2539182, 227028948.551643,
165167762.523232, 669060935.751113, 17905599.826691, NA, 124536243.958677,
62369588.7036755, 123253859.776379, 3137384087.58166, 279956412.016931,
506078060.03775, 70275261.7698334, 115596090.695869, 284469721.056207,
73232219.7923014, 47691287.4633623, 73453936.1223698, NA, NA,
382631908.316345, 78226462.4088062, 60449633.6571361, 25656409.607032,
36523271.0224757, 233944364.555385, 158201233.377931, 74033085.0714528
), state = c("Alborz", "Ardebil", "Bushehr", "Chahar Mahall and Bakhtiari",
"East Azarbaijan", "Esfahan", "Fars", "Gilan", "Golestan", "Hamadan",
"Hormozgan", "Ilam", "Kerman", "Kermanshah", "Khuzestan", "Kohgiluyeh and Buyer Ahmad",
"Kordestan", "Lorestan", "Markazi", "Mazandaran", "North Khorasan",
"Qazvin", "Qom", "Razavi Khorasan", "Semnan", "Sistan and Baluchestan",
"South Khorasan", "Tehran", "West Azarbaijan", "Yazd", "Zanjan"
)), .Names = c("veggies", "fruits", "nuts", "state"), row.names = c(NA,
31L), class = "data.frame")
However, the raster data set is too large and I don't know how to make a random raster with this country extent. However, if someone knows how. Here is the transformation I did in order to plot it with geom_tile():
nut_iran.spdf <- as(nut_iran, "SpatialPixelsDataFrame")
nut_iran.df <- as.data.frame(nut_iran.spdf)
Thanks for your ideas.
EDIT: I managed to plot as #spacedman suggested the raster as I want, setting all values in nut_iran.df$layer < 1000 into NAs. And plot them inculding na.value="transparent" to scale_fill_distiller. Result which is fine:
However, plotting both together delivers:
due to the 2 different fills (pointed by #hrbrmstr) the color are set to last colors (red) and the previous NA from the choropleth map are as well setted transparent.

time series plot in R

My data looks something like this:
There are 10,000 rows, each representing a city and all months since 1998-01 to 2013-9:
RegionName| State| Metro| CountyName| 1998-01| 1998-02| 1998-03
New York| NY| New York| Queens| 1.3414| 1.344| 1.3514
Los Angeles| CA| Los Angeles| Los Angeles| 12.8841| 12.5466| 12.2737
Philadelphia| PA| Philadelphia| Philadelphia| 1.626| 0.5639| 0.2414
Phoenix| AZ| Phoenix| Maricopa| 2.7046| 2.5525| 2.3472
I want to be able to do a plot for all months since 1998 for any city or more than one city.
I tried this but i get an error. I am not sure if i am even attempting this right. Any help will be appreciated. Thank you.
forecl <- ts(forecl, start=c(1998, 1), end=c(2013, 9), frequency=12)
plot(forecl)
Error in plots(x = x, y = y, plot.type = plot.type, xy.labels = xy.labels, :
cannot plot more than 10 series as "multiple"
You might try
require(reshape)
require(ggplot2)
forecl <- melt(forecl, id.vars = c("region","state","city"), variable_name = "month")
forecl$month <- as.Date(forecl$month)
ggplot(forecl, aes(x = month, y = value, color = city)) + geom_line()
To add to #JLLagrange's answer, you might want to pass city through facet_grid() if there are too many cities and the colors will be hard to distinguish.
ggplot(forecl, aes(x = month, y = value, color = city, group = city)) +
geom_line() +
facet_grid( ~ city)
Could you provide an example of your data, e.g. dput(head(forecl)), before converting to a time-series object? The problem might also be with the ts object.
In any case, I think there are two problems.
First, data are in wide format. I'm not sure about your column names, since they should start with a letter, but in any case, the general idea would be do to something like this:
test <- structure(list(
city = structure(1:2, .Label = c("New York", "Philly"),
class = "factor"), state = structure(1:2, .Label = c("NY",
"PA"), class = "factor"), a2005.1 = c(1, 1), a2005.2 = c(2, 5
)), .Names = c("city", "state", "a2005.1", "a2005.2"), row.names = c(NA,
-2L), class = "data.frame")
test.long <- reshape(test, varying=c(3:4), direction="long")
Second, I think you are trying to plot too many cities at the same time. Try:
plot(forecl[, 1])
or
plot(forecl[, 1:5])

How to create a world map in R with specific countries filled in?

I would like to use R to generate a very basic world map with a specific set of countries filled with a red colour to indicate that they are malaria endemic countries.
I have a list of these countries in a data frame but am struggling to overlay them on a world map.
I have tried using the wrld_simpl object and also the joinCountryData2Map method in the rworldmap package.
I would comment on this answer to prevent addition of a possibly redundant question but I do not have enough reputation at the moment, apologies for this.
https://stackoverflow.com/a/9102797/1470099
I am having difficulty understanding the arguments given to the plot() command - I wondered if there was just an easy way to tell R to plot all of the country NAMEs in my list on the wrld_simpl map instead of using grepl() etc. etc.
plot(wrld_simpl,
col = c(gray(.80), "red")[grepl("^U", wrld_simpl#data$NAME) + 1])
Using the rworldmap package, you could use the following:
library(rworldmap)
theCountries <- c("DEU", "COD", "BFA")
# These are the ISO3 names of the countries you'd like to plot in red
malDF <- data.frame(country = c("DEU", "COD", "BFA"),
malaria = c(1, 1, 1))
# malDF is a data.frame with the ISO3 country names plus a variable to
# merge to the map data
malMap <- joinCountryData2Map(malDF, joinCode = "ISO3",
nameJoinColumn = "country")
# This will join your malDF data.frame to the country map data
mapCountryData(malMap, nameColumnToPlot="malaria", catMethod = "categorical",
missingCountryCol = gray(.8))
# And this will plot it, with the trick that the color palette's first
# color is red
EDIT: Add other colors and include picture
## Create multiple color codes, with Burkina Faso in its own group
malDF <- data.frame(country = c("DEU", "COD", "BFA"),
malaria = c(1, 1, 2))
## Re-merge
malMap <- joinCountryData2Map(malDF, joinCode = "ISO3",
nameJoinColumn = "country")
## Specify the colourPalette argument
mapCountryData(malMap, nameColumnToPlot="malaria", catMethod = "categorical",
missingCountryCol = gray(.8), colourPalette = c("red", "blue"))
Try using googleVis package and use gvisGeoMap Functions
e.g.
G1 <- gvisGeoMap(Exports,locationvar='Country',numvar='Profit',options=list(dataMode='regions'))
plot(G1)
library(maptools)
data(wrld_simpl)
myCountries = wrld_simpl#data$NAME %in% c("Australia", "United Kingdom", "Germany", "United States", "Sweden", "Netherlands", "New Zealand")
plot(wrld_simpl, col = c(gray(.80), "red")[myCountries+1])

Resources