How to bring a chord diagram to a similar form? - r

I have a database.
I want to build a chord diagram similar to this one:
https://i.stack.imgur.com/59JcJ.png
My code:
vertices <- data.frame(name = unique(c(as.character(imports$Partner), as.character(imports$Reporter))) )
mygraph <- graph_from_data_frame( imports, vertices=vertices )
from <- match( imports$Reporter, vertices$name)
to <- match( imports$Partner, vertices$name)
ggraph(mygraph, layout = 'dendrogram', circular = TRUE)
geom_conn_bundle(data = get_con(from = from, to = to), alpha=0.2, colour="skyblue", tension = 0)
geom_node_point(aes(filter = leaf, x = x*1.05, y=y*1.05))
theme_void()
Result:
https://i.stack.imgur.com/uY2Yq.png
I searched for all kinds of settings for the chord diagram, but I didn’t find, how to make the links of the same size, and set the number of lines as an indicator of the value. Does anyone know how to create such a diagram?
data=structure(list(Reporter = c("USA", "USA", "USA", "USA", "India",
"Japan", "Japan", "USA", "Rep. of Korea", "USA", "Japan", "Japan",
"Japan", "Rep. of Korea", "USA", "USA", "USA", "China", "USA",
"USA", "Rep. of Korea", "USA", "Japan", "Japan", "Rep. of Korea",
"China", "China", "Rep. of Korea", "India", "China", "Rep. of Korea",
"USA", "Rep. of Korea", "Japan", "China", "Rep. of Korea", "India",
"China", "China", "India", "China", "China"), Partner = c("Saudi Arabia", "Canada", "Venezuela", "Mexico",
"Areas, nes", "Saudi Arabia", "United Arab Emirates", "Nigeria",
"Saudi Arabia", "Iraq", "Iran", "Qatar", "Kuwait", "United Arab Emirates",
"Angola", "Norway", "Colombia", "Oman", "United Kingdom", "Kuwait",
"Iran", "Gabon", "Indonesia", "Oman", "Kuwait", "Angola", "Iran",
"Oman", "Saudi Arabia", "Saudi Arabia", "Qatar", "Ecuador", "Indonesia",
"China", "Indonesia", "Australia", "Nigeria", "Yemen", "Fmr Sudan",
"Kuwait", "Iraq", "Viet Nam", "Iraq", "Australia", "Angola",
"United Arab Emirates", "Argentina", "Iran", "Trinidad and Tobago",
"Congo", "Yemen", "Iraq", "Viet Nam", "Australia", "Malaysia",
"Mexico", "Indonesia", "China", "Congo", "Ecuador", "Malaysia",
"Qatar", "Brunei Darussalam", "Norway"), Qty = c(69785202126, 68349221243, 68326932683,
64923669168, 57159000064, 53691639675, 52396394737, 46817696134,
38307387772, 31471382247, 25554794183, 19184268129, 18481591406,
16695296617, 16497467586, 16029110463, 15953011573, 15660839936,
14459452736, 13796910873, 11134838478, 10393629031, 10258716565,
9751327665, 9417368771, 8636634112, 7000465408, 6586187350, 5769723904,
5730211328, 5702528697, 5553458497, 5290777764, 5113191253, 4575188480,
4361612670, 3888963072, 3612423424, 3313590784, 3223781888, 3183182080,
3158472192, 3151280715, 3081015515, 3067260000, 2921931008, 2850134892,
2607684096, 2587749446, 2547349198, 2485083122, 2443798762, 2365431992,
2342513214, 2308853961, 2130664704, 1942125162, 1828376381, 1814260579,
1785874000, 1609282280, 1598901888, 1534923974, 1477843712, 1476737920,
1454356736, 1355873401, 1293729024, 1285355978, 1278701346, 1259876360,
1252518912, 1248772992, 1223383808, 1163368000, 1144188000, 1108399232,
1062041363, 1041526592, 977722731, 897418483, 877541040, 845556546,
801940467, 744316800, 739848000, 724177472, 694896000, 685405539,
672387008, 554965585, 540327751, 508204324, 4.87e+08, 457252032,
433428000, 430473920, 426744352, 408635880, 392727578, 390598528,
390189912, 389451923, 384376548, 350920922, 327039700, 285413702,
285143680, 275486240, 274015471, 264478000, 260122000, 238997756,
227806048, 204376795, 192144011, 150791409, 140634221, 135842986,
130777039, 129973032, 125115000, 124681401, 123443000, 120061792,
110795499, 106762492, 105548008, 84693986, 70275359, 57248174,
47944463, 40236018, 30783728, 18364000, 13419253, 12551365, 9631763,
5994199, 374000, 350000, 339115, 86420, 24000, 180)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -145L))

Related

Label plot based on condition [duplicate]

This question already has an answer here:
Conditional labeling in ggplot2 using geom_text and subsetting
(1 answer)
Closed 1 year ago.
I have a dot plot with data from dataframe df_roles$result and would like to assign labels only to certain dots specified by their specific name in df_roles$name.
My expected output is to have e.g. China labelled.
I have tried both these arguments but they won't work:
ifelse(df_roles$name = "China",
text(df_roles[[3]], df_roles[[2]], labels = df_roles[[1]], pos = 2, cex=0.3), " ")
geom_text(data = filter(df_roles$result, df_roles$name=="China"),aes(label=df_roles$name))
Reproducible example:
df_roles <- structure(list(name = structure(1:6, .Label = c("Afghanistan",
"Albania", "Algeria", "Angola", "Argentina", "Armenia", "Australia",
"Austria", "Azerbaijan", "Bahrain", "Bangladesh", "Barbados",
"Belarus", "Belgium", "Belize", "Benin", "Bolivia", "Bosnia and Herzegovina",
"Botswana", "Brazil", "Bulgaria", "Burkina Faso", "Burundi",
"Cameroon", "Canada", "Chile", "China", "Colombia", "Congo",
"Costa Rica", "Croatia", "Cuba", "Cyprus", "Democratic Republic of the Congo",
"Denmark", "Dominican Republic", "Ecuador", "Egypt", "El Salvador",
"Eritrea", "Estonia", "Ethiopia", "Finland", "France", "Gabon",
"Gambia", "Georgia", "Germany", "Ghana", "Greece", "Grenada",
"Guatemala", "Guinea", "Guyana", "Haiti", "Honduras", "Hungary",
"India", "Indonesia", "Iran ", "Iraq", "Ireland", "Israel", "Italy",
"Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kuwait",
"Kyrgyzstan", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya",
"Lithuania", "Luxembourg", "Malawi", "Malaysia", "Mali", "Malta",
"Mauritania", "Mexico", "Mongolia", "Montenegro", "Morocco",
"Mozambique", "Namibia", "Nepal", "Netherlands", "New Zealand",
"Nicaragua", "Nigeria", "Norway", "Oman", "Pakistan", "Palestine",
"Panama", "Paraguay", "Peru", "Philippines", "Poland", "Portugal",
"Qatar", "Republic of Korea", "Republic of Moldova", "Romania",
"Russia", "Rwanda", "Saint Lucia", "Saint Vincent and the Grenadines",
"Saudi Arabia", "Senegal", "Serbia", "Singapore", "Slovakia",
"Slovenia", "Somalia", "South Africa", "Spain", "Sri Lanka",
"Sudan", "Suriname", "Sweden", "Switzerland", "Syria", "Taiwan",
"Tajikistan", "Thailand", "Togo", "Trinidad and Tobago", "Tunisia",
"Turkey", "Turkmenistan", "Uganda", "UK", "Ukraine", "United Arab Emirates",
"United Republic of Tanzania", "United States of America", "Uruguay",
"Uzbekistan", "Venezuela", "Viet Nam", "Yemen", "Zambia", "Zimbabwe"
), class = "factor"), result = c(0.0900102874778869, -0.0934265332577318,
-0.177974826946747, -0.590024694573266, 1.20884852383168, -0.183452483887309
)), row.names = c(NA, 6L), class = "data.frame")
Since the data shared does not have 'China' in it I am using 'Afghanistan' as an example. Also using row number as x-axis.
library(dplyr)
library(ggplot2)
df_roles %>%
mutate(name = as.character(name),
label = replace(name, name != 'Afghanistan', ''),
row = row_number()) %>%
ggplot(aes(row, result, label = label)) +
geom_point() + geom_text(vjust = -0.5)

R - Degree calculation based module conditions

I have a dataframe where would like to calculate the
degree of a country (node) - degree of its module / standard deviation of its module.
I have calculated degree and sd of modules by
dgclust <- aggregate(clust[, 2], list(clust$modularity_class), mean)
sdclust <- aggregate(clust[, 2], list(clust$modularity_class), sd)
But I'm not sure how I can write the calculation above so that the code links dgclust and sdclust to the specific module the country belongs to? My expected output is a value for each country based on the described calculation. Any help appreciated!
Reproducible example
clust <- dput(head(clust[1:10, c(1, 5,6)]))
structure(list(Label = structure(1:6, .Label = c("Afghanistan",
"Albania", "Algeria", "Angola", "Antigua and Barbuda", "Argentina",
"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain",
"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
"Bhutan", "Bolivia (Plurinational State of)", "Bosnia and Herzegovina",
"Botswana", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso",
"Burundi", "C?te d'Ivoire", "Cambodia", "Cameroon", "Canada",
"Central African Republic", "Chile", "China", "China, Hong Kong SAR",
"China, Macao SAR", "China, Taiwan Province of", "Colombia",
"Congo", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czechia",
"Democratic People's Republic of Korea", "Democratic Republic of the Congo",
"Denmark", "Dominican Republic", "Ecuador", "Egypt", "El Salvador",
"Eritrea", "Estonia", "Eswatini", "Ethiopia", "Finland", "France",
"Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Grenada",
"Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras",
"Hungary", "India", "Indonesia", "Iran (Islamic Republic of)",
"Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan",
"Kazakhstan", "Kenya", "Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic",
"Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Lithuania",
"Luxembourg", "Madagascar", "Malawi", "Malaysia", "Mali", "Malta",
"Mauritania", "Mexico", "Mongolia", "Montenegro", "Morocco",
"Mozambique", "Myanmar", "Namibia", "Nepal", "Netherlands", "New Zealand",
"Nicaragua", "Niger", "Nigeria", "North Macedonia", "Norway",
"Oman", "Pakistan", "Palestine", "Panama", "Papua New Guinea",
"Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar",
"Republic of Korea", "Republic of Moldova", "Romania", "Russian Federation",
"Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines",
"Saudi Arabia", "Senegal", "Serbia", "Sierra Leone", "Singapore",
"Slovakia", "Slovenia", "Somalia", "South Africa", "Spain", "Sri Lanka",
"Sudan", "Suriname", "Sweden", "Switzerland", "Syrian Arab Republic",
"Tajikistan", "Thailand", "Timor-Leste", "Trinidad and Tobago",
"Tunisia", "Turkey", "Turkmenistan", "Uganda", "Ukraine", "United Arab Emirates",
"United Kingdom of Great Britain and Northern Ireland", "United Republic of Tanzania",
"United States of America", "Uruguay", "Uzbekistan", "Venezuela (Bolivarian Republic of)",
"Viet Nam", "Yemen", "Zambia", "Zimbabwe"), class = "factor"),
Degree = c(5L, 14L, 14L, 8L, 1L, 119L), modularity_class = c(3L,
4L, 2L, 2L, 1L, 2L)), row.names = c("Afghanistan", "Albania",
"Algeria", "Angola", "Antigua and Barbuda", "Argentina"), class = "data.frame")
In base R, we can use ave to calculate (Degree - mean)/sd for each modularity_class.
clust <- transform(clust, result = ave(Degree, modularity_class,
FUN = function(x) (x - mean(x, na.rm = TRUE))/sd(x, na.rm = TRUE)))
This can be written in dplyr and data.table as well -
library(dplyr)
clust <- clust %>%
group_by(modularity_class) %>%
mutate(result = (Degree - mean(Degree, na.rm = TRUE))/sd(Degree, na.rm = TRUE))
library(data.table)
setDT(clust)[, result := (Degree - mean(Degree, na.rm = TRUE))/sd(Degree, na.rm = TRUE), modularity_class]

Separating countries into tropical versus non-tropical R

I have a data frame where one of the column variable is the country. As an example see the vector with the countries.
country=c("Argentina", "Bahamas", "Barbados", "Belize", "Bolivia", "Brazil", "Virgin Islands", "Chile", "Colombia", "Costa Rica", "Cuba", "Dominica", "Dominican Republic", "Ecuador", "El Salvador", "French Guiana", "Guadeloupe", "Guatemala", "Guyana", "Haiti", "Honduras", "Jamaica", "Martinique", "Mexico", "Nicaragua", "Panama", "Paraguay", "Peru","St Lucia", "St Vincent", "Suriname", "Trinidad and Tobago", "Uruguay", "Venezuela", "Bangladesh", "Bhutan", "Brunei", "Cambodia", "India", "Indonesia", "Laos", "Malaysia", "Myanmar", "Nepal", "Pakistan", "Philippines", "Papua New Guinea", "Singapore", "SriLanka", "Thailand", "TimorLeste", "Vietnam", "Angola", "Benin", "Botswana", "BurkinaFaso", "Burundi", "Cameroon", "Central African Republic", "Chad", "Congo","Djibouti", "Democratic Republic of the Congo", "Equatorial Guinea", "Eritrea", "Ethiopia", "Gabon", "Gambia", "Ghana", "Guinea","Guinea Bissau", "Ivory Coast", "Kenya", "Lesotho", "Liberia", "Madagascar", "Malawi", "Mauritania", "Mali", "Mozambique", "Namibia","Niger", "Nigeria", "Rwanda", "Sudan", "Senegal", "Sierra Leone", "Somalia", "South Africa", "South Sudan", "Swaziland", "Tanzania", "Togo", "Uganda", "Zambia", "Zimbabwe", "Canada", "United States of America", "Albania", "Andorra", "Austria", "Belgium", "Bosnia", "Bulgaria", "Croatia", "Czech Republic", "Denmark", "Finland", "France", "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Italy", "Liechtenstein","Luxembourg", "Macedonia", "Malta", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Serbia", "Slovakia","Slovenia", "Spain", "Sweden", "Switzerland", "United Kingdom", "Afghanistan", "Algeria", "Bahrain", "Cyprus", "Egypt", "Iran","Iraq", "Israel", "Jordan", "Kuwait", "Lebanon", "Libya", "Morocco", "Oman", "Qatar", "Saudi Arabia", "Syria", "Tunisia", "Turkey","United Arab Emirates", "Western Sahara", "Yemen", "Armenia", "Azerbaijan", "Belarus", "Estonia", "Georgia", "Kazakhstan", "Kyrgyzstan", "Latvia", "Lithuania", "Moldova", "Russia", "Tajikistan", "Turkmenistan", "Ukraine", "Uzbekistan", "Japan", "Mongolia", "North Korea", "South Korea","China", "Australia", "Cook Islands", "Fiji", "French Polynesia", "Micronesia", "New Caledonia", "New Zealand", "Niue", "Samoa", "Solomon Islands", "Tonga", "Vanuatu")
I would like to separate these countries into tropical versus non-tropical (i.e. those within the tropical region and those that are outside. Does anyone have an idea of how can do this in R?
As #Sirius mentioned in the comments, there are lists like the following:
The csvData.csv can be downloaded here:
https://worldpopulationreview.com/country-rankings/tropical-countries
tropical_data <- read.csv("csvData.csv")
# tropical country (71)
country[country %in% tropical_data$country]
# non tropical country (115)
country[!country %in% tropical_data$country]

Need to do a barplot with large number of xlab values. How do I make them fit? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
This is the code that I have input:
CountriesChart = data.frame(NamesofC =
factor(c(Countries),
levels = c("Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Central African Republic", "Chad", "China", "Colombia", "Comoros", "Congo", "Costa Rica", "Cote d'lvoire", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Federated States of Micronesia", "Fiji", "Finland", "France", "Gabon", "Georgia", "Germany", "Ghana", "Greece", "Greenland", "Grenada", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Lithuania", "Luxembourg", "Macedonia", "Madagascar", "Malawi", "Malaysia", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Moldova", "Mongolia", "Montenegro", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nepal", "Netherlands", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Northern Mariana Islands", "North Korea", "Norway", "Oman", "Pakistan", "Palestine", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Phillipines", "Poland", "Portugal", "Puerto Rico", "Qatar", "Romana", "Russia", "Rwanda", "Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Solomon Islands", "Somalia", "South Africa", "South Korea", "South Sudan", "Spain", "Sri Lanka", "Sudan", "Suriname", "Swaziland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas", "The Gambia", "Timor-Leste", "Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Virgin Islands, US.", "Yemen", "Zambia", "Zimbabwe")),
Values = c(OneData$exp_mean))
barplot(CountriesChart$Values, names = c(CountriesChart$levels))
I need to make this into a graph that shows the countries' names, but it won't show up because there are so many of them. What can I do to fix this? This is the end result of the graph with this code:
barplot(CountriesChart$Values, names = c(CountriesChart$levels), las=2)

Ploting Countries in R Maps Library : Gray boundaries with FILL=TRUE

I am trying to plot countries in R Maps. However, when I use FILL=TRUE all my countries boundaries are drawn in black. I want them in Gray. This is my code:
library(maps) # Provides functions that let us plot the maps
library(mapdata) # Contains the hi-resolution points that mark out the Cnt
countries=c("Argentina","Armenia","Australia","Bahrain","Belgium","Botswana","Bulgaria","Canada", "Chile","Tawain", "Croatia","Cyprus", "Czech Republic", "Denmark","Egypt","UK:Great Britain","Finland", "France", "Georgia", "Germany", "China:Hong Kong", "Hungary", "Indonesia", "Iran", "Ireland", "Israel", "Italy", "Japan", "Jordan", "Kazakhstan", "Korea", "Kuwait", "Lebanon", "Lithuania", "Malaysia", "Malta", "Morocco", "Netherlands", "New Zealand", "UK:Northern Ireland", "Norway", "Oman", "Palestine", "Poland", "Portugal", "Qatar", "Russia", "Saudi Arabia", "Serbia", "Singapore", "Slovak Republic", "Slovenia", "South Africa", "Spain", "Sweden", "Thailand", "Turkey", "United Arab Emirates", "USA")
map('world', resolution=1, col="darkgray")
map('world', countries, resolution=1, fill = T, col = "royalblue", add = T)
map('world', resolution=1,col="darkgray", add=TRUE)
Any ideas will be appreciated.
Thanks
Jº
To change the colour of the boundaries, add the argument border='darkgrey'. This argument is not listed explicitely in the man page for 'map' because it is in fact an argument for the call to 'polygon'. It is mentioned in the '...' segment of the man page, though.
map('world', countries, resolution=1, fill = T,
col = "royalblue", border="darkgrey", add = T)

Resources