VisNetwork in R: Adjust length of edges - r

Is there a way to adjust the length of the edges in VisNetwork so that they are overall a bit longer? Some arrows are a bit hard to see right now especially with the thicker edges.
This is my edge list and nodes list
nodes_t <- structure(list(ahaid = c("6140002", "6140005", "6140007", "6140008",
"6140010", "6140011", "6140012", "6140013", "6140016", "6140017",
"6140065", "6140080", "6140090", "6140215", "6140255", "6140270",
"6140310", "6140420", "6140428", "6140430", "6140465", "6140583",
"6140620", "6140630", "6140690", "6140780", "6140850", "6140900",
"6140923", "6140980", "6141010", "6141020", "6141095", "6141110",
"6141130", "6141170", "6141300", "6141355", "6141395", "6141410",
"6141430", "6141450", "6141500", "6141530", "6141570", "6141630",
"6141640", "6141660", "6141705", "6141720", "6141890", "6141900",
"6141940", "6141955", "6142000", "6142200", "6142280", "6142350"
), weight = c(129L, 67L, 72L, 111L, 193L, 56L, 32L, 601L, 406L,
151L, 19L, 56L, 25L, 1909L, 22L, 6L, 38L, 6L, 264L, 1416L, 169L,
133L, 98L, 87L, 387L, 53L, 63L, 101L, 91L, 32L, 11L, 55L, 456L,
34L, 225L, 62L, 31L, 158L, 185L, 24L, 6L, 43L, 47L, 112L, 93L,
94L, 12L, 46L, 175L, 161L, 30L, 452L, 234L, 379L, 86L, 19L, 85L,
135L)), row.names = c(NA, -58L), class = "data.frame")
edges_t <- structure(list(from = c("6140002", "6140008", "6140010", "6140012",
"6140013", "6140013", "6140013", "6140016", "6140017", "6140080",
"6140215", "6140215", "6140215", "6140215", "6140215", "6140215",
"6140215", "6140215", "6140215", "6140215", "6140215", "6140215",
"6140428", "6140430", "6140430", "6140430", "6140923", "6141020",
"6141095", "6141095", "6141170", "6141660", "6141940"), to = c("6140430",
"6141095", "6140215", "6140430", "6140630", "6141450", "6141720",
"6140215", "6140690", "6140215", "6140310", "6140428", "6140430",
"6140583", "6140690", "6141095", "6141130", "6141395", "6141530",
"6141630", "6141900", "6142000", "6140690", "6140850", "6141530",
"6141900", "6141130", "6141955", "6141300", "6142350", "6141355",
"6141955", "6141955"), weight = c(19L, 22L, 7L, 8L, 9L, 17L,
9L, 15L, 19L, 12L, 22L, 7L, 27L, 90L, 14L, 20L, 13L, 32L, 9L,
11L, 34L, 8L, 7L, 11L, 17L, 16L, 6L, 16L, 7L, 5L, 10L, 17L, 18L
)), row.names = c(NA, -33L), class = "data.frame")
This is my code
vis.nodes <- nodes_t
vis.links <- edges_t
vis.nodes$borderWidth <- 2
vis.nodes <- vis.nodes %>% mutate(size = weight/16)
vis.links$width <- 1+edges_t$weight/4
vis.links$arrows <- "to"
library('visNetwork')
p <- visNetwork(vis.nodes, vis.links, width="400%", height="1600px")

You could use the visEdges function with argument length to change the length of the edges. Since I can't reproduce your plot, I created a reproducible example:
library(visNetwork)
library(dplyr)
# Data
nodes <- data.frame(id = 1:2)
edges <- data.frame(from = 1, to = 2)
# Plot with length 100
visNetwork(nodes, edges) %>%
visEdges(arrows = 'to', length = 100)
# Plot with length 400
visNetwork(nodes, edges) %>%
visEdges(arrows = 'to', length = 400)
Created on 2022-12-18 with reprex v2.0.2
As you can see the first plot has an edge length of 100 pixels and the second plot has an edge length of 400 pixels.
If you want to have variable lengths, you could add an variable length to your edge dataframe like this:
library(visNetwork)
library(dplyr)
# Data
nodes <- data.frame(id = 1:2)
edges <- data.frame(from = 1, to = 2, length = c(100, 400))
# Plot
visNetwork(nodes, edges) %>%
visEdges(arrows = "to")
Created on 2022-12-18 with reprex v2.0.2

Related

Reordering a bar chart to be an ascending manner

I have a bar chart presenting the number of samples, with the number of responders and non-responders in multiple datasets. I want to order it according to the number of samples in each dataset, in an ascending manner (starting with lowest samples number and going to the top).
the code:
myData <- data.frame(Articles, Samples_number, Response, No_Response)
library(ggplot2)
myData |>
tidyr::pivot_longer(c(Response, No_Response)) |>
ggplot(aes(Articles, value, fill = name)) +
geom_col(position = position_stack()) +
theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust=1))
the plot:
The dataframe that contains all the information, myData :
structure(list(Articles = c("Allen.SKCM", "Auslander.SKCM", "Braun.KIRC",
"Cho.NSCLC", "Freeman.SKCM (MGH)", "He.THCA", "Hoffman.BLCA (IMvigor210)",
"Hugo.SKCM", "Liu.SKCM", "Lozano.SKCM", "McDermott.KIRC (IMmotion150)",
"Motzer.KIRC", "Newell.SKCM", "Pender.PAN", "Riaz.SKCM", "Rizos.SKCM",
"Snyder.BLCA"), Samples_number = c(49L, 181L, 44L, 14L, 354L,
165L, 298L, 47L, 39L, 25L, 21L, 53L, 9L, 82L, 165L, 119L, 16L
), Response = c(10L, 57L, 22L, 2L, 197L, 48L, 68L, 27L, 7L, 13L,
7L, 34L, 4L, 17L, 14L, 47L, 4L), No_Response = c(39L, 124L, 22L,
12L, 157L, 117L, 230L, 20L, 32L, 12L, 14L, 19L, 5L, 65L, 24L,
72L, 12L)), class = "data.frame", row.names = c(NA, -17L))
You can use the following code:
myData <- structure(list(Articles = c("Allen.SKCM", "Auslander.SKCM", "Braun.KIRC",
"Cho.NSCLC", "Freeman.SKCM (MGH)", "He.THCA", "Hoffman.BLCA (IMvigor210)",
"Hugo.SKCM", "Liu.SKCM", "Lozano.SKCM", "McDermott.KIRC (IMmotion150)",
"Motzer.KIRC", "Newell.SKCM", "Pender.PAN", "Riaz.SKCM", "Rizos.SKCM",
"Snyder.BLCA"), Samples_number = c(49L, 181L, 44L, 14L, 354L,
165L, 298L, 47L, 39L, 25L, 21L, 53L, 9L, 82L, 165L, 119L, 16L
), Response = c(10L, 57L, 22L, 2L, 197L, 48L, 68L, 27L, 7L, 13L,
7L, 34L, 4L, 17L, 14L, 47L, 4L), No_Response = c(39L, 124L, 22L,
12L, 157L, 117L, 230L, 20L, 32L, 12L, 14L, 19L, 5L, 65L, 24L,
72L, 12L)), class = "data.frame", row.names = c(NA, -17L))
library(ggplot2)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidyr)
myData %>%
pivot_longer(c(Response, No_Response)) %>%
group_by(Articles) %>%
mutate(total = sum(value)) %>%
ggplot(aes(x = Articles, y = value, fill = name)) +
geom_col(aes(x = reorder(Articles, total, sum), y = value), position = position_stack()) +
theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust=1))
Created on 2022-08-28 with reprex v2.0.2
use reorder function:
library(ggplot2)
myData |>
tidyr::pivot_longer(c(Response, No_Response)) |>
ggplot(aes(reorder(Articles, value), value, fill = name)) +
geom_col(position = position_stack()) +
theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust=1))

Loop with multiple subset of data frame

I have a data.frame fish.test0 for which I want to grep specific variables (in varlist) matching the group column to create a sub-data.frame that will undergo a statistical test. The results of the test is saved in tests.res.t. I want to loop the varlist so that I get one results for each input in varlist
Script:
varlist <- c("Abiotrophia","Alphatorquevirus")
for (i in varlist) {
fish.test <- fish.test0[grep("i",fish.test0$group),]
column <- c("ACDC")
tests <- list()
dat_test <- sapply( column, function(colx)
lapply( unique(fish.test$Merge), function(x)
fisher.test( data.frame(
a=c(( fish.test[ which(fish.test$Merge %in% x)[2],"Present"] -
fish.test[ which(fish.test$Merge %in% x)[2], colx] ),fish.test[ which(fish.test$Merge %in% x)[2], colx]
),
b=c(( fish.test[ which(fish.test$Merge %in% x)[1],"NotPresent"] -
fish.test[ which(fish.test$Merge %in% x)[1], colx] ), fish.test[ which(fish.test$Merge %in% x)[1], colx]))) #,alternative = "greater"
) )
rownames(dat_test) <- unique(fish.test$Merge )
colnames(dat_test) <- column
tests.res <- sapply(dat_test[1:dim(dat_test)[1],1], function(x) {
c(x$estimate[1],
x$estimate[2],
ci.lower = x$conf.int[1],
ci.upper = x$conf.int[2],
p.value = x$p.value)
})
tests.res.t <- as.data.frame(t(tests.res))
}
test-data:
fish.test0 <- structure(list(Present = c(4L, 4L, 9L, 9L, 57L, 57L, 146L, 146L,
91L, 91L, 26L, 26L, 6L, 6L, 12L, 12L, 33L, 33L, 10L, 10L, 66L,
66L, 4L, 4L, 4L, 4L, 9L, 9L, 18L, 18L, 19L, 19L, 51L, 51L, 50L,
50L, 12L, 12L, 7L, 7L, 14L, 14L, 27L, 27L, 9L, 9L, 5L, 5L, 6L,
6L, 22L, 22L, 3L, 3L, 14L, 14L, 4L, 4L, 15L, 15L, 6L, 6L, 8L,
8L, 4L, 4L), NotPresent = c(11L, 11L, 44L, 44L, 126L, 126L, 532L,
532L, 382L, 382L, 97L, 97L, 14L, 14L, 43L, 43L, 85L, 85L, 41L,
41L, 336L, 336L, 19L, 19L, 27L, 27L, 67L, 67L, 108L, 108L, 81L,
81L, 240L, 240L, 258L, 258L, 47L, 47L, 31L, 31L, 82L, 82L, 110L,
110L, 63L, 63L, 178L, 178L, 672L, 672L, 451L, 451L, 120L, 120L,
104L, 104L, 47L, 47L, 387L, 387L, 94L, 94L, 300L, 300L, 133L,
133L), group = c("G__Abiotrophia_NotPresent_Anus", "G__Abiotrophia_Present_Anus",
"G__Abiotrophia_NotPresent_Bile duct", "G__Abiotrophia_Present_Bile duct",
"G__Abiotrophia_NotPresent_Bone/Soft tissue", "G__Abiotrophia_Present_Bone/Soft tissue",
"G__Abiotrophia_NotPresent_Breast", "G__Abiotrophia_Present_Breast",
"G__Abiotrophia_NotPresent_Colorectum", "G__Abiotrophia_Present_Colorectum",
"G__Abiotrophia_NotPresent_Esophagus", "G__Abiotrophia_Present_Esophagus",
"G__Abiotrophia_NotPresent_Gallbladder", "G__Abiotrophia_Present_Gallbladder",
"G__Abiotrophia_NotPresent_Head and neck", "G__Abiotrophia_Present_Head and neck",
"G__Abiotrophia_NotPresent_Kidney", "G__Abiotrophia_Present_Kidney",
"G__Abiotrophia_NotPresent_Liver", "G__Abiotrophia_Present_Liver",
"G__Abiotrophia_NotPresent_Lung", "G__Abiotrophia_Present_Lung",
"G__Abiotrophia_NotPresent_Lymphoid tissue", "G__Abiotrophia_Present_Lymphoid tissue",
"G__Abiotrophia_NotPresent_Mesothelium", "G__Abiotrophia_Present_Mesothelium",
"G__Abiotrophia_NotPresent_Nervous system", "G__Abiotrophia_Present_Nervous system",
"G__Abiotrophia_NotPresent_Ovary", "G__Abiotrophia_Present_Ovary",
"G__Abiotrophia_NotPresent_Pancreas", "G__Abiotrophia_Present_Pancreas",
"G__Abiotrophia_NotPresent_Prostate", "G__Abiotrophia_Present_Prostate",
"G__Abiotrophia_NotPresent_Skin", "G__Abiotrophia_Present_Skin",
"G__Abiotrophia_NotPresent_Small intestine", "G__Abiotrophia_Present_Small intestine",
"G__Abiotrophia_NotPresent_Stomach", "G__Abiotrophia_Present_Stomach",
"G__Abiotrophia_NotPresent_Unknown", "G__Abiotrophia_Present_Unknown",
"G__Abiotrophia_NotPresent_Urothelial tract", "G__Abiotrophia_Present_Urothelial tract",
"G__Abiotrophia_NotPresent_Uterus", "G__Abiotrophia_Present_Uterus",
"G__Alphatorquevirus_NotPresent_Bone/Soft tissue", "G__Alphatorquevirus_Present_Bone/Soft tissue",
"G__Alphatorquevirus_NotPresent_Breast", "G__Alphatorquevirus_Present_Breast",
"G__Alphatorquevirus_NotPresent_Colorectum", "G__Alphatorquevirus_Present_Colorectum",
"G__Alphatorquevirus_NotPresent_Esophagus", "G__Alphatorquevirus_Present_Esophagus",
"G__Alphatorquevirus_NotPresent_Kidney", "G__Alphatorquevirus_Present_Kidney",
"G__Alphatorquevirus_NotPresent_Liver", "G__Alphatorquevirus_Present_Liver",
"G__Alphatorquevirus_NotPresent_Lung", "G__Alphatorquevirus_Present_Lung",
"G__Alphatorquevirus_NotPresent_Pancreas", "G__Alphatorquevirus_Present_Pancreas",
"G__Alphatorquevirus_NotPresent_Skin", "G__Alphatorquevirus_Present_Skin",
"G__Alphatorquevirus_NotPresent_Urothelial tract", "G__Alphatorquevirus_Present_Urothelial tract"
), ABCD = c(3L, 2L, 17L, 6L, 34L, 18L, 240L, 53L, 321L, 73L,
87L, 25L, 6L, 3L, 20L, 8L, 15L, 7L, 19L, 4L, 265L, 42L, 6L, 1L,
4L, 2L, 22L, 4L, 70L, 13L, 54L, 12L, 116L, 33L, 58L, 11L, 6L,
2L, 26L, 6L, 42L, 8L, 74L, 18L, 19L, 3L, 52L, 0L, 288L, 5L, 377L,
17L, 110L, 2L, 19L, 3L, 21L, 2L, 298L, 9L, 60L, 6L, 68L, 1L,
89L, 3L), Total = c(15L, 15L, 53L, 53L, 183L, 183L, 678L, 678L,
473L, 473L, 123L, 123L, 20L, 20L, 55L, 55L, 118L, 118L, 51L,
51L, 402L, 402L, 23L, 23L, 31L, 31L, 76L, 76L, 126L, 126L, 100L,
100L, 291L, 291L, 308L, 308L, 59L, 59L, 38L, 38L, 96L, 96L, 137L,
137L, 72L, 72L, 183L, 183L, 678L, 678L, 473L, 473L, 123L, 123L,
118L, 118L, 51L, 51L, 402L, 402L, 100L, 100L, 308L, 308L, 137L,
137L), Merge = c("Abiotrophia_Anus", "Abiotrophia_Anus", "Abiotrophia_Bile duct",
"Abiotrophia_Bile duct", "Abiotrophia_Bone/Soft tissue", "Abiotrophia_Bone/Soft tissue",
"Abiotrophia_Breast", "Abiotrophia_Breast", "Abiotrophia_Colorectum",
"Abiotrophia_Colorectum", "Abiotrophia_Esophagus", "Abiotrophia_Esophagus",
"Abiotrophia_Gallbladder", "Abiotrophia_Gallbladder", "Abiotrophia_Head and neck",
"Abiotrophia_Head and neck", "Abiotrophia_Kidney", "Abiotrophia_Kidney",
"Abiotrophia_Liver", "Abiotrophia_Liver", "Abiotrophia_Lung",
"Abiotrophia_Lung", "Abiotrophia_Lymphoid tissue", "Abiotrophia_Lymphoid tissue",
"Abiotrophia_Mesothelium", "Abiotrophia_Mesothelium", "Abiotrophia_Nervous system",
"Abiotrophia_Nervous system", "Abiotrophia_Ovary", "Abiotrophia_Ovary",
"Abiotrophia_Pancreas", "Abiotrophia_Pancreas", "Abiotrophia_Prostate",
"Abiotrophia_Prostate", "Abiotrophia_Skin", "Abiotrophia_Skin",
"Abiotrophia_Small intestine", "Abiotrophia_Small intestine",
"Abiotrophia_Stomach", "Abiotrophia_Stomach", "Abiotrophia_Unknown",
"Abiotrophia_Unknown", "Abiotrophia_Urothelial tract", "Abiotrophia_Urothelial tract",
"Abiotrophia_Uterus", "Abiotrophia_Uterus", "Alphatorquevirus_Bone/Soft tissue",
"Alphatorquevirus_Bone/Soft tissue", "Alphatorquevirus_Breast",
"Alphatorquevirus_Breast", "Alphatorquevirus_Colorectum", "Alphatorquevirus_Colorectum",
"Alphatorquevirus_Esophagus", "Alphatorquevirus_Esophagus", "Alphatorquevirus_Kidney",
"Alphatorquevirus_Kidney", "Alphatorquevirus_Liver", "Alphatorquevirus_Liver",
"Alphatorquevirus_Lung", "Alphatorquevirus_Lung", "Alphatorquevirus_Pancreas",
"Alphatorquevirus_Pancreas", "Alphatorquevirus_Skin", "Alphatorquevirus_Skin",
"Alphatorquevirus_Urothelial tract", "Alphatorquevirus_Urothelial tract"
)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 10L, 9L, 12L,
11L, 13L, 14L, 16L, 15L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 28L, 27L, 29L, 30L, 31L, 32L, 34L, 33L, 35L, 36L, 38L,
37L, 40L, 39L, 42L, 43L, 45L, 44L, 47L, 46L, 1011L, 1012L, 1014L,
1013L, 1015L, 1016L, 1017L, 1018L, 1019L, 1020L, 1022L, 1021L,
1023L, 1024L, 1026L, 1025L, 1027L, 1028L, 1029L, 1030L), class = "data.frame")
This is probably not an answer but it should help to improve you code. If I'm terribly wrong, I'll remove my answer right away. I have loeft out the test business which I don't understand, but your problem seems to be extraction.
The first thing is that you need to remove the quotation marks in your grep command, try:
varlist <- c("Abiotrophia","Alphatorquevirus")
for( i in varlist )
{
# extract rows which contain the variable
fish.test <- fish.test0[ grep( i, fish.test0$group ), ]
print( head( fish.test ) )
}
From what I understand, you need to define column and tests outside your loop. Does that give you more of what you want:
varlist <- c("Abiotrophia","Alphatorquevirus")
column <- "ACDC"
tests <- list()
for( i in 1 : length( varlist ) ) # index can be used later to fill the list
{
# extract rows which contain the variable
fish.test <- fish.test0[ grep( varlist[ i ], fish.test0$group ), ]
# add a column with your name of choice
fish.test <- cbind( fish.test, c( 1: length( fish.test$group ) ) )
colnames( fish.test )[ length( fish.test ) ] <- column
# write each result into your defined list
tests[[ i ]] <- fish.test
}

Exact number of every value next to the bar [duplicate]

This question already has answers here:
How to put labels over geom_bar in R with ggplot2
(4 answers)
Closed 5 years ago.
Having a dataset like this:
df <- structure(list(word = structure(c(1L, 12L, 23L, 34L, 43L, 44L,
45L, 46L, 47L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L, 25L, 26L, 27L,
28L, 29L, 30L, 31L, 32L, 33L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L), .Label = c("word1", "word10", "word11", "word12", "word13",
"word14", "word15", "word16", "word17", "word18", "word19", "word2",
"word20", "word21", "word22", "word23", "word24", "word25", "word26",
"word27", "word28", "word29", "word3", "word30", "word31", "word32",
"word33", "word34", "word35", "word36", "word37", "word38", "word39",
"word4", "word40", "word41", "word42", "word43", "word44", "word45",
"word46", "word47", "word5", "word6", "word7", "word8", "word9"
), class = "factor"), frq = c(1975L, 1665L, 1655L, 1469L, 1464L,
1451L, 1353L, 1309L, 1590L, 1545L, 1557L, 1556L, 1130L, 1153L,
1151L, 1150L, 1144L, 1141L, 1115L, 194L, 195L, 135L, 135L, 130L,
163L, 167L, 164L, 159L, 153L, 145L, 143L, 133L, 133L, 153L, 153L,
150L, 119L, 115L, 115L, 115L, 114L, 113L, 113L, 113L, 115L, 102L,
101L)), .Names = c("word", "frq"), class = "data.frame", row.names = c(NA,
-47L))
With this command lines I produce a bar plot graph
dat2 = transform(df,word = reorder(word,frq))
df2 <- head(dat2, 10)
p = ggplot(df2, aes(x = word, y = frq)) + geom_bar(stat = "identity", fill = "yellow")
p2 <- p +coord_flip()
How is it possible to have the number of frq in the end of every bar?
I would use annotate..
p2 + annotate(geom = "text",x = df2$word, y= df2$frq, label = df2$frq)

How do I melt a dataframe that includes pre-computed confidence intervals for plotting?

I have the a dataframe that includes +/- confidence intervals. I assembled the dataset myself from secondary open datasets that only had +/- CIs, so there is not much that I can do. I understand that the quickest way to plot multiple series with ggplot2 is to reshape2 the dataframe, which I can easily do like
melt(df, id.vars = c("Year"))
except that that turns CI columns into proper series. Now, I'd eventually like to produce a plot like this.
which I produced with
ggplot(df, aes(x = Year)) +
geom_line(aes(y = Total.inflow), color="red") +
geom_ribbon(aes(ymin = Total.inflow-Total.inflow.CI, ymax = Total.inflow+Total.inflow.CI), colour="red", fill="red", alpha=0.1) +
geom_line(aes(y = EU.inflow), color="blue") +
geom_ribbon(aes(ymin = EU.inflow-EU.inflow.CI, ymax = EU.inflow+EU.inflow.CI), colour="blue", fill="blue", alpha=0.1) +
geom_line(aes(y = ROW.inflow), color="green") +
geom_ribbon(aes(ymin = ROW.inflow-ROW.inflow.CI, ymax = ROW.inflow+ROW.inflow.CI), colour="green", fill="green", alpha=0.1)
Ideas?
WORKING SOLUTION
Thanks to #lukeA for pointing me towards the right method. For some reason, his solution produced an empty data frame, but I managed to figure out what he was trying to do and found a reasonable solution myself.
First of all, let's separate the GDP column from the flows dataset. I suspected this was necessary from the beginning, but I was confident I could filter it out while plotting. Turns out it's just easier to separate the two. Also, I'll normalise its values for later, because tens of billions…
df <- read.csv('stats.csv', header=T)
gdp <- data.frame(Year = df$Year, GDP = df$GDP/10000000000)
df <- within(df, rm(GDP))
The goal is to get the CI values side by side with their corresponding series. This was the code inside the inner_join in #lukeA answer. Once I took it apart, the path to the solution became clearer to me.
var_value <- df %>%
select(-ends_with("CI")) %>%
gather(var, value, -Year)
var_conf <- df %>%
select(Year, ends_with("CI")) %>%
setNames(sub("(.*)\\sCI$", "\\1", names(.))) %>%
gather(var, conf, -Year)
final.df = data.frame(var_value, conf = var_conf$conf)
Finally, #lukeA's ggplot code does produce the chart he shows.
ggplot(final.df, aes(
x = Year,
y = value,
ymin = value - conf,
ymax = value + conf,
color = var,
fill = var
)) +
geom_ribbon(alpha = .2) +
geom_line()
Data
df <- structure(list(Year = 1991:2014, Total.inflow = c(329L, 268L,
266L, 315L, 312L, 318L, 327L, 391L, 454L, 479L, 481L, 516L, 511L,
589L, 567L, 596L, 574L, 590L, 567L, 591L, 566L, 498L, 526L, 632L
), Total.inflow.CI = c(23L, 20L, 19L, 23L, 22L, 25L, 27L, 27L,
31L, 31L, 30L, 32L, 33L, 40L, 37L, 39L, 40L, 39L, 30L, 31L, 28L,
27L, 29L, 36L), Total.outflow = c(-285L, -281L, -266L, -238L,
-236L, -264L, -279L, -251L, -291L, -321L, -309L, -363L, -363L,
-344L, -361L, -398L, -341L, -427L, -368L, -339L, -351L, -321L,
-317L, -319L), Total.outflow.CI = c(23L, 21L, 20L, 20L, 19L,
28L, 24L, 22L, 24L, 27L, 25L, 29L, 32L, 28L, 31L, 34L, 27L, 41L,
22L, 20L, 22L, 20L, 19L, 22L), UK.inflow = c(93L, 81L, 75L, 91L,
67L, 75L, 79L, 90L, 92L, 83L, 89L, 74L, 85L, 73L, 82L, 66L, 60L,
71L, 82L, 84L, 69L, 73L, 70L, 68L), UK.inflow.CI = c(15L, 15L,
12L, 16L, 13L, 15L, 14L, 15L, 16L, 16L, 16L, 14L, 16L, 12L, 16L,
14L, 12L, 14L, 13L, 14L, 11L, 11L, 12L, 11L), UK.outflow = c(-142L,
-146L, -141L, -112L, -130L, -141L, -140L, -121L, -133L, -151L,
-150L, -172L, -184L, -189L, -175L, -200L, -158L, -159L, -130L,
-125L, -133L, -131L, -125L, -128L), UK.outflow.CI = c(17L, 16L,
16L, 14L, 15L, 22L, 19L, 18L, 16L, 18L, 18L, 22L, 22L, 23L, 22L,
26L, 19L, 22L, 11L, 11L, 12L, 14L, 11L, 13L), EU.inflow = c(60L,
49L, 48L, 53L, 60L, 74L, 70L, 75L, 64L, 55L, 54L, 57L, 58L, 128L,
149L, 173L, 189L, 186L, 162L, 171L, 168L, 148L, 193L, 256L),
EU.inflow.CI = c(12L, 10L, 8L, 10L, 11L, 14L, 18L, 14L, 16L,
13L, 15L, 16L, 17L, 22L, 23L, 26L, 28L, 27L, 19L, 21L, 18L,
17L, 20L, 25L), EU.outflow = c(-51L, -39L, -40L, -46L, -38L,
-50L, -51L, -52L, -57L, -55L, -50L, -54L, -47L, -45L, -56L,
-63L, -66L, -126L, -104L, -92L, -92L, -75L, -78L, -86L),
EU.outflow.CI = c(10L, 6L, 7L, 8L, 7L, 13L, 10L, 9L, 13L,
12L, 13L, 13L, 16L, 10L, 14L, 15L, 15L, 31L, 16L, 13L, 14L,
12L, 12L, 15L), ROW.inflow = c(175L, 138L, 143L, 171L, 185L,
169L, 178L, 226L, 298L, 340L, 338L, 385L, 368L, 388L, 336L,
358L, 325L, 333L, 323L, 336L, 329L, 277L, 264L, 308L), ROW.inflow.CI = c(13L,
10L, 11L, 13L, 15L, 14L, 14L, 17L, 21L, 23L, 20L, 24L, 22L,
31L, 25L, 25L, 25L, 25L, 19L, 18L, 19L, 18L, 18L, 24L), ROW.outflow = c(-91L,
-96L, -85L, -80L, -69L, -73L, -88L, -78L, -101L, -114L, -109L,
-136L, -133L, -109L, -129L, -135L, -117L, -142L, -134L, -122L,
-126L, -115L, -114L, -105L), ROW.outflow.CI = c(12L, 12L,
10L, 11L, 8L, 10L, 11L, 9L, 14L, 15L, 13L, 15L, 16L, 13L,
17L, 16L, 12L, 16L, 9L, 9L, 11L, 9L, 9L, 11L), GDP = c(1142797178130.51,
1179659529659.53, 1061388722255.55, 1140489745944.29, 1237561937825.47,
1306575663026.52, 1446444007858.55, 1537103345478.64, 1565408509949.85,
1554801028899.98, 1535942133294.95, 1680256294964.03, 1943025306122.45,
2297889051629.44, 2418941818181.82, 2588077276908.92, 2969733893557.42,
2793376838235.29, 2314577036921.64, 2403504326328.8, 2594904662714.31,
2630472981169.65, 2712296271989.99, 2990201431078.23)), .Names = c("Year",
"Total.inflow", "Total.inflow.CI", "Total.outflow", "Total.outflow.CI",
"UK.inflow", "UK.inflow.CI", "UK.outflow", "UK.outflow.CI", "EU.inflow",
"EU.inflow.CI", "EU.outflow", "EU.outflow.CI", "ROW.inflow",
"ROW.inflow.CI", "ROW.outflow", "ROW.outflow.CI", "GDP"), row.names = c(NA,
-24L), class = "data.frame")
For example
download.file(
"http://www.sharecsv.com/dl/88f76c7be8ade3a626f474f4857e16f8/stats.csv",
tf <- tempfile(),
method = "libcurl"
)
library(tidyverse)
df <- read_csv(tf)
inner_join(
df %>%
select(-ends_with("CI")) %>%
gather(var, value, -Year),
df %>%
select(Year, ends_with("CI")) %>%
setNames(sub("(.*)\\sCI$", "\\1", names(.))) %>%
gather(var, conf, -Year),
by = c("Year", "var")
) %>%
ggplot(aes(
x = Year,
y = value,
ymin = value - conf,
ymax = value + conf,
color = var,
fill = var
)) +
geom_ribbon(alpha = .2) +
geom_line()
gives you
(I'm using the latest development version of ggplot2)

Set ggmap boundary based on Latitude and Longitude

So I have R program, and am struggling with getting all points in map
library(ggmap)
library(ggplot2)
setwd("d:/GIS/")
sep <- read.csv("SEP_assets_csv.csv")
Sub1 <- sep[grep("SEP.12", names(sep))]
sep$newCol <- 100*rowSums(Sub1)/rowSums(sep[4:7])
# create a new grouping variable
Percent_SEP12_Assets <- ifelse(sep[,8] >= 50, "Over 50", "Under 50")
# get the map
map <- get_map("Kissena Park, Queens", zoom = 13, maptype = 'roadmap')
# plot the map and use the grouping variable for the fill inside the aes
ggmap(map) +
geom_point(data=sep, aes(x = Longitude, y = Latitude, color=Percent_SEP12_Assets ), size=9, alpha=0.6) +
scale_color_manual(breaks=c("Over 50", "Under 50"), values=c("green","red"))
And here is output map
I wish to zoom in enough without cutting out data points, but no matter location I pick on map, the data keeps getting cut, i.e. Removed 2 rows containing missing values (geom_point).
Is there a way to set boundaries based on the extremities of latitude and longitude? The csv I import at
sep <- read.csv("SEP_assets_csv.csv")
Has list of latitude and longitude.
Help!
Coordinates
Latitude Longitude
40.758365 -73.824407
40.774168 -73.818543
40.761748 -73.811379
40.765602 -73.828293
40.751762 -73.81778
40.764834 -73.789712
40.777951 -73.842932
40.76501 -73.794319
40.785959 -73.817349
40.755764 -73.799256
40.745593 -73.829283
40.789929 -73.839501
40.760072 -73.783908
40.726437 -73.807592
40.741093 -73.808757
40.720926 -73.823358
40.729642 -73.81781
40.724191 -73.80937
40.782346 -73.77844
40.778164 -73.799841
40.775122 -73.8185
40.760344 -73.817909
40.792326 -73.809516
40.78322 -73.806977
40.73106 -73.805449
40.736521 -73.813001
40.783714 -73.795027
40.770194 -73.82762
40.735855 -73.823583
40.74943 -73.82141
40.769753 -73.832001
40.754465 -73.826204
40.738775 -73.823892
40.764868 -73.826819
40.738332 -73.82028
40.735017 -73.821339
40.72535 -73.811325
40.721466 -73.820401
dput
> dput(sep)
structure(list(School = structure(1:38, .Label = c("Queens\\25Q020",
"Queens\\25Q021", "Queens\\25Q022", "Queens\\25Q023", "Queens\\25Q024",
"Queens\\25Q025", "Queens\\25Q029", "Queens\\25Q032", "Queens\\25Q079",
"Queens\\25Q107", "Queens\\25Q120", "Queens\\25Q129", "Queens\\25Q130",
"Queens\\25Q154", "Queens\\25Q163", "Queens\\25Q164", "Queens\\25Q165",
"Queens\\25Q168", "Queens\\25Q169", "Queens\\25Q184", "Queens\\25Q185",
"Queens\\25Q189", "Queens\\25Q193", "Queens\\25Q194", "Queens\\25Q200",
"Queens\\25Q201", "Queens\\25Q209", "Queens\\25Q214", "Queens\\25Q219",
"Queens\\25Q237", "Queens\\25Q242", "Queens\\25Q244", "Queens\\25Q425",
"Queens\\25Q460", "Queens\\25Q499", "Queens\\25Q515", "Queens\\25Q707",
"Queens\\25Q792"), class = "factor"), Latitude = c(40.758365,
40.774168, 40.761748, 40.765602, 40.751762, 40.764834, 40.777951,
40.76501, 40.785959, 40.755764, 40.745593, 40.789929, 40.760072,
40.726437, 40.741093, 40.720926, 40.729642, 40.724191, 40.782346,
40.778164, 40.775122, 40.760344, 40.792326, 40.78322, 40.73106,
40.736521, 40.783714, 40.770194, 40.735855, 40.74943, 40.769753,
40.754465, 40.738775, 40.764868, 40.738332, 40.735017, 40.72535,
40.721466), Longitude = c(-73.824407, -73.818543, -73.811379,
-73.828293, -73.81778, -73.789712, -73.842932, -73.794319, -73.817349,
-73.799256, -73.829283, -73.839501, -73.783908, -73.807592, -73.808757,
-73.823358, -73.81781, -73.80937, -73.77844, -73.799841, -73.8185,
-73.817909, -73.809516, -73.806977, -73.805449, -73.813001, -73.795027,
-73.82762, -73.823583, -73.82141, -73.832001, -73.826204, -73.823892,
-73.826819, -73.82028, -73.821339, -73.811325, -73.820401), Windows.SEP.11 = c(48L,
154L, 11L, 62L, 20L, 72L, 9L, 37L, 8L, 22L, 9L, 47L, 44L, 99L,
78L, 91L, 42L, 122L, 55L, 14L, 162L, 108L, 89L, 87L, 23L, 14L,
75L, 74L, 141L, 73L, 43L, 14L, 534L, 189L, 128L, 10L, 79L, 38L
), Mac.SEP.11 = c(49L, 0L, 180L, 2L, 202L, 116L, 41L, 1L, 17L,
22L, 33L, 43L, 1L, 28L, 2L, 0L, 238L, 13L, 76L, 55L, 76L, 42L,
0L, 1L, 12L, 0L, 16L, 10L, 1L, 7L, 0L, 1L, 1L, 67L, 16L, 7L,
31L, 24L), Windows.SEP.12 = c(52L, 252L, 1L, 2L, 12L, 45L, 108L,
15L, 14L, 4L, 19L, 21L, 46L, 90L, 10L, 86L, 15L, 76L, 122L, 2L,
9L, 52L, 39L, 120L, 43L, 17L, 9L, 54L, 19L, 199L, 40L, 25L, 64L,
164L, 14L, 27L, 45L, 2L), Mac.SEP.12 = c(73L, 2L, 91L, 53L, 288L,
6L, 2L, 107L, 109L, 97L, 41L, 18L, 12L, 16L, 2L, 2L, 270L, 32L,
45L, 92L, 54L, 190L, 1L, 4L, 19L, 53L, 1L, 10L, 0L, 61L, 50L,
27L, 27L, 25L, 3L, 1L, 43L, 0L), newCol = c(56.3063063063063,
62.2549019607843, 32.5088339222615, 46.218487394958, 57.4712643678161,
21.3389121338912, 68.75, 76.25, 83.1081081081081, 69.6551724137931,
58.8235294117647, 30.2325581395349, 56.3106796116505, 45.4935622317597,
13.0434782608696, 49.1620111731844, 50.4424778761062, 44.4444444444444,
56.0402684563758, 57.6687116564417, 20.9302325581395, 61.734693877551,
31.0077519379845, 58.4905660377358, 63.9175257731959, 83.3333333333333,
9.9009900990099, 43.2432432432432, 11.8012422360248, 76.4705882352941,
67.6691729323308, 77.6119402985075, 14.5367412140575, 42.4719101123596,
10.5590062111801, 62.2222222222222, 44.4444444444444, 3.125)), .Names = c("School",
"Latitude", "Longitude", "Windows.SEP.11", "Mac.SEP.11", "Windows.SEP.12",
"Mac.SEP.12", "newCol"), row.names = c(NA, -38L), class = "data.frame")
You haven't provided us with any of the data, so I'm going to give an example using a dataset in the historydata package. Instead of getting a map based on a location and a zoom, you can get a map based on the bounding box of the latitudes and longitudes in your dataset.
library(historydata)
library(ggmap)
data("catholic_dioceses")
bbox <- make_bbox(catholic_dioceses$long, catholic_dioceses$lat, f = 0.01)
map <- get_map(bbox)
ggmap(map) +
geom_point(data=catholic_dioceses, aes(x = long, y = lat))
Note that the f = argument to make_bbox() lets you control how much padding there is around your map.
In your case, I think this will work:
library(ggmap)
bbox <- make_bbox(sep$Longitude, sep$Latitude, f = 0.01)
map <- get_map(bbox)
ggmap(map) +
geom_point(data=sep, aes(x = Longitude, y = Latitude,
color = Percent_SEP12_Assets),
size = 9, alpha = 0.6) +
scale_color_manual(breaks=c("Over 50", "Under 50"), values=c("green","red"))

Resources