圣诞节微生信生物送你一颗专属生信领域的圣诞树

写在前面

外国人把R语言绘制圣诞树在国外都作为家庭作业,虽然我并不过圣诞节,但是看看他们编写的代码还是蛮有意思的,今天我为大家带来三个版本的圣诞树绘制过程。都是基于R语言。这也算是我们一种娱乐吧。

注意:本次无需任何数据,直接复制代码,即可出图。

library(ggplot2)
ggplot()+geom_polygon(aes(x=c(-1,1,1,-1),y=c(0,0,3,3)),fill="brown")+geom_polygon(aes(x=c(0,0,4),y=c(20,1,1)),fill="green")+coord_equal()+
geom_polygon(aes(x=-c(0,0,4),y=c(20,1,1)),fill="green")+theme_void()+
geom_point(aes(x=c(-2,-1,0,1,2,1,-1,0,1,2,-2),y=c(2,7,15,3,6,11,12,5,8,2,5)),color=c("red"),shape=c(19))+
theme(panel.background = element_rect(fill = "blue"))+
geom_point(aes(x=0,y=20),shape=8,color="gold",size=8)+
geom_curve(aes(x=-3.2,y=5,xend=3.2,yend=5),color="gold",size=2)+
geom_curve(aes(x=-2.1,y=10,xend=2.1,yend=10),color="gold",size=2)

下面来一颗奢华版本的圣诞树


library(ggplot2)

# generowanie losowych punktów

x1 <- runif(100000, -1, 1)
y1 <- runif(100000, -1, 1.6)

# równanie na serce
which1 <- x1^2+(y1-(x1^2)^(1/4))^2<=1
x1 <- x1[which1]
y1 <- y1[which1]

# drugi poziom
x2 <- 1.5*x1 -1
y2 <- 1.5*y1

# trzeci poziom
x3 <- 2*x1 -2
y3 <- 2*y1

dt <- data.frame(c(x1,x2,x3),c(y1,y2,y3))
colnames(dt) <- c("x", "y")
dt$y <- dt$y - 0.25
dt <- dt[dt$x>-2 & dt$x<1 & dt$y>-2.25 & dt$y<0,]
dt <- data.frame(c(dt$x,dt$x), c(dt$y, -dt$y))
colnames(dt) <- c("x", "y")

#pieniek

p1 <- runif(1000, -0.5, 0.5)
p2 <- runif(1000, -2.5, -2)

pieniek <- data.frame(p1,p2)

colors <- c('green', 'green2', 'green4', 'darkgreen')
colors2 <- rep(colors,length.out=dim(dt)[1])

bombki <- data.frame(c(-1.8, -1.7, -1.1, -0.6, 0, 0.3), c(-1,0.7,-0.9,0.1,-0.3,0.3))
colnames(bombki) <- c("x", "y")

lancuch <- data.frame(data.frame(x1 = -0.5, x2 = 1, y1 = 21.0, y2 = 15.0))

gwiazda <- data.frame(c(0), c(0.7))
colnames(gwiazda)<- c("x", "y")

ggplot(data=NULL) + geom_point(data=dt, aes(x = y, y=x), col=colors2) +
geom_point(data=bombki, aes(x=y, y=x), size=10, col='blue') +
geom_point (data=pieniek, aes(x=p1, y=p2), col='brown') +
geom_point(data=gwiazda, aes(x=x, y=y),size=10,shape=24, col="yellow", fill='yellow') +
geom_point(data=gwiazda, aes(x=x ,y=y),size=10,shape=25, col="yellow", fill='yellow') +
geom_curve(aes(x=-1,xend=1.2,y=-0.65,yend=-1.2), col='red',size=2, curvature = 0.3) +
geom_curve(aes(x=1.2, y=-1.2, xend=-1.2, yend=-1.5), size=2, col="red", curvature = -0.3) +
geom_curve(aes(x=-1,xend=0.8,y=-0.65,yend=-0.5), col='red',size=2, curvature = 0.3) +
geom_curve(aes(x=-0.8,xend=0.8,y=-0.2,yend=-0.5), col='red',size=2, curvature = 0.2) +
geom_curve(aes(x=-0.8,xend=0.9,y=-0.2,yend=0.15), col='red',size=2, curvature = 0.3) +
geom_curve(aes(x=-0.65,xend=0.9,y=0.35,yend=0.15), col='red',size=2, curvature = 0.3) +
theme(axis.line=element_blank(),axis.text.x=element_blank(),
axis.text.y=element_blank(),axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),legend.position="none",
panel.background=element_blank(),panel.border=element_blank(),panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),plot.background=element_blank())

最后我们来一张交互的圣诞树

(shiny 版本的圣诞树)

library(datasets)
library(ggplot2)
library(RColorBrewer)
library(shiny)

## dane
k = 101
a <- cbind(rep(100,k),rep(100,k),seq(0,100,1), rep(0,k))
a1 <- cbind(rep(100,34), rep(100,34), seq(67,100,1), rep(0,34))
a2 <- cbind(rep(100,67), rep(100,67), seq(34,100,1), rep(0,67))
a3 <- cbind(rep(100,101), rep(100,101), seq(0,100,1), rep(0,101))
x <- rbind(a1,a2,a3)
wektor <- c(rep("a",34),rep("b",67),rep("c",101))
data <- cbind(x,wektor,rep("p",202))

b <- cbind(rep(0,k),rep(100,k),seq(0,100,1), rep(0,k))
b1 <- cbind(rep(0,34), rep(100,34), seq(0,33,1), rep(0,34))
b2 <- cbind(rep(0,67), rep(100,67), seq(0,66,1), rep(0,67))
b3 <- cbind(rep(0,101), rep(100,101), seq(0,100,1), rep(0,101))
y <- rbind(b1,b2,b3)
wektor2 <- c(rep("a",34),rep("b",67),rep("c",101))
data2 <- cbind(y,wektor2,rep("q",202))

l = 31
c <- cbind(rep(seq(94,100,0.2)),rep(100,l),seq(85,100,0.5),rep(0,l))
wektor3 <- c(rep("d",l))
data3 <- cbind(c,wektor3,rep("p",l))

d <- cbind(rep(seq(0,6,0.2)),rep(100,l),seq(0,15,0.5),rep(0,l))
wektor4 <- c(rep("d",l))
data4 <- cbind(d,wektor4,rep("q",l))

kolory <- brewer.pal(9,"Greens")

data <- rbind(data,data2,data3,data4)

lancuchyGora <- rbind(c(84,50,100,50,"a","p"),c(76,25,100,25,"a","p"),
c(16,50,0,50,"a","q"),c(24,25,0,25,"a","q"))
lancuchySrodek <- rbind(c(69,50,100,50,"b","p"),c(50,25,100,25,"b","p"),
c(31,50,0,50,"b","q"),c(50,25,0,25,"b","q"))
lancuchyDol <- rbind(c(50,50,100,50,"c","p"),c(25,25,100,25,"c","p"),
c(50,50,0,50,"c","q"),c(75,25,0,25,"c","q"))

gwiazda <- rbind(c(100,88,0,0,"a","p"),c(0,88,0,0,"a","q"))

swiatelka <- rbind(c(80,20,1,0,"a","p"),c(85,35,2,0,"a","p"),c(90,40,1,0,"a","p"),
c(20,20,2,0,"a","q"),c(15,35,1,0,"a","q"),c(10,40,2,0,"a","q"),
c(60,30,1,0,"b","p"),c(70,40,2,0,"b","p"),c(90,50,1,0,"b","p"),
c(40,30,2,0,"b","q"),c(30,40,1,0,"b","q"),c(10,50,2,0,"b","q"),
c(30,20,1,0,"c","p"),c(50,30,2,0,"c","p"),c(70,40,1,0,"c","p"),c(90,50,2,0,"c","p"),
c(70,20,2,0,"c","q"),c(50,30,1,0,"c","q"),c(30,40,2,0,"c","q"),c(10,50,1,0,"c","q"))

data <- rbind(data,lancuchyGora,lancuchySrodek,lancuchyDol,gwiazda, swiatelka)
data <- data.frame(data)
colnames(data) <- c("xstart","ystart","xend","yend","wiersz","kolumna")
data$xstart <- as.numeric(as.character(data[,1]))
data$ystart <- as.numeric(as.character(data[,2]))
data$xend <- as.numeric(as.character(data[,3]))
data$yend <- as.numeric(as.character(data[,4]))

##

ui <- fixedPage(

titlePanel("Ubierz Choinkę FacetGrid"),
sidebarLayout(
sidebarPanel(
selectInput("gwiazdka", "Gwiazdka",
choices = c("TAK","NIE"), selected = "NIE"),
checkboxGroupInput("lancuch",
"Łańcuchy:",
choices = c("Góra","Środek","Dół"),
selected = FALSE),
sliderInput("light",
"Oświetlenie:",
min = 0,
max = 4,
value = 0),
fluidRow(
actionButton("posprzataj", "Posprzątaj po świętach"),
actionButton("ups", "UPS"))
),
mainPanel(
plotOutput("choinkaPlot",width="60%",height = "450px")
)
)
)

server <- function(input, output) {
v <- reactiveValues(flag=TRUE)
choinka <- reactive({

p <- ggplot(data) + geom_segment(data=data[1:466,],aes(x=xstart,y=ystart,xend=xend, yend=yend, color=wiersz)) +
facet_grid(vars(wiersz),vars(kolumna), margin=FALSE) + theme_minimal() +
guides(color=FALSE) + scale_color_manual(values = c(kolory[6], kolory[7], kolory[8], "brown")) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0))

if("Góra" %in% input$lancuch){
p <- p + geom_curve(data=data[467:470,],aes(x=xstart,y=ystart,xend=xend, yend=yend),
curvature = -0.1,size=2.5, color="grey")
}
if("Środek" %in% input$lancuch){
p <- p + geom_curve(data=data[471:474,],aes(x=xstart,y=ystart,xend=xend, yend=yend),
curvature = -0.1,size=2.5, color="grey")
}
if("Dół" %in% input$lancuch){
p <- p + geom_curve(data=data[475:478,],aes(x=xstart,y=ystart,xend=xend, yend=yend),
curvature = -0.1,size=2.5, color="grey")
}
if(input$gwiazdka=="TAK"){
p <- p + geom_point(data=data[479:480,],aes(x=xstart,y=ystart),
size = 30, color = "darkgoldenrod1", pch="*")
}
if(input$light>0){
p <- p + geom_point(data=data[481:500,], aes(x=xstart,y=ystart, fill = as.factor(xend)),
size = input$light+2, pch=24, colour = "transparent") +
scale_fill_manual(values=brewer.pal(3,"Set1")[1:2]) + guides(fill=FALSE)
}

library(grid)
height <- 1e-6 # Vertical spacing
aux <- 1e-5 # Auxiliary number to identify 'height' among other heights
width <- 1e-6 # Desirable horizontal spacing

p <- p + theme(panel.spacing = unit(height + aux, "lines"))

if(v$flag==FALSE){
ggplot()
} else {
p
}

})

posprzataj <- observeEvent(input$posprzataj, {
v$flag <- FALSE
})

ups <- observeEvent(input$ups, {
v$flag <- TRUE
})

output$choinkaPlot <- renderPlot({
p <- choinka() + labs(x="",y="")
p
})
}

shinyApp(ui = ui, server = server)

(0)

相关推荐

  • R语言GEO数据处理(七)

    # 6. 可视化展示 ---------------------------------------------------------------- ##6.1 火山图 library(ggplot ...

  • R绘图:gggibbous,基于ggplot2的Moon charts

    绘图往期回顾: ggplot2绘图学习 两个连续性变量 ggplot2绘图学习:单变量+绘图背景 R绘图:ggeconodist,基于ggplot2的另类箱图 R语言学习系列之"多变的热图& ...

  • 在x方向上扩展ggplot`geom_ribbon()

    https://stackoverflow.com/questions/55290819/extend-ggplot-geom-ribbon-in-the-x-directionlibrary(tid ...

  • ggdag:DAG和因果图

    近几年来,因果推断同时受到多个学科的重视,是最火热的研究方向之一.因果图(或称路径图)是研究因果关系的一个有效的辅助性工具.借助因果图可以分析因果关系,将复杂问题图形化.本文介绍一个用来绘制因果图的R ...

  • 跟着Nature Genetics 学画图:R语言ggplot2画箱线图(boxplot)展示D s...

    简介:R语言统计与绘图公众号目前致力于分享医学统计与R绘图知识,手把手教你使用R语言绘制基线特征表.KM生存曲线.森林图.ROC曲线等.每天一篇精彩R语言推文教程,手把手带你入门R语言绘图. 今天推文 ...

  • 微生信生物-扩增子结题报告(南京美他生物科技有限公司)

    微生物组学分析报告(南京美他生物科技有限公司) result_and_plot/Base_diversity_16s(ITS) 这部分存储微生物组部分多样性分析结果: 这部分结果使用OTU或者其他分类 ...

  • 微生信生物&根际互作生物学实验室年终总结

    阅读数量 这一年常读用户数量变化 微生信生物的前行和荆棘 2020年不平凡,有很多事情没有做,有很多事情却做了,有很多人走了,有很多人却来了,新冠疫情就像是将有限的资源进行了破碎又缝合,我认为这是新鲜 ...

  • 微生信生物历史推文集合 (持续更新)

    微生信生物历史推文集合 技巧经验思考资源 Rstudio切换挂载R版本及本地安装一些包 pubmed凉了,我们这里依然很美 ggplot版钢铁侠 当科研遇见python 学习R语言&生物信息不 ...

  • 使用R语言的20条建议-微生信生物博主五年经验总结

    写在前面 如果说有什么理念或者习惯支撑在这几年的R语言学习中的话,我认为是这几条,如果大家将这几条能够理解大半,相信最起码会节省时间,提高效率. 注:这些建议不一定都会很好用,大家挑选适合自己的融会贯 ...

  • 无代码福音-微生信生物又要持续发力origin绘非典型柱状图

    上一期结束的时候留了个小问题: 一.前情回顾 首先,数据还是要分组的,因为如果放一列就是一组,最后还得一个一个改(Ctrl+鼠左双击),很麻烦. 那数据的B/C/D列一起作图会是怎样 为什么会这样,上 ...

  • 微生信生物---年中纪--2020

    2020年中纪I 抱歉占用大家一整个版面写下这个纪,毕竟什么都有个开始,有个结束,有的东西结束了,有的东西今天要开始,在此记录,铭记于心,方得始终. 2020年不平凡,经历了新冠肺炎后我们都很珍惜生命 ...

  • 0代码教程来了-来自-微生信生物-的零水平Origin制图

    写在前面 使用代码出图,R语言是最为广泛的,并且漂亮的出图和连带的分析让我们确实是受益良多.但是许多小伙伴,相信有不少人,都是没有足够的时间学习代码,因为大量的科研问题足够让我们头疼.因此,大家来看看 ...

  • 微生信生物科研爬虫项目等你来

    写在前面 微生信生物主编,最近大量任务缠身,我(五谷杂粮) 已经辅助运营了一段时间了,我们的好朋友抱起大块块毕业了.有一个月的空余时间,所以为他写了这篇推送. 前言 面对目前科研任务的多样化,越来越多 ...

  • 微生信生物学习进行时

    不知不觉,微生信生物已经陪伴我们三年了,似乎R语言和扩增子这两个主题占据了大部分的篇幅,这让这是最早涉及的内容呢?随着技术发展和研究内容改变,单一组学无法满足日益严峻的科研形式,所以我们再后来准备了代 ...