How to create a double bar contrast plot using ggplot2 - r

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())

Related

ordering of ggplot not working with factors

I have created a vector with the order of a dot plot mentioned but it doesn't plot in that order/ Thanks for the suggestions.
order <- sav %>%
filter(Subject == "Food") %>%
arrange(desc(Percentage)) %>%
select(Location) %>%
unlist() %>%
unname()
order <- replace(order, c(1, 8), order[c(8, 1)])
sav %>%
ggplot(aes(x = factor(Location, levels = order), y = Percentage,
color = Subject))+
geom_point(data = filter(sav, Location != "IRELAND"),
size = 4, position = position_dodge(0.5))+
geom_point(data = filter(sav, Location == "IRELAND"),
size = 6, position = position_dodge(1))+
geom_linerange(data = filter(sav, Location == "IRELAND"),
aes(ymin = 0, ymax = Percentage),
position = position_dodge(1),
linetype = "dotdash") +
geom_linerange(data = filter(sav, Location != "IRELAND"),
aes(ymin = 0, ymax = Percentage),
position = position_dodge(0.5), linetype = "dotdash") +
coord_flip()+
ggtitle(label = "Increase in inflation (by CPI) in Ireland compared to OECD and other countries in OECD")+
xlab("Countries --> ") +
ylab("Increase in CPI by % -->")+
scale_y_continuous(breaks = round(seq(0, 20, by = 1),1))+
scale_color_manual(name = "Type of Items",
labels = c("Food", "Total", "Excluding food and energy"),
values=c(unname(colorblind_colors[2]),
unname(colorblind_colors[3]),
unname(colorblind_colors[4])))+
theme(panel.grid.major.x = element_line(linewidth =.01, color="black"),
panel.grid.major.y = element_blank(),
legend.position = "top"
)
> dput(order)
c("IRELAND", "NETHERLANDS", "SPAIN", "OECD", "ITALY", "FRANCE",
"UNITED STATES", "GERMANY", "CANADA")
> dput(sav)
structure(list(Location = c("CANADA", "CANADA", "FRANCE", "FRANCE",
"GERMANY", "GERMANY", "IRELAND", "IRELAND", "ITALY", "ITALY",
"NETHERLANDS", "NETHERLANDS", "SPAIN", "SPAIN", "UNITED STATES",
"UNITED STATES", "OECD", "OECD", "CANADA", "ITALY", "SPAIN",
"FRANCE", "IRELAND", "UNITED STATES", "NETHERLANDS", "OECD",
"GERMANY"), Subject = c("Food", "Total", "Food", "Total", "Food",
"Total", "Food", "Total", "Food", "Total", "Food", "Total", "Food",
"Total", "Food", "Total", "Food", "Total", "Total_Minus_Food_Energy",
"Total_Minus_Food_Energy", "Total_Minus_Food_Energy", "Total_Minus_Food_Energy",
"Total_Minus_Food_Energy", "Total_Minus_Food_Energy", "Total_Minus_Food_Energy",
"Total_Minus_Food_Energy", "Total_Minus_Food_Energy"), Frequency = c("Monthly",
"Monthly", "Monthly", "Monthly", "Monthly", "Monthly", "Monthly",
"Monthly", "Monthly", "Monthly", "Monthly", "Monthly", "Monthly",
"Monthly", "Monthly", "Monthly", "Monthly", "Monthly", "Monthly",
"Monthly", "Monthly", "Monthly", "Monthly", "Monthly", "Monthly",
"Monthly", "Monthly"), Time = c("2022-12", "2022-12", "2022-12",
"2022-12", "2022-12", "2022-12", "2022-12", "2022-12", "2022-12",
"2022-12", "2022-12", "2022-12", "2022-12", "2022-12", "2022-12",
"2022-12", "2022-12", "2022-12", "2022-12", "2022-12", "2022-12",
"2022-12", "2022-12", "2022-12", "2022-12", "2022-12", "2022-12"
), Percentage = c(11.02015, 6.319445, 12.86678, 5.850718, 19.75631,
8.550855, 11.74636, 8.224299, 13.14815, 11.63227, 16.7983, 9.586879,
15.68565, 5.70769, 11.88275, 6.454401, 15.60381, 9.438622, 5.58275,
4.469475, 4.442303, 3.36004, 4.999758, 5.707835, 6.034873, 7.221961,
5.05511)), class = "data.frame", row.names = c(NA, -27L))
A couple of things:
The factors need to be in the data that ggplot sees at every geom, but while you're setting factor(Location,levels=order) in the first mapping, none of the data= arguments is using the same data.
For this, I generally prefer factorizing the data up-front, and using ~-style "functions" for data=.
Not sure exactly why this is the case, but it still doesn't work ... but if the first call to geom_point uses mutate and replaces IRELAND's percentage values with NA, the levels are retained. Weird.
### I still don't have these :-)
colorblind_colors <- 1:4
sav %>%
mutate(Location = factor(Location, levels = order)) %>%
ggplot(aes(x = Location, y = Percentage, color = Subject)) +
geom_point(data = ~ mutate(., Percentage = if_else(Location == "Ireland", Percentage[NA], Percentage)),
size = 4, position = position_dodge(0.5), na.rm = TRUE) +
geom_point(data = ~ filter(., Location == "IRELAND"),
size = 6, position = position_dodge(1)) +
geom_linerange(data = ~ filter(., Location == "IRELAND"),
aes(ymin = 0, ymax = Percentage),
position = position_dodge(1),
linetype = "dotdash") +
geom_linerange(data = ~ filter(., Location != "IRELAND"),
aes(ymin = 0, ymax = Percentage),
position = position_dodge(0.5), linetype = "dotdash") +
coord_flip()+
ggtitle(label = "Increase in inflation (by CPI) in Ireland compared to OECD and other countries in OECD")+
xlab("Countries --> ") +
ylab("Increase in CPI by % -->")+
scale_y_continuous(breaks = round(seq(0, 20, by = 1),1))+
scale_color_manual(name = "Type of Items",
labels = c("Food", "Total", "Excluding food and energy"),
values=c(unname(colorblind_colors[2]),
unname(colorblind_colors[3]),
unname(colorblind_colors[4])))+
theme(panel.grid.major.x = element_line(linewidth =.01, color="black"),
panel.grid.major.y = element_blank(),
legend.position = "top"
)

R studio Barplot

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:

Add_annotations plotly first and last datapoints

My Main Goal:
Trying to add annotations to both the first datapoint of my
scatterplot and the last datapoint of my scatterplot (the entries for
years 2006 and 2021 respectively).
My Secondary Goals:
If possible, it would also be helpful to find out how to select out
specific datapoints to add annotations, as I only know the
which.max/which.min functions so far.
It would also be nice to know how to list the jobs on each point.
My Dput:
structure(list(Year = 2006:2021, Month_USD = c(1160L, 1240L,
1360L, 1480L, 1320L, 1320L, 375L, 1600L, 2000L, 2000L, 1600L,
2240L, 1900L, 2300L, 2900L, 2300L), Degree = c("High School",
"High School", "High School", "High School", "High School", "High School",
"High School", "High School", "High School", "BA", "BA", "BA",
"BA", "BA", "M.Ed", "M.Ed"), Country = c("USA", "USA", "USA",
"USA", "USA", "USA", "DE", "USA", "USA", "USA", "USA", "USA",
"PRC", "PRC", "PRC", "HK"), Job = c("Disher", "Prep", "Prep",
"Prep", "Prep", "Prep", "Au Pair", "CSA", "Valet", "Valet", "Intake",
"CM", "Teacher", "Teacher", "Teacher", "Student"), Median_Household_Income_US = c(4833L,
4961L, 4784L, 4750L, 4626L, 4556L, 4547L, 4706L, 4634L, 4873L,
5025L, 5218L, 5360L, 5725L, NA, NA), US_Home_Price_Index = c(183.24,
173.36, 152.56, 146.69, 140.64, 135.16, 143.88, 159.3, 166.5,
175.17, 184.51, 195.99, 204.9, 212.59, 236.31, NA)), class = "data.frame", row.names = c(NA,
-16L))
Current Scatterplot:
pal <- c("Red", "Blue", "Green")
plot_ly(data = Earnings_Year,
x=~Year,
y=~Month_USD,
type='scatter',
mode='markers',
symbol = ~as.factor(Degree),
symbols=c("star-open-dot","hexagon-open-dot","diamond-open-dot"),
color = ~as.factor(Degree),
colors = pal,
hoverinfo="text",
text= paste("Year: ",
Earnings_Year$Year,
"<br>", #this is a line break
"Monthly USD: ",
Earnings_Year$Month_USD),
size=10) %>%
add_annotations(
x=Earnings_Year$Year[which.min(Earnings_Year$Month_USD)],
y=Earnings_Year$Month_USD[which.min(Earnings_Year$Month_USD)],
text = "Au Pair Job in Germany") %>%
add_annotations(
x=Earnings_Year$Year[which.max(Earnings_Year$Month_USD)],
y=Earnings_Year$Month_USD[which.max(Earnings_Year$Month_USD)],
text = "Last Teaching Job in China") %>%
layout(legend= list(x=1,y=0.5),
title="Earnings by Degree",
xaxis=list(title="Year"),
yaxis=list(title="Monthly USD"))
Image of Current Scatter:
Scatter That I Want:
Figured it out. Just needed to pipe additional add_annotations as well as just select specific values for x and y:
pal <- c("Red", "Blue", "Green")
plot_ly(data = Earnings_Year,
x=~Year,
y=~Month_USD,
type='scatter',
mode='markers',
symbol = ~as.factor(Degree),
symbols=c("star-open-dot","hexagon-open-dot","diamond-open-dot"),
color = ~as.factor(Degree),
colors = pal,
hoverinfo="text",
text= paste("Year: ",
Earnings_Year$Year,
"<br>", #this is a line break
"Monthly USD: ",
Earnings_Year$Month_USD),
size=10) %>%
add_annotations(
x=Earnings_Year$Year[which.min(Earnings_Year$Month_USD)],
y=Earnings_Year$Month_USD[which.min(Earnings_Year$Month_USD)],
text = "Au Pair Job in Germany") %>%
add_annotations(
x=Earnings_Year$Year[which.max(Earnings_Year$Month_USD)],
y=Earnings_Year$Month_USD[which.max(Earnings_Year$Month_USD)],
text = "Last Teaching Job in China") %>%
add_annotations(
x=Earnings_Year$Year[Earnings_Year$Year==2006],
y=Earnings_Year$Month_USD[Earnings_Year$Month_USD==1160],
text="First Job"
) %>%
add_annotations(
x=Earnings_Year$Year[Earnings_Year$Year==2021],
y=Earnings_Year$Month_USD[Earnings_Year$Month_USD==2300],
text="Began Ph.D.") %>%
add_annotations(
x=Earnings_Year$Year[Earnings_Year$Year==2008],
y=Earnings_Year$Month_USD[Earnings_Year$Month_USD==1360],
text="Finished H.S.") %>%
add_annotations(
x=Earnings_Year$Year[Earnings_Year$Year==2015],
y=Earnings_Year$Month_USD[Earnings_Year$Month_USD==2000],
text="Finished BA") %>%
layout(legend= list(x=1,y=0.5),
title="Earnings by Degree",
xaxis=list(title="Year"),
yaxis=list(title="Monthly USD"))
Finished Product:

How to group a legend or get seperate legends by facets in ggplot2

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)

ggmap with value showing on the countries

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

Resources