At present, I generate a figure using the following script
dat <- matrix(runif(1000*99),99,1000)
dat <- rbind(rep(0.1,1000),dat)
out <- cmdscale(dist(dat),k = 2)
plot(out)
points(out[1,1],out[1,2],col = "red")
Based on the above figure, I want to connect that red point with other points, how to do that?
If you want to connect all the points to that red point, you could try...
segments(out[1,1],out[1,2],out[,1],out[,2])
Adjusting the order of the printing and the graphical characteristics could make it a little easier to look at too:
dat <- matrix(runif(1000*99),99,1000)
dat <- rbind(rep(0.1,1000),dat)
out <- cmdscale(dist(dat),k = 2)
plot(out,type="n")
segments(out[1,1],out[1,2],out[,1],out[,2],col="#cccccc")
points(out,col="black",pch=20)
points(out[1,1],out[1,2],col = "red",pch=20)
Related
Is it possible to create custom graphs using ggplot2, for example I want to create a graph of kissing people.
Simple variant
Not completely, but partially, I was able to reproduce it, everything except for the "lines of the eyes" is not clear how to mark them
But how to make a more complex graph of kissing people. In general, is it possible to somehow approximate such a curve, more voluminou?
thank you for your help.
perhaps not what you are looking for, but if you have already got the image, and want to reproduce it in ggplot, then you can use the following method:
library(tidyverse)
library(magick)
library(terra)
# read image
im <- image_read("./data/kiss_1.png")
# conver to black/white image
im2 <- im %>%
image_quantize(
max = 2,
colorspace = "gray" )
# get a matrix of the pixel-colors
m <- as.raster(im2) %>% as.matrix()
# extract coordinates of the black pixels
df <- as.data.frame(which(m == "#000000ff", arr.ind=TRUE))
df$row <- df$row * -1
# plot point
ggplot(df, aes(x = col, y = row)) + geom_point()
I have a CSV file need to draw a graph.
The graph contains nodes and edges.
Therefore, I used the following code to do it.
start.time <- Sys.time()
#Loading Packages
library(igraph)
library(readr)
library(haven)
#import data
df = read.csv('../../Pre_Draw_Graph_for_R.csv', header = TRUE, encoding = 'UTF-8')
#Creating an iGraph Style Edge List
df_Edge_List <- df
#Creating Graph
df_graph = graph.data.frame(df_Edge_List, directed = TRUE)
#df Network: First Try
#Layout Options
set.seed(3500)
layout1 <- layout.fruchterman.reingold(df_graph)
#Node or vertex Options: Color
V(df_graph)$color <- "yellow"
V(df_graph)[degree(df_graph, mode = "in") > 500]$color <- "red"
#Edge Options: Size
E(df_graph)$color <- "grey"
#Plotting
plot(df_graph, vertex.label=NA)
#plot(df_graph)
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken
I can output the following result, but the result has some problems. It shows that every node is very crowded.
I hope to increase the graph distance, but I used a lot of methods already. I cannot fix the problem. I hope to get the following result, but I cannot do it now. I want to make it clear to see the graph.
I used the following to show dput result.
dput(df_graph, file = "G.R")
Due to the data is too big, I used google link to share it
https://drive.google.com/file/d/1wdF8ZKFde8bDSrFrN9e9KfD3tm6dN_s0/view?usp=sharing
Can anyone help me? Thanks
You can get most of the way to a nice graph with a few simple changes. First, change the vertex labeling to numbers as in your second graph. Also, use layout_components so that the components are separated from each other.
library(igraph)
set.seed(1234)
LOC = layout_components(df_graph)
plot(df_graph, layout=LOC, vertex.label=1:vcount(df_graph))
This is OK, but the arrows are a mess. There are two reasons for that: the arrow heads are too big and most (all?) vertices have loops back to themselves. Let's remove the loops and reduce the size of the arrows.
This looks a lot more useable. If you want anything nicer, you will need to start moving around badly placed vertices by hand. That is a lot more work, but you get a nicer picture.
LOC[ 5,] = c(-1.5,-5)
LOC[ 9,] = c(-3,-2)
LOC[16,] = c(-3,14)
LOC[21,] = c(-3,18)
LOC[25,] = c(9,18)
LOC[29,] = c(-9,18)
LOC[35,] = c(-6,15)
LOC[30,] = c(-9,12)
LOC[31,] = c(-6,18)
LOC[37,] = c(1,14)
LOC[38,] = c(-9,15)
LOC[44,] = c(1,18)
plot(DFGS, layout=LOC, vertex.label=1:vcount(df_graph),
edge.arrow.size = 0.5)
More could be done with the hand-editing, but I will leave that to you.
I am trying to draw a Bsyesian Network in R with bnlearn. Here is the my R code
library(bnlearn)
library(Rgraphviz)
first_variable <- rnorm(100)
second_variable <- rnorm(100)
third_variable <- rnorm(100)
v <- data.frame(first_variable,second_variable,third_variable)
b <- hc(v)
hlight <- list(nodes = nodes(b), arcs = arcs(b),col = "grey", textCol = "red")
pp <- graphviz.plot(b, highlight = hlight)
The code above works, but the size of the text in the plot is very smaller than I expected. Here it is:
I think that is because my variables have long names . In my real data, the variable names are even longer. Here is the BN plot for my real dataset:
Is there any way to increase the size of the text in the plot?
This is basically answered in the post here (albeit that wasn't the OPs only question).
The two approaches suggested are to change the text size globally:
par(cex=0.05)
graphviz.plot(res, highlight =
list(nodes=nodes(res), fill="lightgreen", col="black"))
But I don't find that this works.
Alternatively (and this is what I have been doing) is to change the node characteristics separately:
g <- Rgraphviz::layoutGraph(bnlearn::as.graphNEL(b))
graph::nodeRenderInfo(g) <- list(fontsize=20)
Rgraphviz::renderGraph(g)
Let's say I generate 5 sets of random data and want to visualize them using boxplots and save those to a file "boxplots.png". Using the code
png("boxplots.png")
data <- matrix(rnorm(25),5,5)
boxplot(data, names = c("Name1","Name2","Name3","Name4","Name5"))
dev.off()
there are 5 boxplots created as desired in "boxplots.png", however the names for the second ("Name2") and the fourth ("Name4") boxplot are omitted. Even changing the window of my png-view makes no difference. How can I avoid this behavior?
Thank you!
Your offered code does not produce an overlap in my setting, but that point is relatively moot: you want a way to allow more space between words.
One (brute-force-ish) way to fix the symptom is to alternate putting them on separate lines:
set.seed(42)
data <- matrix(rnorm(25),5,5)
nms <- c("Name1","Name2","Name3","Name4","Name5")
oddnums <- which(seq_along(nms) %% 2 == 0)
evennums <- which(seq_along(nms) %% 2 == 1)
(There's got to be a better way to do that, but it works.)
From here:
png("boxplot.png", height = 240)
boxplot(data, names = FALSE)
mtext(nms[oddnums], side = 1, line = 2, at = oddnums)
mtext(nms[evennums], side = 1, line = 1, at = evennums)
dev.off()
(The use of png is not important here, I just used it because of your edit.)
I have found that the beanplot is the best way to represent my data. I want to look at multiple beanplots together to visualize my data. Each of my plots contains 3 variables, so each one looks something like what would be generated by this code:
library(beanplot)
a <- rnorm(100)
b <- rnorm(100)
c <- rnorm(100)
beanplot(a, b ,c ,ylim = c(-4, 4), main = "Beanplot",
col = c("#CAB2D6", "#33A02C", "#B2DF8A"), border = "#CAB2D6")
(Would have just included an image but my reputation score is not high enough, sorry)
I have 421 of these that I want to put into one long PDF (EDIT: One plot per page is fine, this was just poor wording on my part). The approach I have taken was to first generate the beanplots in a for loop and store them in a list at each iteration. Then I will use the multiplot function (from the R Cookbook page on multiplot) to display all of my plots on one long column so I can begin my analysis.
The problem is that the beanplot function does not appear to be set up to assign plot objects as a variable. Example:
library(beanplot)
a <- rnorm(100)
b <- rnorm(100)
plot1 <- beanplot(a, b, ylim = c(-5,5), main = "Beanplot",
col = c("#CAB2D6", "#33A02C", "#B2DF8A"), border = "#CAB2D6")
plot1
If you then type plot1 into the R console, you will get back two of the plot parameters but not the plot itself. This means that when I store the plots in the list, I am unable to graph them with multiplot. It will simply return the plot parameters and a blank plot.
This behavior does not seem to be the case with qplot for example which will return a plot when you recall the stored plot. Example:
library(ggplot2)
a <- rnorm(100)
b <- rnorm(100)
plot2 <- qplot(a,b)
plot2
There is no equivalent to the beanplot that I know of in ggplot. Is there some sort of workaround I can use for this issue?
Thank you.
You can simply open a PDF device with pdf() and keep the default parameter onefile=TRUE. Then call all your beanplot()s, one after the other. They will all be in one PDF document, each one on a separate page. See here.