前天那个玫瑰图画出来了, 构建geom_rose的那个函数中的fill = calc(level))出现了问题,改成stat()就好了,当然把fill = calc(level))去掉的话也可以,但是花就变成了黑色。
代码如下,各位可以试一下
library(raster)##没安装的话安装一下install.package('raster')
library(ggplot2)##同上
knitr::opts_chunk$set(dev = "svg")
library(tidyverse)
p <- ggplot() +
coord_equal(1, c(-4, 2), c(-7, 3)) +
geom_curve(aes(x = -1, y = -7, xend = 0, yend = 0),
ncp = 1000, curvature = -0.3, size = 1,
color = "olivedrab3")
p
geom_leaf <- function(x, xend, f, xoffset = 0, yoffset = 0,
xflip = 1, yflip = 1, ...) {
.x <- seq(x, xend, length.out = 100)
.y <- f(.x)
df <- tibble(x = c(.x, .y), y = c(.y, .x))
df$x <- xflip * df$x + xoffset
df$y <- yflip * df$y + yoffset
geom_polygon(aes(x = x, y = y), data = df, ...)
}
f <- function(x) x^2 / 2
p <- p +
geom_leaf(0, 2, f, -1.6, -4.5, 1,
fill = "olivedrab3", color = "palegreen") +
geom_leaf(0, 2, f, -1.6, -5, -1,
fill = "olivedrab3", color = "palegreen")
p
geom_rose <- function(n, mean = c(0, 0), ...) {
.x <- mvtnorm::rmvnorm(n, mean)
df <- tibble(x = .x[, 1], y = .x[, 2])
list(
stat_density_2d(
aes(x = x, y = y, fill = stat(level)), data = df, ###去掉fill=stat(level)花会变成黑色哈哈哈
geom = "polygon", show.legend = FALSE, color = "grey80"),
scale_fill_gradient2(...)
)
}
p +
geom_rose(1000, mean = c(0, 0),
low = "red", mid = "purple", high = "pink",
midpoint = 0.075) +
theme_void()
今天学习箱线图 需要数据data可以微信问我要,这里发不了文件
getwd()
setwd('C:/Users/14327/Desktop/公众号/第四篇')
##加载包
library(ggpubr)
##读取数据
data <- read.csv('data.csv')
head(data)
画图
attach(data)
p1 <- ggboxplot(data, x = "Treatment", y = "SOC",
bxp.errorbar=T,#显示误差条
width = 0.5,#箱体的宽度
color = "Treatment", #分组
palette="aaas",##调色
)
p2 <- ggboxplot(data, x = "Treatment", y = "SOC",
bxp.errorbar=T,
width = 0.5,
color = "Treatment",
palette="aaas",
orientation = "horizontal"#调整图形方向为水平
)
p3 <- ggboxplot(data, x = "Treatment", y = "SOC",
bxp.errorbar=T,
width = 0.5,
color = "Treatment",
palette="aaas",
notch = TRUE,#添加缺口,
order = c("DD","CC","BB","AA")#调整顺序
)
p4 <- ggboxplot(data, x = "Treatment", y = "SOC",
bxp.errorbar=T,
width = 0.5,
color = "Treatment",
select = c("DD")#选择特定的处理来画图
)
###拼接上面的图
library(patchwork)
pp <- p1+p2+p3+p4
pp
添加散点
###添加散点
ggboxplot(data, x = "Treatment", y = "SOC",
width = 0.5,
add = "jitter",#添加图形元素
add.params=list(color = "Treatment",size=0.8, shape = 23))#参数add的参数,可设置颜色
参考资料:https://eric.netlify.app/2017/11/24/using-a-multivariate-normal-to-draw-a-flower-in-ggplot2/ ,https://www.jianshu.com/p/20eb223be60c
以上就是今天的内容啦,明天准备回家吃排骨,这几天要读文献,接下来几天会发点生态文献,大家劳动节快乐!