I need help changing the vertex labels on graphs produced using bnlearn.
First, I run the program to get an undirected graph. When I plot the graph with plot(data) the vertices are labelled with "V1,V2,V3...". Instead of this I want to plot them with their real labels.
The default plot should have the variable names as the node labels - you will need to share some code to show why this is not so. However, below is a method to manually change the node labels.
library(bnlearn)
library(Rgraphviz)
m <- hc(learning.test)
par(mfrow=c(1,2))
#default plot
g <- graphviz.plot(m)
# change labels
z <- paste0("newlab_", letters[1:numNodes(g)])
names(z) <- nodes(g)
nAttrs <- list()
nAttrs$label <- z
# updated plots
plot(g, nodeAttrs=nAttrs)
For more info on Rgraphviz see How To Plot A Graph Using Rgraphviz, Jeff Gentry
Related
I'm new to R, I searched but I find outdate info only.
I've done a simple single linkage clustering process.
d<-dist(scale(DATA),method="euclidean",diag=TRUE,upper=TRUE)
hls<-hclust(d,method="complete")
How can I plot a scatterplot which uses a color each cluster?
Exactly like this example
I created some sample data to work with. If your data looks different, please provide some sample data as part of your question.
To create a scatter plot colored by group, first create your groups using the cutree function. You can specify an integer value to indicate how may groups you want to create.
Next use your favorite graphing package (e.g. ggplot) to create the scatter plot.
# Sample data
rData <- data.frame(x=c(1,1,3,4), y=c(1,2,5,4))
print(rData)
# Cluster
d <- dist(scale(rData), method="euclidean", diag=TRUE, upper=TRUE)
hls <- hclust(d, method="complete")
# Create groups
cluster <- cutree(hls, 2)
# Create scatter plot
ggData <- cbind(rData, cluster)
ggData$cluster <- as.factor(ggData$cluster)
print(ggData)
ggplot(ggData, aes(x=x, y=y, color=cluster)) + geom_point(size=5)
I would recommend exploring http://www.cookbook-r.com/Graphs/ to learn more about ggplot.
I found some similar questions but the answers didn't solve my problem.
I try to plot a time series of to variables as a scatterplot and using the date to color the points. In this example, I created a simple dataset (see below) and I want to plot all data with timesteps in the 1960ties, 70ties, 80ties and 90ties with one colour respectively.
Using the standard plot command (plot(x,y,...)) it works the way it should, as I try using the ggplot library some strange happens, I guess I miss something. Has anyone an idea how to solve this and generate a correct plot?
Here is my code using the standard plot command with a colorbar
# generate data frame with test data
x <- seq(1,40)
y <- seq(1,40)
year <- c(rep(seq(1960,1969),2),seq(1970,1989,2),seq(1990,1999))
df <- data.frame(x,y,year)
# define interval and assing color to interval
myinterval <- seq(1959,1999,10)
mycolors <- rainbow(4)
colbreaks <- findInterval(df$year, vec = myinterval, left.open = T)
# basic plot
layout(array(1:2,c(1,2)),widths =c(5,1)) # divide the device area in two panels
par(oma=c(0,0,0,0), mar=c(3,3,3,3))
plot(x,y,pch=20,col = mycolors[colbreaks])
# add colorbar
ncols <- length(myinterval)-1
colbarlabs <- seq(1960,2000,10)
par(mar=c(5,0,5,5))
image(t(array(1:ncols, c(ncols,1))), col=mycolors, axes=F)
box()
axis(4, at=seq(0.5/(ncols-1)-1/(ncols-1),1+1/(ncols-1),1/(ncols-1)), labels=colbarlabs, cex.axis=1, las=1)
abline(h=seq(0.5/(ncols-1),1,1/(ncols-1)))
mtext("year",side=3,line=0.5,cex=1)
As I would like to use ggplot package, as I do for other plots, I tried this version with ggplot
# plot with ggplot
require(ggplot2)
ggplot(df, aes(x=x,y=y,color=year)) + geom_point() +
scale_colour_gradientn(colours= mycolors[colbreaks])
but it didn't work the way I thought it would. Obviously, there is something wrong with the color coding. Also, the colorbar looks strange. I also tried it with scale_color_manual and scale_color_gradient2 but I got more errors (Error in continuous_scale).
Any idea how to solve this and generate a plot according to the standard plot 3 including a colorbar.
I'm making a network plot in R using iGraph. I first plot it using tkplot() so that I can manually reposition some of the nodes. Then I capture the new coordinates and then insert those in the plot function to replot the graph along with additional adjustments (changing the opacity of the nodes).
The problem is that even when using the tkplot.coords coordinates, the second graph doesn't look like the tkplot. Instead, some of the arrow heads appear in the middle of the edge rather than at the end, and the nodes are tightly clustered and overlapping, even though that isn't the case with the tkplot. Any suggestions for how I can get the plot() function to exactly mimic the plot produced using tkplot()?
I am using R Studio, so I am wondering if there is a conversion issue with that.
My simplified code is as follows:
Net1 <- graph.data.frame(myedgedata, vertices=nodeslist, directed=TRUE)
g <- graph.adjacency(get.adjacency(Net1), weighted = TRUE)
E(g)$weight <- E(g)$weight+1
tkplot(g)
coords <- tkplot.getcoords(1)
plot(g, edge.width=E(g)$weight, vertex.color = adjustcolor(nodeslist$colors, alpha=.5), layout=coords)
I'm brand new to R and currently am stuck while working on a social network. I'm using the package igraph to create the network.
The way I'm calling for the plot is:
plot(k, layout=layout.fruchterman.reingold, vertex.label=V(k)$Rank)
Is it possible to have multiple labels on a vertex?
You can use paste to concatenate the labels together. Here I choose dummy labels ,
labe is lower letter:UPPER LETTER
library(igraph)
g <- graph.ring(5)
V(g)$size <- 100
V(g)$label=paste(letters[1:5],LETTERS[1:5],sep=':')
V(g)$label.cex <- 2
plot(g)
I am trying to construct a graph consisting of 2-3 filled.contour plots next to each other. The color scale is the same for all plots, and I would like only one z value key plot. I am having difficulties to do this with par(mfrow=c(1,3))
Example code:
x <- 1:5
y <- 1:5
z <- matrix(outer(x,y,"+"),nrow=5)
filled.contour(x,y,z)
filled.contour(x,y,z,color.palette=rainbow)
z2 <- z
z2[5,5] <- Inf
filled.contour(x,y,z2,col=rainbow(200),nlevels=200)
Is it possible to stack 2-3 of these plots next to each other with only one z value color key? I can do this in GIMP but I was wondering if it is natively in R possible.
No I do not think this is possible in filled.contour.
Although extensions have been written for you already. To be found here, here and here and a legend code here.
[If you are using the filled.contour3 function referred to on those sites, and using a more recent version, then you need to use the upgrade fix referred to in this SO post].
Using those codes I produced:
It can be solved with cowplot.
The clue is the cowplot::draw_plot function which successfully represent the plot (cowplot::draw_grob(~filled.contour(x,y,z))).
Later the grob representations can be binded.
The possible problem is that each plot will have own legend.
x <- 1:5
y <- 1:5
z <- matrix(outer(x,y,"+"),nrow=5)
z2 <- z
z2[5,5] <- Inf
cowplot::plot_grid(~filled.contour(x,y,z), ~filled.contour(x,y,z,color.palette=rainbow), ~filled.contour(x,y,z2,col=rainbow(200),nlevels=200))
Created on 2023-02-14 with reprex v2.0.2