I'm having trouble adding a title and legend to the inside of my graph. Is it not possible with tkplot OR plot?
dff <- data.frame(a = c(0,1,2,3,4),b = c(3,4,5,6,7))
nod <- data.frame(node = c(0:7),wt = c(1:8))
pg <- graph_from_data_frame(d = dff, vertices = nod,directed = F)
# plot function with edge.label added
tkplot(pg, edge.label = nod$wt, main = "this is my graph")
legend('topleft',legend= degree(pg)*5,pt.cex=20,col='white',
pch=21, pt.bg='white')
It doesn't seem to want to work the way I want it to. I just want the legend to show the vertices adjusted to be bigger for a higher degree.
I was also wondering if there is a way to plot only certain vertices from the graph? For example, you choose one vertex and plot only the vertices that form a path back to it?
Based on the help for tkplot, here's an example of how to add a title to an interactive plot:
library(tcltk)
id = tkplot(pg, edge.label = nod$wt, main = "this is my graph")
canvas = tk_canvas(id)
width = as.numeric(tkcget(canvas, "-width"))
height = as.numeric(tkcget(canvas, "-height"))
tkcreate(canvas, "text", width/2, 25, text="My title",
justify="center",
font=tkfont.create(family="helvetica", size=20, weight="bold"))
For the standard base R plot, your code works as is:
plot(pg, edge.label = nod$wt, main = "this is my graph")
legend('topleft',legend= degree(pg)*5,pt.cex=20,col='white',
pch=21, pt.bg='white')
Related
I am trying to plot a likert scale of a 1 - 7 scale for every country with the likert package in R, but the R base plot() function won't change any of the text sizes (or even add a main title).
I am generating the plot with:
p <- likert(summary = data )
plot(p,
plot.percents=FALSE,
plot.percent.low=FALSE,
plot.percent.high=FALSE,
include.center=TRUE,
plot.percent.neutral = FALSE,
col = c(brewer.pal(n = 7, name = "RdBu")),
cex.axis = 0.5,
cex.lab=0.5,
main = "title")
Which produces the following plot:
Plot of Countries of the World
All the other plot parameters are working, so I'm not sure why the last 3, the most basic aren't working.
I fixed this by using theme(axis.text) like so:
plot(p,
plot.percents=FALSE,
plot.percent.low=FALSE,
plot.percent.high=FALSE,
include.center=TRUE,
plot.percent.neutral = FALSE,
col = c(brewer.pal(n = 7, name = "RdBu")),
main = "title") +
theme(axis.text=element_text(size=4)
This doesn't answer the larger question as to why the plot() function isn't accepting the parameters, as the title still isn't showing, but it's a band aid solution
I am trying to automate the process of plotting and saving Manhattan plots. However, of all the processes that I have tried so far, the quality and detail of the automated plots are significantly lower than the manual method, where I first plot the Manhattan plot, then zoom the plot into a Plot Zoom window, and then saving the plot there. In the Plot Zoom version, the plot displays much more details than the automated version. You can see each individual data point more clearly. Is there a way for me automatically plot and save the Plot Zoom version of the Manhattan Plot?
The general methods I've tried resolves something around this:
#data_file is just a list of the various files names
plot_list = list()
for (i in 1:length(data_files)) {
df <- GSL_data[[i]]
manhattan(df, chr = ">CHR", bp = "Positions", p = "P-Value", snp = "SNP-Hash",
main = paste("Manhattan Plot - ", data_files[i]))
p = recordPlot()
plot_list[[i]] = p
file_name = paste(data_files[i], ".png", sep = "")
png(file_name, width = 2218, height = 1464, res = 300)
print(plot_list[[i]])
dev.off()
}
I would recommend trying ggsave. All you need to do is adjust the dimensions you want.
In your code replace
png(file_name, width = 2218, height = 1464, res = 300)
print(plot_list[[i]])
dev.off()
with
ggsave(filename = file_name, plot = p, width = 4, height = 4)
I have build a network and created both igraph and network object. I can plot the object with ggplot and export to plot_ly using ggplotly.
The problem is that i donĀ“t have real control over the graph so i was wondering if there is a better way to import my network (or Ig graph) into plotly to have better control over the layout.
For example when connecting two nodes i have a weight score and would like to add a slider to filter according to the weight score (between 0 and 1).
I guess plotly could be a nice alternative that would allow dynamic filtering over the network graph.
So here is a reproducible exemple.
I create a bipartite network as follows:
library(ggnet)
library(plotly)
library(network)
n <- data.frame(event1 = c(-0.2,0.8,0.4,0),
event2 = c(0.34,-0.17,0.3,0),
event3 = c(0.2,0.1,-0.73,0),
row.names = letters[1:4])
net <- network(n,
matrix.type = "bipartite",
ignore.eval = FALSE,
names.eval = "weights")
col = c("actor" = "grey", "event" = "gold")
set.edge.attribute(net, "color", ifelse(net %e% "weights" > 0, "green", "red"))
g <- ggnet2(net, color = "mode", palette = col, label = TRUE, edge.color = "color",edge.label="weights")
Thanks,
I convert the plot to ggplot format:
gp <- ggplotly(g)
htmlwidgets::saveWidget(as_widget(gp), "ggplot.html")
I get the following graph.
How can i add a slider to the graph so i can filter out (dynamically) the edges above or nelow a certain value ???
For example to see all edges >0 ??? I know how do it from the graph itself but it is not dynamic. I want to use plotly to view and use a slider bar to remove edges.
Thanks,
I'm trying to plot a graph with characters as labels. The issue it that the text is a bit long and the vertices overlap between them. Is there a way to minimize the area of the circular vertex by fitting the text inside? and also, how to avoid overlapping between them? This is the edge list and the data frame of attributes that I'm using(The second column of the df has the labels that I'm trying to fit. Here is my code:
## READ THE EDGE LIST AS A GRAPH OBJECT ##
library('igraph')
cn <- as.matrix(cn, header= FALSE)
g <- graph.edgelist(cn,directed= TRUE)
plot(g, layout = layout.reingold.tilford(g, root=1))
## GET ATTRIBUTES ##
V(g)$name=as.character(att$Name)
V(g)$desc=as.character(att$Description)
## GET LABELS ##
V(g)$label <- V(g)$name
Here I tried to use the method proposed in this post.
## FIT THE LABELS INSIDE THE VERTEX ##
ly <- layout.reingold.tilford(g, root=1) ly <- layout.auto(g)
plot(0, type="n", ann=FALSE, axes=FALSE, xlim=extendrange(ly[,1]), ylim=extendrange(ly[,2])) plot(g, layout=ly, rescale=FALSE, add=TRUE,
vertex.size=(strwidth(V(g)$label) + strwidth("oo")) * 100)
It is working for fitting the text inside the vertexes, but it is not minimizing the area of the circle. For instance, the string of characters "Random Process" would fit better in two lines instead of one and so on.. Also, it has problems with vertex overlapping. I would like to keep the structure of a tree diagram. Here is my outcome so far:
Try with this:
plot(G, vertex.size=8, vertex.color = rainbow(10, .8, .8, alpha= .8),
vertex.label.color = "black", vertex.label.cex = 0.4, vertex.label.degree = -pi/2,
edge.arrow.size = 0.3, edge.arrow.width = 0.4, edge.color = "black")
With vertex.label.degree and vertex.label.cex you fit the text on the node. Best!
I have been playing around with ggplot2 a bunch and found Adding table within the plotting region of a ggplot in r
I was wondering is there any method for this for plotting using non cartesian coordinates, eg if map coordinates were used for the positioning of the table. I had some maps and thought it would be cool if they could have their corresponding data in a table for points to show more detail.
If anyone knows a work around for annotation_custom for non cartesian coordinates it would be greatly appreciated.
EDIT:Here is a image of what my map looks like, I was just thinking is there another way to plot the table on the left side of this.
EDIT: here is what Im attempting to do
EDIT: Here is the basic code structure for the plot
library(ggplot2)
library(ggmap)
plotdata <- read.csv("WellSummary_All_SE_NRM.csv", header = T)
plotdata <- na.omit(plotdata)
plotdata <- plotdata[1:20, c("Unit_No","neg_decimal_lat", "decimal_long", "max_drill_depth", "max_drill_date")]
map.plot<- get_map(location = c(min(plotdata$decimal_long),
min(plotdata$neg_decimal_lat),
max(plotdata$decimal_long),
max(plotdata$neg_decimal_lat)),
maptype ="hybrid",source = "google", zoom=8)
theme_set(theme_bw(base_size = 8))
colormap <- c("darkblue","blue","lightblue", "green", "yellow", "orange","darkorange", "red", "darkred")
myBreaks <- c(0,2, 10, 50, 250, 1250, 2000, 2500)
static.map <- ggmap(map.plot) %+% plotdata +
aes(x = decimal_long,
y = neg_decimal_lat,
z= max_drill_depth)+
stat_summary2d(fun = median, binwidth = c(.03, .03),alpha = 0.7) +
scale_fill_gradientn(name = "depth", colours= colormap, breaks=myBreaks,labels = format(myBreaks),
limits= c(0,2600), space = "Lab") +
labs(x = "Longitude",y = "Latitude")+
geom_text(aes(label=Unit_No),hjust=0, vjust=0,size=2,
position = position_dodge(width=0.9), angle = 45)+
coord_map()
#Creates image of the plot in file to Working Directory
filename=paste("2dmap",".png", sep="")
cat("\t",filename,"file created, saving...\n")
print(static.map)
cat("\tpassed mapping, file now being made\n")
ggsave(filename=filename,
plot = static.map,
scale = 1,
width = 6, height = 4,
dpi = 300)
I will try to upload the data today, cheers for some of the pointers already!
I have uploaded the data, dont worry about the positioning of the gradient values and text tags as I can fix them later I will also link the current ggmap code but I am using a very large loop for the data to be sorted.
https://drive.google.com/file/d/0B8qOIJ-nPp9rM1U1dkEzMUM0Znc/edit?usp=sharing
try this,
library(gridExtra)
grid.arrange(tableGrob(head(iris)), qplot(1,1), ncol=2)
annotation_custom wouldn't help, it's meant for adding things inside the plot panel, not to the side.