I am looking for some help with the given sample data of countries on one column and count on another column. I am trying a build a geo maps using ggplot showing the count and name of the country in the respective places of the map when I hover above the country. Below is the sample data given. I tried with the ggmap with the lat and long position to identify the country but not able to show the count and name of the country on hovering.
structure(list(Countries = c("USA", "India", "Europe", "LATAM",
"Singapore", "Phillipines", "Australia", "EMEA", "Malaysia",
"Hongkong", "Philippines", "Thailand", "New Zealand"
), count = c(143002, 80316, 33513, 3736, 2180, 1905, 1816, 921,
707, 631, 207, 72, 49)), .Names = c("Countries", "count"), row.names = c(NA,
13L), class = "data.frame")
I tried the below code.
countries = geocode(Countryprofile$Countries)
Countryprofile = cbind(Countryprofile,countries)
mapWorld <- borders("world", colour="grey", fill="lightblue")
q<-ggplot(data = Countryprofile) + mapWorld + geom_point(aes(x=lon, y=lat) ,color="red", size=3)+
geom_text(data = Countryprofile,aes(x=lon,y=lat,label=Countries))
ggplotly(q)
You can change any attribute in the result from ggplotly. In this case you can set the text attribute of the 2nd trace (where you markers are defined).
plotly_map <- ggplotly(q)
plotly_map$x$data[[2]]$text <- paste(Countryprofile$Countries,
Countryprofile$count,
sep='<br />')
plotly_map
library(plotly)
library(ggmap)
Countryprofile <- structure(list(Countries = c("USA", "India", "Europe", "LATAM",
"Singapore", "Phillipines", "Australia", "EMEA", "Malaysia",
"Hongkong", "Philippines", "Thailand", "New Zealand"
), count = c(143002, 80316, 33513, 3736, 2180, 1905, 1816, 921,
707, 631, 207, 72, 49)), .Names = c("Countries", "count"), row.names = c(NA,
13L), class = "data.frame")
countries = geocode(Countryprofile$Countries)
Countryprofile = cbind(Countryprofile,countries)
mapWorld <- borders("world", colour="grey", fill="lightblue")
q<-ggplot(data = Countryprofile) + mapWorld + geom_point(aes(x=lon, y=lat) ,color="red", size=3)+
geom_text(data = Countryprofile,aes(x=lon,y=lat,label=Countries))
plotly_map <- ggplotly(q)
plotly_map$x$data[[2]]$text <- paste(Countryprofile$Countries, Countryprofile$count, sep='<br />')
plotly_map
Related
I am trying to replicate a plot using ggplot2 to show the performance between two soccer teams. I look for getting this plot:
I have the data df for that. This is the code I have been using:
library(ggplot2)
#Plot
ggplot(df,aes(x=type,y=value,fill=name))+
geom_bar(stat = 'identity')+
geom_text(aes(label=abs(value),hjust=1))+
coord_flip()
Which produces next result:
The result is far away from first plot.
The issues I face are next:
The scale is too large for some type values, so is not possible to see each measure with independent scale. I think a facet_grid() or facet_wrap() would solve this but it did not worked.
The values for type must go in the middle, but I do not know how to move the axis to middle.
The labels for each bar should be on top of each bar but when I adjust hjust one of them goes to the right place but the other is placed wrong.
Finally, in df there is column named logo which stores the flags of two teams, how I can add the flags for each team one at top right side and the other at top left side.
Many thanks. This is the data df.
#Data
df <- structure(list(type = c("Shots on Goal", "Shots off Goal", "Total Shots",
"Blocked Shots", "Shots insidebox", "Shots outsidebox", "Fouls",
"Corner Kicks", "Offsides", "Ball Possession % ", "Yellow Cards",
"Red Cards", "Goalkeeper Saves", "Total passes", "Passes accurate",
"Passes %", "Shots on Goal", "Shots off Goal", "Total Shots",
"Blocked Shots", "Shots insidebox", "Shots outsidebox", "Fouls",
"Corner Kicks", "Offsides", "Ball Possession % ", "Yellow Cards",
"Red Cards", "Goalkeeper Saves", "Total passes", "Passes accurate",
"Passes %"), value = c(7, 2, 13, 4, 10, 3, 9, 8, 2, 78, 0, 0,
1, 797, 716, 90, -3, -4, -8, -1, -6, -2, -14, 0, -2, -22, -2,
0, -1, -215, -142, -66), name = c("England", "England", "England",
"England", "England", "England", "England", "England", "England",
"England", "England", "England", "England", "England", "England",
"England", "Iran", "Iran", "Iran", "Iran", "Iran", "Iran", "Iran",
"Iran", "Iran", "Iran", "Iran", "Iran", "Iran", "Iran", "Iran",
"Iran"), logo = c("https://media.api-sports.io/football/teams/10.png",
"https://media.api-sports.io/football/teams/10.png", "https://media.api-sports.io/football/teams/10.png",
"https://media.api-sports.io/football/teams/10.png", "https://media.api-sports.io/football/teams/10.png",
"https://media.api-sports.io/football/teams/10.png", "https://media.api-sports.io/football/teams/10.png",
"https://media.api-sports.io/football/teams/10.png", "https://media.api-sports.io/football/teams/10.png",
"https://media.api-sports.io/football/teams/10.png", "https://media.api-sports.io/football/teams/10.png",
"https://media.api-sports.io/football/teams/10.png", "https://media.api-sports.io/football/teams/10.png",
"https://media.api-sports.io/football/teams/10.png", "https://media.api-sports.io/football/teams/10.png",
"https://media.api-sports.io/football/teams/10.png", "https://media.api-sports.io/football/teams/22.png",
"https://media.api-sports.io/football/teams/22.png", "https://media.api-sports.io/football/teams/22.png",
"https://media.api-sports.io/football/teams/22.png", "https://media.api-sports.io/football/teams/22.png",
"https://media.api-sports.io/football/teams/22.png", "https://media.api-sports.io/football/teams/22.png",
"https://media.api-sports.io/football/teams/22.png", "https://media.api-sports.io/football/teams/22.png",
"https://media.api-sports.io/football/teams/22.png", "https://media.api-sports.io/football/teams/22.png",
"https://media.api-sports.io/football/teams/22.png", "https://media.api-sports.io/football/teams/22.png",
"https://media.api-sports.io/football/teams/22.png", "https://media.api-sports.io/football/teams/22.png",
"https://media.api-sports.io/football/teams/22.png")), row.names = c(NA,
-32L), class = c("tbl_df", "tbl", "data.frame"))
Perhaps something like this?
library(tidyverse)
library(ggimage)
df %>%
group_by(type) %>%
mutate(tot = sum(abs(value)),
prop = value/tot) %>%
ggplot(aes(prop, type, color = name)) +
geom_linerange(aes(xmin = -1, xmax = 1), color = "gray95", linewidth = 3) +
geom_linerange(aes(xmin = prop, xmax = 0), linewidth = 3) +
scale_color_manual(values = c("#8ded05", "#00aaff")) +
geom_text(aes(label = type, x = 0), check_overlap = TRUE, nudge_y = 0.4,
color = "black") +
geom_text(aes(x = ifelse(name == "England", 1.05, -1.05),
label = abs(value)), color = "black") +
theme_void() +
scale_y_discrete(expand = c(0.1, 0)) +
annotate(geom = "text", x = c(-0.5, 0.5), y = c(17, 17),
label = c("Iran", "England"), size = 5) +
geom_image(data = data.frame(x = c(1, -1), y = 17,
image = unique(df$logo)),
aes(x, y, image = image), inherit.aes = FALSE)+
guides(color = guide_none())
I have the following dataframe:
structure(list(share.beer = c(0.277, 0.1376, 0.1194, 0.0769,
0.0539, 0.0361, 0.0361, 0.0351, 0.0313, 0.03, 0.0119, 0.0084,
0.007, 0.0069), country = c("Brazil", "China, mainland", "United States",
"Thailand", "Vietnam", "China, mainland", "China, mainland",
"China, mainland", "China, mainland", "Argentina", "Indonesia",
"China, mainland", "China, mainland", "India"), Beer = c("soyb",
"maiz", "soyb", "cass", "cass", "whea", "rape", "soyb", "rice",
"soyb", "cass", "cott", "swpo", "rape")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -14L))
I want to create a barplot so that the beer type appears in the legend, the countries as y values while the share.beer are my values to be filled.
I have tried in various ways, including the following code, but I can't get the result I would like to. Here, for instance, I kept the variable "Beer""
df %>%
pivot_longer(cols = -Country, values_to = "Count", names_to = "Type") %>%
ggplot() +
geom_col(aes(x = reorder(Country, -Count), y = Count, fill = Beer))
However, I get an error
Can't combine share beer and Beer .
Any help?
You actually don't need the pivot_longer to create a suitable dataframe. You can use the following code:
library(tidyverse)
df %>%
ggplot() +
geom_col(aes(x = reorder(country, -share.beer), y = share.beer, fill = Beer)) +
xlab("Country") +
ylab("Share beer") +
coord_flip()
Output:
I am looking to plot some data for APAC countries for a an upcoming presentation. I followed this tutorial however my output is different. Below is some sample data I would like to plot -
country_list <-
c("Australia", "India", "New Zealand", "Singapore", "Malasia", "China", "Indonesia",
"Hong Kong", "Hong Kong", "Japan", "Philipines", "Thailand", "Vietnam", "Korea", "Taiwan",
"Cambodia", "Mongolia, Myanmar", "Laos")
values <-
c(33260, 24586, 5468, 2698, 2547, 6248, 3654, 6589, 2545, 2548, 835, 536, 565, 665,
236, 548, 158, 152)
data <- cbind(country_list, values)
Then here is how I plot the map -
library(tidyverse)
library(mapdata)
countries <-
map_data("world",
region = country_list)
data2 <- merge(countries, data, by.x = "region", by.y = "country_list") %>%
mutate(values = values %>% as.numeric)
ggplot(data2, aes(long, lat, group = group, fill = values))+
geom_polygon()
And here is my output -
Clearly not fit for a presentation. How can I improve on this map, perhaps add some interactivity to it.
your code is not reproducible, I can just filter after 0 longitude to show well
ggplot(countries %>% filter(long>0), aes(long, lat, group = group))+
geom_polygon()
I have been trying to plot an animation which will show the number of refugees over the years. The code runs without issues, but it won't show the output animation. Could someone please tell me what I am doing wrong?
animation <- refugees_clean %>%
ggplot( aes(x=year, y=number, group= origin_region, color=origin_region)) +
geom_line() +
geom_point() +
scale_color_discrete() +
ggtitle("Refugee numbers over the years") +
theme_dark() +
ylab("Number of refugees") +
transition_reveal(year)
animation
dput(head(refugees_clean))
structure(list(origin_country = c("Afghanistan", "Angola", "Armenia",
"Azerbaijan", "Belarus", "Bhutan"), iso3 = c("AFG", "AGO", "ARM",
"AZE", "BLR", "BTN"), origin_region = c("South Asia", "Sub-Saharan
Africa",
"Europe & Central Asia", "Europe & Central Asia", "Europe & Central
Asia",
"South Asia"), origin_continent = c("Asia", "Africa", "Asia",
"Asia", "Europe", "Asia"), year = c(2006, 2006, 2006, 2006, 2006,
2006), number = c(651, 13, 87, 77, 350, 3), year_date =
structure(c(13149,
13149, 13149, 13149, 13149, 13149), class = "Date")), row.names =
c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
I have a dataset that I am presenting facetted by region and then using sub region as a fill. I have defined the colours using a separate named variable relating to the names of the subregion. I am wondering if it is possible to make the legend itself grouped in a similar way to the facet to make it easier to interpret.
The named sub_region variable
sub_region_colours <- c("South America" = "#0570b0", "Western Africa" = "#8c96c6", "Central America" = "#74a9cf", "Eastern Africa" = "#8856a7", "Northern Africa" = "#edf8fb", "Middle Africa" = "#b3cde3", "Southern Africa" = "#810f7c", "Northern America" = "#f1eef6", "Caribbean" = "#bdc9e1", "Eastern Asia" = "#bd0026", "Southern Asia" = "#fd8d3c", "South-Eastern Asia" = "#f03b20", "Southern Europe" = "#238b45", "Australia and New Zealand" = "#ce1256", "Melanesia" = "#df65b0", "Micronesia" = "#d7b5d8", "Polynesia" = "#f1eef6", "Central Asia" = "#fecc5c", "Western Asia" = "#ffffb2", "Eastern Europe" = "#66c2a4", "Northern Europe" = "#edf8fb", "Western Europe" = "#b2e2e2", "Small Islands" = "#252525")
This is the head(exporting_countries) grouping by sender_iso3, year and sender_region removed.
structure(list(sender_iso3 = c("ABW", "ABW", "ABW", "ABW", "ABW",
"ABW"), year = c(2005, 2011, 2014, 2015, 2016, 2017), sender_region = c("Americas",
"Americas", "Americas", "Americas", "Americas", "Americas"),
sender_subregion = c("Caribbean", "Caribbean", "Caribbean",
"Caribbean", "Caribbean", "Caribbean"), export = c(1, 1,
4, 5, 2, 1)), class = "data.frame", row.names = c(NA, -6L
))
Finally this is the code for the current plot
geom_bar()+
labs(title = "Number of countries reporting export of chickens",
fill = "Subregion")+
facet_wrap(~ sender_region)+
theme_minimal()+
scale_x_continuous(name = "Year", limits = c(1986, 2017), breaks = c(1986, 1990, 2000, 2010, 2017), guide = guide_axis(angle = 90))+
scale_fill_manual(values = sub_region_colours)+
guides(fill = guide_legend(ncol = 2))
Which at the moment produces this:
Graph with less than ideal legend
It would be great if I can group the legend fill colours similarly to the facets which would make it easier to read off.
One approach to achieve this would be to make seperate plots for each region and make use of patchwork to glue the plots together. A second approach would be to make use of the ggnewscale package which allows to have multiple fill (or ...) scales and legends in one plot.
However, similiar to using patchwork the approach using ggnewscale package could become a bit tedious as it requires to split the data according to the number of facets and plot each dataset via seperate layers. Therefore my solution adds a helper function which 1) splits the data and sets up the layers for each region or facet and 2) can be used to loop over the regions via e.g. lapply.
BTW: As your sample data included only one region I added a second region.
library(dplyr)
library(ggplot2)
library(ggnewscale)
sub_region_colours <- c("South America" = "#0570b0", "Western Africa" = "#8c96c6", "Central America" = "#74a9cf", "Eastern Africa" = "#8856a7", "Northern Africa" = "#edf8fb", "Middle Africa" = "#b3cde3", "Southern Africa" = "#810f7c", "Northern America" = "#f1eef6", "Caribbean" = "#bdc9e1", "Eastern Asia" = "#bd0026", "Southern Asia" = "#fd8d3c", "South-Eastern Asia" = "#f03b20", "Southern Europe" = "#238b45", "Australia and New Zealand" = "#ce1256", "Melanesia" = "#df65b0", "Micronesia" = "#d7b5d8", "Polynesia" = "#f1eef6", "Central Asia" = "#fecc5c", "Western Asia" = "#ffffb2", "Eastern Europe" = "#66c2a4", "Northern Europe" = "#edf8fb", "Western Europe" = "#b2e2e2", "Small Islands" = "#252525")
d <- structure(list(sender_iso3 = c(
"ABW", "ABW", "ABW", "ABW", "ABW",
"ABW", "ABW", "ABW", "ABW", "ABW", "ABW", "ABW"
), year = c(
2005,
2011, 2014, 2015, 2016, 2017, 2005, 2011, 2014, 2015, 2016, 2017
), sender_region = c(
"Americas", "Americas", "Americas", "Americas",
"Americas", "Americas", "Africa", "Africa", "Africa", "Africa",
"Africa", "Africa"
), sender_subregion = c(
"Caribbean", "Caribbean",
"Caribbean", "Caribbean", "Caribbean", "Caribbean", "Southern Africa",
"Southern Africa", "Southern Africa", "Southern Africa", "Southern Africa",
"Southern Africa"
), export = c(
1, 1, 4, 5, 2, 1, 1, 1, 4, 5,
2, 1
)), class = "data.frame", row.names = c(NA, -12L))
regions <- unique(d$sender_region)
# Layers for each region
make_layers <- function(x) {
d <- filter(d, sender_region == regions[[x]])
list(
if (x != 1) new_scale_fill(),
geom_bar(data = d, aes(x = year, fill = sender_subregion)),
scale_fill_manual(
values = sub_region_colours,
guide = guide_legend(
order = x,
title = regions[x],
title.position = "top"
)
)
)
}
p <- ggplot() +
lapply(seq_along(regions), make_layers)
# Add theme and wrap
p +
theme_minimal() +
scale_x_continuous(
name = "Year", limits = c(1986, 2017),
breaks = c(1986, 1990, 2000, 2010, 2017),
guide = guide_axis(angle = 90)
) +
facet_wrap(~sender_region)