How can I add edges into an existing plot? - r

I am wanting to plot graph clusters that I define by myself. I am using the simplified undirected enron data.
library(igraphdata)
data("enron")
g <- as.undirected(enron)
g <- simplify(g)
rm("enron")
member <- c(1, 8, 9, 9, 10, 10, 8, 7, 4, 1, 2, 6, 3, 1, 2, 8, 7, 2, 1, 5,
1, 7, 6, 4, 8, 4, 8, 10, 3, 6, 1, 4, 7, 4, 3, 7, 9, 10, 3, 8, 1,
9, 8, 2, 7, 2, 9, 5, 1, 2, 6, 10, 3, 3, 2, 1, 9, 10, 3, 5, 6, 5,
5, 3, 7, 6, 9, 10, 8, 10, 8, 8, 10, 10, 10, 8, 7, 7, 9, 1, 9, 2, 9,
7, 2, 7, 7, 3, 2, 5, 2, 1, 6, 5, 10, 4, 3, 2, 4, 6, 4, 9, 5, 4,
1, 10, 2, 3, 4, 3, 6, 3, 6, 4, 6, 8, 2, 4, 5, 1, 5, 1, 4, 10, 4, 7,
5, 9, 10, 1, 2, 1, 5, 7, 5, 3, 5, 8, 7, 9, 5, 8, 1, 5, 3, 3, 3, 10,
1, 7, 8, 4, 1, 10, 9, 6, 9, 9, 4, 2, 6, 4, 6, 3, 5, 6, 9, 7, 6, 6,
4, 8, 6, 8, 8, 2, 5, 4, 3, 2, 9, 10, 2, 7)
I have tried many ways but none looks good. The best I can make is
edges_data_frame <- get.data.frame(g, what = "edges")
w.mem <- rep(0, length(E(g)))
for (i in 1:length(E(g))){
w.mem[i] <- ifelse(member[edges_data_frame$from[i]] == member[edges_data_frame$to[i]], 500, 1)
}
mem <- make_clusters(g,member)
E(g)$weight <- w.mem
colors <- rainbow(max(membership(mem)))
layout <- layout.fruchterman.reingold(g, weights=w.mem)
set.seed(1234)
plot(g, vertex.color=colors[mem$membership],
mark.groups=communities(mem),
vertex.label = NA,
edge.width = 1, edge.color = "lightgray", vertex.size = 5)
my first trial
I found that the "deleting edges plot" looks much cleaner
coGrph <- delete_edges(g, E(g)[crossing(mem, g)])
col_vector <- c('#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4', '#46f0f0', '#f032e6', '#bcf60c', '#fabebe', '#008080', '#e6beff', '#9a6324', '#fffac8', '#800000', '#aaffc3', '#808000', '#ffd8b1', '#000075', '#808080', '#ffffff', '#000000')
temp <- sapply(1:length(V(g)), FUN = function(i) {col_vector[member[i]]})
V(coGrph)$color <- temp
plot(coGrph, vertex.label = NA, vertex.size = 5)
my second trial
However, this plot has some missing edges and does not reflect the true connection of the plot. I want to use this plot and add the deleted edges back to the plot without changing the positions I have right now. Is it possible?
Thank you very much I really appreciate your help.

Yes. Use your coGrph to create a layout, but then plot the original graph.
Continuing your "second trial"
set.seed(1234)
LOcG = layout_nicely(coGrph)
V(g)$color <- temp
plot(g, layout=LOcG, vertex.label = NA, vertex.size = 5)

Related

How to properly set rendering Rmarkdown to pdf?

I use Rmarkdown to generate reports and if my line is too long it is usually cut after rendering.
Is there a way to fix it?
I attach a screenshot in order better explain my issue.
You can use the chunk option tidy=TRUE to automatically insert line breaks in the code.
---
output: pdf_document
---
```{r, tidy = TRUE}
c(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
```
The linebreaks are inserted by formatR::tidy_source(). See https://yihui.org/knitr/options/#code-decoration for more details.
chunk_content <- "c(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0)"
formatR::tidy_source(text = chunk_content, width.cutoff = 30)
#> c(1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
#> 1, 2, 3, 4, 5, 6, 7, 8, 9,
#> 0, 1, 2, 3, 4, 5, 6, 7, 8,
#> 9, 0, 1, 2, 3, 4, 5, 6, 7,
#> 8, 9, 0)

How to convert a stem and leaf plot into a data set in R?

The stem and leaf plot that I need to convert is given below-
24|9
23|
22|1
21|7
20|2, 2, 5, 5, 6, 9, 9, 9
19|0, 0, 0, 0, 0, 1, 1, 2, 4, 4, 5, 8
18|0, 1, 1, 2, 2, 2, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 9, 9, 9
17|1, 1, 1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 9
16|0, 0, 1, 1, 1, 1, 2, 4, 5, 5, 6, 6, 8, 8, 8, 8
15|0, 1, 1, 1, 1, 1, 1, 5, 5, 5, 5, 6, 6, 6, 7, 7, 8, 9
14|0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 7, 7, 8, 9, 9
13|0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 5, 5, 6, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9, 9
12|1, 1, 1, 2, 2, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9
11|0, 1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 9, 9
10|0, 2, 3, 3, 3, 4, 4, 5, 7, 7, 8
9|0, 0, 9
8|6
Here's maybe one way. If your data looks like this
stem <- "24|9
23|
22|1
21|7
20|2, 2, 5, 5, 6, 9, 9, 9
19|0, 0, 0, 0, 0, 1, 1, 2, 4, 4, 5, 8
18|0, 1, 1, 2, 2, 2, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 9, 9, 9
17|1, 1, 1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 9
16|0, 0, 1, 1, 1, 1, 2, 4, 5, 5, 6, 6, 8, 8, 8, 8
15|0, 1, 1, 1, 1, 1, 1, 5, 5, 5, 5, 6, 6, 6, 7, 7, 8, 9
14|0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 7, 7, 8, 9, 9
13|0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 5, 5, 6, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9, 9
12|1, 1, 1, 2, 2, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9
11|0, 1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 9, 9
10|0, 2, 3, 3, 3, 4, 4, 5, 7, 7, 8
9|0, 0, 9
8|6"
Then we can split up the rows and for each row we split by the pipe. Then we split the right side by commas and join each of those values to the value to the left of the pipe.
rows <- strsplit(stem,"\n")[[1]]
values <- unlist(lapply(strsplit(rows,"\\|"), function(x) {
end_digits <- strsplit(x[2], ", ")[[1]]
if (!all(is.na(end_digits))) {
paste0(x[1], end_digits)
} else {
NULL
}
}
))
This will return character values, but you could convert to numeric with
as.numeric(values)
Here is a different approach. Using #MrFlick's stem and rows objects:
rows <- strsplit(stem,"\n")[[1]]
rows.lst <- strsplit(rows,"\\|")
tens <- as.numeric(sapply(rows.lst, "[", 1)) * 10
ones <- sapply(strsplit(sapply(rows.lst, "[", 2), ","), as.numeric)
vals <- unlist(mapply("+", tens, ones))
vals <- vals[!is.na(vals)]

Creating a histogram with appropriate counts and labels in R

I have a dataset (dat), which I am hard-coding in here:
dat = c(5, 9, 5, 6, 5, 6, 8, 4, 6, 4, 6, 6, 4, 6, 4, 6, 5, 5, 6, 5, 6, 7, 4, 5, 4, 4, 6, 4, 4, 5, 7, 6, 3, 5, 5, 5, 5, 4, 6, 3, 6, 5, 4, 6, 5, 8, 4, 8, 5, 5, 4, 4, 6, 6, 4, 6, 4, 7, 4, 1, 4, 6, 3, 6, 3, 4, 6, 6, 3, 6, 6, 2, 5, 5, 4, 7, 6)
table(dat)
By doing the table function above on the data, I see that there should be a count of 1 for values of 1, and count of 1 for values of 2. However, when I plot the data using hist, I get a count of 2.
hist(dat, col="lightgreen", labels = TRUE, xlim=c(0,10), ylim=c(0,27))
This is the first problem. The other problem is that I am trying to plot the x label value for the corresponding bin (where there should be 11 bins, labeled 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10). Even though I have no 0 values or 10 values, I would like to illustrate that they had a count of 0, and have their bins - like the rest- labeled. How can I accomplish that?
Thanks.
am = hist(dat, col="lightgreen", labels = TRUE,
breaks=seq(min(dat)-2,max(dat)),
axes=F)
axis(2)
axis(1,at=am$mids,seq(min(dat)-1,max(dat)))
Did you mean like this:
hist(dat, col="lightgreen", labels = TRUE,
xlim=c(0,10), ylim=c(0,27), breaks = 0:10, at=0:10)

Error reading dataset in R

I have problem in reading a dataset
My code :
require(igraph)
g <- graph(c(0, 1, 1, 2, 2, 0, 1, 3, 3, 4,
4, 5, 5, 3, 4, 6, 6, 7, 7, 8,
8, 6, 9, 10, 10, 11, 11, 9))
Error :
Error in graph(c(0, 1, 1, 2, 2, 0, 1, 3, 3, 4, 4, 5, 5, 3, 4, 6, 6, 7, :
At structure_generators.c:84 : Invalid (negative) vertex id, Invalid vertex id
The problem seems to be vertex of name 0
yourgraph <- c(0, 1, 1, 2, 2, 0, 1, 3, 3, 4,
4, 5, 5, 3, 4, 6, 6, 7, 7, 8,
8, 6, 9, 10, 10, 11, 11, 9)
g <- graph(yourgraph + 1)

Visualization of Cayley tables in random colors?

I've got the following data which I can visualize like this
A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 2, 1, 5, 6, 3, 4, 9, 10, 7, 8, 12, 11, 3, 5, 1, 7, 2, 9, 4, 11, 6, 12, 8, 10, 4, 6, 7, 8, 9, 10, 11, 1, 12, 2, 3, 5, 5, 3, 2, 9, 1, 7, 6, 12, 4, 11, 10, 8, 6, 4, 9, 10, 7, 8, 12, 2, 11, 1, 5, 3, 7, 9, 4, 11, 6, 12, 8, 3, 10, 5, 1, 2, 8, 10, 11, 1, 12, 2, 3, 4, 5, 6, 7, 9, 9, 7, 6, 12, 4, 11, 10, 5, 8, 3, 2, 1, 10, 8, 12, 2, 11, 1, 5, 6, 3, 4, 9, 7, 11, 12, 8, 3, 10, 5, 1, 7, 2, 9, 4, 6, 12, 11, 10, 5, 8, 3, 2, 9, 1, 7, 6, 4),nrow=12,ncol=12,byrow=TRUE)
require(plotrix)
color2D.matplot(A)
(A could be any square matrix of whole numbers)
I need to make it display with random colors which aren't too similar. Here's an example of what I am trying to achieve:
I've been unable to get randomized colors to work. Is matplot the function for this? Can anyone show me how to randomize the colors?
Per #DWin's comment, try:
plot(NULL, type= "n", xlim = c(1,ncol(A)), ylim = c(1, nrow(A)), xlab = "column", ylab = "row",
main = "HCL colors, pseudo-random hue, scaled chroma and luminance")
rect(col(A)-.5,row(A)-.5,col(A)+.5,row(A)+.5,
col = hcl(h = round(runif(length(A))*360), c = 60*A/max(A)+20, l = 60*A/max(A)+20)
)
I guessed that you still wanted the values in your matrix to still determine the 'darkness' of the colors, as was the case in the grayscale image. The only thing random here is the hue- i.e. a randomly picked angle from a color wheel.

Resources