R language learning recommended website | worth collecting

written in front

We have been promoting the basic tutorials of R drawing, one is the recommendation of books, and the other is the recommendation of learning code. In the current era where you can "dumb your mother" for everything, there are really a lot of online resources. We can basically find what we want on the Internet ( PS: as long as you want to check ). Today, I also recommend Xiaodu’s own website for graphing in R language: The R Graph Gallery .

Tutorial URL

https://r-graph-gallery.com/

website content

It contains a lot of basic codes for drawing graphics, and you can basically draw pictures directly according to the codes, which is very convenient.




After you click on each graphic, the drawing code of this graphic will appear.

Operation example

  1. clickEdge bundling
  2. There are also different options after entering

  3. Click on the graphic you want to display the drawing code

drawing code

  1. Download Data
#'@加载数据
library(ggraph)
library(igraph)
library(tidyverse)
##'@创建数据框架
set.seed(1234)
d1 <- data.frame(from="origin", to=paste("group", seq(1,10), sep=""))
d2 <- data.frame(from=rep(d1$to, each=10), to=paste("subgroup", seq(1,100), sep="_"))
hierarchy <- rbind(d1, d2)
head(hierarchy)

##'@创建数据
all_leaves <- paste("subgroup", seq(1,100), sep="_")
connect <- rbind( 
  data.frame( from=sample(all_leaves, 100, replace=T) , to=sample(all_leaves, 100, replace=T)), 
  data.frame( from=sample(head(all_leaves), 30, replace=T) , to=sample( tail(all_leaves), 30, replace=T)), 
  data.frame( from=sample(all_leaves[25:30], 30, replace=T) , to=sample( all_leaves[55:60], 30, replace=T)), 
  data.frame( from=sample(all_leaves[75:80], 30, replace=T) , to=sample( all_leaves[55:60], 30, replace=T)) )
connect$value <- runif(nrow(connect))

# create a vertices data.frame. One line per object of our hierarchy
vertices  <-  data.frame(
  name = unique(c(as.character(hierarchy$from), as.character(hierarchy$to))) , 
  value = runif(111)
) 
#'@添加分组
vertices$group  <-  hierarchy$from[ match( vertices$name, hierarchy$to ) ]

# Create a graph object
mygraph <- graph_from_data_frame( hierarchy, vertices=vertices )

# The connection object must refer to the ids of the leaves:
from  <-  match( connect$from, vertices$name)
to  <-  match( connect$to, vertices$name)
  1. data
  2. draw basic graphics
##'@绘制基础图形
ggraph(mygraph, layout = 'dendrogram', circular = TRUE) + 
  geom_conn_bundle(data = get_con(from = from, to = to), alpha=0.2, colour="skyblue", tension = .5) + 
  geom_node_point(aes(filter = leaf, x = x*1.05, y=y*1.05)) +
  theme_void()

beautify graphics

Several schemes for beautifying graphics are given, you can modify them as a reference. We will not list them uniformly here.

p <- ggraph(mygraph, layout = 'dendrogram', circular = TRUE) + 
  geom_node_point(aes(filter = leaf, x = x*1.05, y=y*1.05)) +
  theme_void()
# 0.1
p +  geom_conn_bundle(data = get_con(from = from, to = to), alpha=0.2, colour="skyblue", width=0.9, 
                      tension=0.1) 

color parameters

# Use the 'value' column of the connection data frame for the color:
p +  geom_conn_bundle(data = get_con(from = from, to = to), aes(colour=value, alpha=value)) 

p +  
  geom_conn_bundle(data = get_con(from = from, to = to), width=1, alpha=0.2, aes(colour=..index..)) +
  scale_edge_colour_distiller(palette = "RdPu") +
  theme(legend.position = "none")
ggsave("color02.pdf",width = 6, height = 6)


For more details, you can check it yourself.


Previous articles:

1. The most complete WGCNA tutorial (replace the data to get all the results and graphics)


2. Beautiful graphics drawing tutorial


Xiao Du's life letter notes , mainly publish or include bioinformatics tutorials, and R-based analysis and visualization (including data analysis, graph drawing, etc.); share interested literature and learning materials!!

Guess you like

Origin blog.csdn.net/kanghua_du/article/details/131762125