在R中绘制方程

在R中绘制方程,r,plot,ggplot2,R,Plot,Ggplot2,我最近看到了这张图片,想在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,并对图像进行切面环绕,以正方形排列进行绘制尝试以下

我最近看到了这张图片,想在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,并对图像进行切面环绕,以正方形排列进行绘制

尝试以下方法:

 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))
从刻面开始:

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)

做得很好,很容易理解你在这里所做的事情。谢谢这也是一个很好的答案——解释得很清楚,结果也很好——我希望我能接受这两个答案!我之所以选择@Axeman,是因为我认为他在时间安排上超过了你,并且成功地完成了任务challenge@JonnyPolonsky当前位置我没有使用facet这是真的,因为我认为目标只是绘制曲线,但我认为我首先提交了:,将其稍作修改以适应您的要求。