我需要显示一个带有visreg的模型,同时自定义顶部层的 colored颜色 . 我以空气质量数据集为例.这是我创建Visreg层的代码,我想在其中可视化不同级别的风的模型.绘制时,根据第三个数值变量的点的 colored颜色 将减少到2.

visreg(fit,"Solar.R","Wind",
          overlay=TRUE,breaks = 2,gg=TRUE)+
     scale_color_manual(values = c("#90da90", "red3"))+
     scale_fill_manual(values = c("gray", "gray"))+
     theme_minimal()

enter image description here

即使我想要两个风值的线条,我也希望我的点在风的梯度中着色,以反映原始值,点应该如下所示:

 ggplot(airquality, aes(x=Solar.R,y=Ozone,color=Wind))+
      geom_point()+
      scale_color_gradient(low = "#90da90", high = "red3")+
      theme_minimal()

enter image description here

但我不能让这两件事在同一个情节中发挥作用

我有过几次不成功的试验,大多数都是这种

 visreg(fit,"Solar.R","Wind",
           overlay=TRUE,breaks = 2,gg=TRUE,
           partial=FALSE,rug=FALSE)+
      scale_color_manual(values = c("#90da90", "red3"))+
      scale_fill_manual(values = c("gray", "gray"))+
      geom_point(aes(color=Wind))+  
      scale_color_gradient(low = "#90da90", high = "red3")+
      theme_minimal()

但以某种方式,我得到了一个与第三个变量的大小相关的错误

有人知道怎么做吗?拜托,我的想象力已经用完了.

推荐答案

遗憾的是,visreg对象不包含原始值或原始值Wind.此外,这些点显示的Ozone值与Ozone的原始值并不对应.相反,这些已经是"合适的"值了.

因此,按Wind为点着色的一种 Select 是首先创建一个新的数据集,其中我们将airquality数据集中的Wind列合并到visreg数据中.然后,该新数据集可用于添加的geom_point个层.

其次,在香草ggplot2中,每个美学只能有一个标尺,标尺要么是离散的,要么是连续的.为了克服这一点,我使用了ggnewscale套装,它允许为相同的美学使用多个标尺.

此外,在下面的代码中,我又添加了两个调整.首先,我放弃了已经存在的geom_point层,第二,我最终调换了点和线层的顺序,将线放在了上面.但是,如果这两行代码对您来说并不重要,您可以删除这两行代码.

library(visreg)
library(ggplot2)
library(ggnewscale)

fit <- lm(Ozone ~ Solar.R + Wind + Temp, data = airquality)

p <- visreg(fit, "Solar.R", "Wind",
  overlay = TRUE, breaks = 2, gg = TRUE
) +
  scale_color_manual(values = c("#90da90", "red3")) +
  scale_fill_manual(values = c("gray", "gray")) +
  theme_minimal()
#> Scale for colour is already present.
#> Adding another scale for colour, which will replace the existing scale.
#> Scale for fill is already present.
#> Adding another scale for fill, which will replace the existing scale.

# Get rid of default points layer (You can drop that)
p$layers[[2]] <- NULL

# Merge original Wind values to visreg data
dat_points <- p$data[c("x", "y")] |> 
  merge(airquality[c("Solar.R", "Wind")], by.x = "x", by.y = "Solar.R")

p <- p +
  ggnewscale::new_scale_color() +
  geom_point(data = dat_points, aes(color = Wind)) +
  scale_color_gradient(
    name = "Wind",
    low = "#90da90", high = "red3"
  )

# Switch order of layers to put lines on top  (You can drop that)
p$layers <- p$layers[c(1, 3, 2)]

p

R相关问答推荐

为什么以及如何修复Mapview不显示所有点并且st_buffer合并一些区域R?

过滤矩阵以获得R中的唯一组合

在使用ggroove后,将图例合并在gplot中

derrr summarise每个组返回多行?

将向量组合到一个数据集中,并相应地命名行

R—将各种CSV数字列转换为日期

迭代到DataFrame列并获得成对的值列表(col1->;col2、col2->;col3、col3->;col4等)的正确方法.

根据1个变量绘制 colored颜色 发散的 map ,由另一个变量绘制饱和度,ggplot2不工作

R -在先前group_by级别汇总时获取最大大小子组的计数

R:如果为NA,则根据条件,使用列名模式将缺少的值替换为另一列中的值

R -如何分配夜间GPS数据(即跨越午夜的数据)相同的开始日期?

来自程序包AFEX和amp;的类/函数和NICE_TABLE&冲突

将多个变量组合成宽格式

如何筛选截止年份之前最后一个测量年度的所有观测值以及截止年份之后所有年份的所有观测值

错误包arrowR:READ_PARQUET/OPEN_DATASET&QOT;无法反序列化SARIFT:TProtocolException:超出大小限制&Quot;

防止正则表达式覆盖以前的语句

R预测包如何处理ARIMA(Auto.arima函数)中的缺失值

我已经运行了几个月的代码的`Palette()`中出现了新的gglot错误

如何在刻面和翻转堆叠条形图中对齐geom_text()

将某个阈值以下的列中的值分类到不同的列中,否则保持该列的原样