小编Z.L*_*Lin的帖子

如何识别geom_smooth()使用的函数

我想显示一个由创建的图,geom_smooth()但是对我来说,能够描述如何创建该图很重要。

我可以从文档中看到,当n> = 1000时,使用gam作为平滑函数,但是我看不到使用了多少个结或使用哪个函数生成了平滑。

例:

library(ggplot2)

set.seed(12345)
n <- 3000
x1 <- seq(0, 4*pi,, n)
x2 <- runif(n)
x3 <- rnorm(n)
lp <- 2*sin(2* x1)+3*x2 + 3*x3
p <- 1/(1+exp(-lp))
y <- ifelse(p > 0.5, 1, 0)

df <- data.frame(x1, x2, x3, y)

# default plot
ggplot(df, aes(x = x1, y = y)) +
  geom_smooth() 

# specify method='gam'
# linear
ggplot(df, aes(x = x1, y = y)) +
  geom_smooth(method = 'gam') 

# specify gam and splines
# Shows …
Run Code Online (Sandbox Code Playgroud)

r spline smoothing ggplot2 gam

6
推荐指数
1
解决办法
2208
查看次数

日志转换后,如何在ggplot的图例中保留0值?

如何添加在图例中的NA或“0”的标签,如果我用trans="log"scale_fill_viridis或其它连续的尺度

> d1
       persona num.de.puntos puntos
    1       p1             1      3
    2       p1             2      4
    3       p1             3      0
    4       p1             4      4
    5       p1             5      2
    6       p2             1      2
    7       p2             2      3
    8       p2             3      0
    9       p2             4      0
    10      p2             5      4
    11      p3             1      0
    12      p3             2      1
    13      p3             3      0
    14      p3             4      5
    15      p3             5      8

p <- ggplot(d1, aes(persona, num.de.puntos, fill = puntos)) + 
  scale_fill_viridis(trans="log", …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

6
推荐指数
1
解决办法
128
查看次数

我无法在 RStudio 中将我的文档编成 PDF

我必须使用 RStudio 以 PDF 格式编写报告,但是当我尝试使用 Rmarkdown 中一个块中所需的包来编写文档时,它向我显示此错误:

tlmgr search --file --global '/multirow.sty'

tlmgr: Remote repository is newer than local (2018 < 2019)
Cross release updates are only supported with
  update-tlmgr-latest(.sh/.exe) --update
Please see https://tug.org/texlive/upgrade.html for details.
! LaTeX Error: File `multirow.sty' not found.

! Emergency stop.
<read *> 

Errore: Failed to compile questionario_sulla_responsabilita_222222.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See questionario_sulla_responsabilita_222222.log for more info.
Inoltre: Warning message:
In system2("tlmgr", args, ...) :
  running command ''tlmgr' search --file --global '/multirow.sty'' had status …
Run Code Online (Sandbox Code Playgroud)

r rstudio knitr

6
推荐指数
1
解决办法
2499
查看次数

通过 geom_label_repel 向 ggplot 添加一个带有填充头部的箭头

我想使用该geom_label_repel函数向 ggplot 对象添加一个带有填充头部的箭头。我以为我可以使用:arrow.fill = 'black'就像我对 所做的那样geom_segment,但它在geom_label_repel. 这是获得填充箭头的另一种方法吗?

我使用 的原因geom_label_repel是它是我设法在标签边界处开始箭头的唯一方法。如果可以通过其他方式找到此坐标,我可以使用geom_segment代替。

library(tidyverse)
library(ggrepel)

dmax <- iris %>%
  filter(Sepal.Length == max(Sepal.Length))

ggplot(data = iris, aes(x=Sepal.Width, y=Sepal.Length)) +
  geom_point() +
  geom_label_repel(data=dmax, aes(label = 'max'), 
                   box.padding = unit(.25, 'lines'), 
                   point.padding = unit(1.5, 'lines'), 
                   arrow = arrow(length = unit(0.25, 'cm'), type = 'closed')) +
  geom_segment(aes(x=3, xend=max(Sepal.Width), y=0, yend=max(Sepal.Width)), 
               arrow=arrow(length = unit(0.25, 'cm'), type = 'closed'), 
               arrow.fill = 'black')
Run Code Online (Sandbox Code Playgroud)

r ggplot2 ggrepel

6
推荐指数
1
解决办法
669
查看次数

ggplot2 geom_function 可以跨 arg 值进行分面吗?

我想通过分面 geom_function 创建 ggplot2 图,以便函数的参数在网格中变化。例如这样的东西:

my_function<-function(x, par)
{
 if(par==1)
 {
  return(sin(x))
 }
 else
 {
  return(cos(x))
 }
}

> head(data)
      x parameter
1 -1.00   1
2 -0.99   1
3 -0.98   1
4 -0.97   1
5 -0.96   1
6 -0.95   1

> tail(data)
       x parameter
397 0.95   2
398 0.96   2
399 0.97   2
400 0.98   2
401 0.99   2
402 1.00   2

> my_plot<-ggplot(data, aes(x)) + geom_function(fun = my_function, args=list(par=parameter)+facet_wrap(~parameter)
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为“参数”不在 geom_function 的 args 参数范围内。这可能吗?

r ggplot2

6
推荐指数
1
解决办法
1096
查看次数

ggplot2-秤在哪里建造?

我想看看将因子值转换为数值的地方。我试图通过简单地print在各处添加语句来实现这一点。

geom_tile2 <- function(mapping = NULL, data = NULL,
                      stat = "identity2", position = "identity",
                      ...,
                      na.rm = FALSE,
                      show.legend = NA,
                      inherit.aes = TRUE) {
  layer(
    data = data,
    mapping = mapping,
    stat = stat,
    geom = GeomTile2,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      na.rm = na.rm,
      ...
    )
  )
}

GeomTile2 <- ggproto("GeomTile2", GeomRect,
  extra_params = c("na.rm", "width", "height"),

  setup_data = function(data, params) {
    print(data)

    data$width <- data$width %||% params$width …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 ggproto

5
推荐指数
1
解决办法
78
查看次数

ggplot2中的自定义varwidth

df = data.frame(a = c(0, 0), b = c(17, 15), 
                c = c(35,37), d = c(55,57), 
                e = c(80, 85), x = c(1, 2), 
                w1 = c(20, 30), w2 = c(0.2, 0.3))

ggplot(df) + 
  geom_boxplot(aes(x = x, ymin = a, lower = b, middle = c, upper = d, ymax = e),
               stat = "identity")
Run Code Online (Sandbox Code Playgroud)

我有一个数据框,包含箱图的每个分位数的值,(ae).是否可以使用列w1或w2来定义ggplot中箱线图的宽度?

我期望的结果是与使用varwidthgraphics::boxplot,但自定义的宽度.

graphics::boxplot(mpg~gear, mtcars, varwidth = T)
Run Code Online (Sandbox Code Playgroud)

不要认为这是重复的,因为看起来这个weight论点不起作用stat = identity.

r ggplot2

5
推荐指数
1
解决办法
120
查看次数

如何在R中绘制美国多个州的轮廓?

我想使用经度和纬度点在 R 中绘制美国多个州的轮廓。现在我只能画出每个州的轮廓,所以我想知道如何画出美国多个州的轮廓(仅限外线)。

library(tidyverse)
library(knitr)    
library(broom)
library(stringr)
library(modelr)
library(forcats)
library(ggmap)


states <- map_data("state")# %>% as_tibble()
counties <- map_data("county")
il_df <- filter(fifty_states, id == "illinois")
midwest <- subset(fifty_states, id %in% c("illinois", "indiana", "iowa",
                                    "kansas", "michigan", "minnesota",
                                    "missouri", "nebraska", "north dakota",
                                    "ohio", "south dakota", "wisconsin"))


il_mid <- ggplot(data = midwest, mapping = aes(x = long, y = lat, group = group)) + 
  coord_fixed(1.3) + 
  geom_polygon(color = "black", fill = "gray")
il_mid
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想知道如何绘制下图中的粉红色线。

在此输入图像描述

r ggplot2

5
推荐指数
1
解决办法
2633
查看次数

如何协调 facet_wrap 和 scale="free_y" 中的轴?

我想绘制连续变量和分类变量(geom_boxplotwith ggplot2)之间关系的箱线图,这适用于几种情况(facet_wrap)。很简单:

data("CO2")
ggplot(CO2, aes(Treatment, uptake) ) + 
  geom_boxplot(aes(Treatment, uptake), 
               col="black", fill="white", alpha=0, width=.5) + 
  geom_point(col="black", size=1.2) + 
  facet_wrap(~Type, ncol=3, nrow=6, scales= "free_y") + 
  theme_bw() + 
  ylab("Uptake")
Run Code Online (Sandbox Code Playgroud)

结果: 在此处输入图片说明

This is quite nice with this toy dataset, but applied to my own data (where facet_wrap enables me to plot 18 different graphs) the y-axes are hardly readable, with varying number of y-ticks and varying spacing between them:

在此处输入图片说明

What could be a nice way to harmonize the …

r ggplot2

5
推荐指数
1
解决办法
904
查看次数

R - 在 ScatterPlot 中绘制椭圆(不是* 统计椭圆)

我在 R 中使用 ggplot 创建了一个散点图:

ggplot(data, aes(x=x, y=y)) + 
  xlim(0,800) + 
  ylim(0,600) + 
  geom_point(colour="black") + 
  geom_path(aes(color="red")
Run Code Online (Sandbox Code Playgroud)

我想使用中心坐标、高度和宽度在该图上绘制一个叠加的椭圆(请不是置信区间椭圆)。

我已经尝试过draw.ellipseplotrix包中的函数,但这仅适用于R默认plot 函数创建的散点图。

有人知道如何在 ggplot 创建的散点图上绘制椭圆吗?

r ellipse ggplot2

5
推荐指数
1
解决办法
2867
查看次数

标签 统计

r ×10

ggplot2 ×9

ellipse ×1

gam ×1

ggproto ×1

ggrepel ×1

knitr ×1

rstudio ×1

smoothing ×1

spline ×1