我最近看到了这张图片,并想在R
中模仿这些图。
然而,更多的是数据争夺者"与数学家相比,我不知道该如何做到这一点。理想情况下,我想使用ggplot2
。我管理了第一个例子(' L'),然后陷入困境:
library(ggplot2)
ggplot(data.frame(x = c(1, 100)), aes(x = x)) +
stat_function(fun = function(x){1/x}, geom = 'line')
理想的答案是使用ggplot2
,并制作facet_wrap
张图片以正方形排列
答案 0 :(得分:2)
试试这个:
library(ggplot2)
title <- textGrob(expression(bold('ALL YOU NEED IS')), gp = gpar(fontsize = 36))
p1.text <- ggplot() +
annotate("text", x=10, y=40, label='italic(y == frac(1,x))', parse=TRUE, size=8) +
theme_bw() +
theme(axis.text = element_blank(), axis.title = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.ticks=element_blank())
p1.curve <- ggplot(data.frame(x = c(1, 50)), aes(x = x)) +
stat_function(fun = function(x){1/x}, geom = 'line', col='red', lwd=2) +
geom_hline(yintercept=0, col='gray50') +
geom_vline(xintercept=0, col='gray50') +
theme_bw() +
theme(axis.text = element_blank(), axis.title = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.ticks=element_blank())
p2.text <- ggplot() +
annotate("text", x=10, y=40, label="italic(x^2+y^2==9)", parse=TRUE, size=8) +
theme_bw() +
theme(axis.text = element_blank(), axis.title = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.ticks=element_blank())
p2.curve <- ggplot(data.frame(theta = seq(-10, 10, .01)), aes(x = 3*cos(theta), y = 3*sin(theta))) +
geom_point(col='red') + theme_bw() +
geom_hline(yintercept=0, col='gray50') +
geom_vline(xintercept=0, col='gray50') +
theme_bw() +
theme(axis.text = element_blank(), axis.title = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.ticks=element_blank())
p3.text <- ggplot() +
annotate("text", x=10, y=40, label="italic(y)==abs(-italic(2*x))", parse=TRUE, size=8) +
theme_bw() +
theme(axis.text = element_blank(), axis.title = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.ticks=element_blank())
p3.curve <- ggplot(data.frame(x = c(-25, 25)), aes(x = x)) +
stat_function(fun = function(x){abs(-2*x)}, geom = 'line', col='red', lwd=2) +
geom_hline(yintercept=0, col='gray50') +
geom_vline(xintercept=0, col='gray50') +
theme_bw() +
theme(axis.text = element_blank(), axis.title = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.ticks=element_blank())
p4.text <- ggplot() +
annotate("text", x=10, y=40, label="x==-italic(3)*abs(italic(sin(y)))", parse=TRUE, size=8) +
theme_bw() +
theme(axis.text = element_blank(), axis.title = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.ticks=element_blank())
p4.curve <- ggplot(data.frame(x = c(-3.2, 3.2)), aes(x = x)) +
stat_function(fun = function(x){-3*abs(sin(x))}, geom = 'line', col='red', lwd=2) +
coord_flip() +
geom_hline(yintercept=0, col='gray50') +
geom_vline(xintercept=0, col='gray50') +
theme_bw() +
theme(axis.text = element_blank(), axis.title = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.ticks=element_blank())
library(grid)
library(gridExtra)
grid.arrange(title, p1.text,p1.curve, p2.text, p2.curve, p3.text, p3.curve, p4.text, p4.curve, ncol=2,
layout_matrix = matrix(c(1,1,2,3,4,5,6,7,8,9), ncol=2, byrow=TRUE))
答案 1 :(得分:2)
从方面开始:
library(ggplot2)
library(dplyr)
L <- data_frame(x = 1:100,
y = 1 / x)
O <- data_frame(t = seq(-pi, pi, l = 100),
x = 3 * cos(t),
y = 3 * sin(t))
V <- data_frame(x = -50:50,
y = abs(-2 * x))
E <- data_frame(y = seq(-pi, pi, l = 100),
x = -3 * abs(sin(y)))
pd <- bind_rows(L = L, O = O, V = V, E = E, .id = 'letter')
pd$letter <- factor(pd$letter,
c('L', 'O', 'V', 'E'),
c('y == 1/x', 'x^2 + y^2 == 9', 'y == abs(-2*x)', 'x == -3*abs(sin(y))'))
ggplot(pd, aes(x, y)) +
geom_vline(xintercept = 0) + geom_hline(yintercept = 0) +
geom_path(size = 1.5, col = 'red') +
facet_wrap(~letter, scales = 'free', labeller = label_parsed) +
theme_minimal() +
theme(axis.text = element_blank(), axis.title = element_blank(),
panel.grid = element_blank())
ggsave('LOVE.png', w = 3.5, h = 4, dpi = 300)