R ggplot scale_alpha_数据中离散变量和连续变量手册

R ggplot scale_alpha_数据中离散变量和连续变量手册,r,ggplot2,R,Ggplot2,我有一些数据看起来像: variable value rfvalue stdfvalue mean_value model 1 Protocol Active 0.7975318 0.000000 0.00000000 0.8833071 2013-07-24 to 2019-07-22 2 Protocol Active 0.2716946 0.000000 0.00000000 0.3929385 2011-

我有一些数据看起来像:

         variable      value   rfvalue  stdfvalue mean_value                    model
1 Protocol Active  0.7975318  0.000000 0.00000000  0.8833071 2013-07-24 to 2019-07-22
2 Protocol Active  0.2716946  0.000000 0.00000000  0.3929385 2011-10-12 to 2017-10-10
3            Wind  0.7384344  1.806452 0.07128713  0.2818933 2010-01-01 to 2015-12-31
4 Protocol Active  1.2555754  0.000000 0.00000000  0.8833071 2013-07-24 to 2019-07-22
5 Protocol Active  0.8638037  0.000000 0.00000000  0.8833071 2013-07-24 to 2019-07-22
6            Wind -0.4009561 13.419355 0.54027120  0.4125869 2013-07-24 to 2019-07-22
我可以使用以下方法绘制数据:

d %>% 
  ggplot() +
  coord_flip() +
  ggforce::geom_sina(aes(
    x = variable,
    y = value,
    color = stdfvalue,
    alpha = value),
    method = "counts", maxwidth = 1, size = 1) +
  facet_wrap(~model) +
  scale_color_gradient(
    low = "darkblue",
    high = "darkred",
    breaks = c(0, 1),
    labels = c("     Low", "     High"), 
    guide = guide_colorbar(barwidth = 12, barheight = 0.3))
但是,在我的完整数据集上,绘图如下所示:

         variable      value   rfvalue  stdfvalue mean_value                    model
1 Protocol Active  0.7975318  0.000000 0.00000000  0.8833071 2013-07-24 to 2019-07-22
2 Protocol Active  0.2716946  0.000000 0.00000000  0.3929385 2011-10-12 to 2017-10-10
3            Wind  0.7384344  1.806452 0.07128713  0.2818933 2010-01-01 to 2015-12-31
4 Protocol Active  1.2555754  0.000000 0.00000000  0.8833071 2013-07-24 to 2019-07-22
5 Protocol Active  0.8638037  0.000000 0.00000000  0.8833071 2013-07-24 to 2019-07-22
6            Wind -0.4009561 13.419355 0.54027120  0.4125869 2013-07-24 to 2019-07-22

(其中我将
深蓝色
更改为
黄色
)。
Wind
变量看起来很好,它是一个连续变量,但是
协议活动
变量“隐藏”了我想要显示的一些数据。它是一个离散变量(0,1),我想更加强调1的值。我尝试添加
scale\u alpha\u手册(value=c(1,0.1),guide=FALSE)
,它返回一个错误。我似乎也无法让
scale\u alpha\u手册
同时处理离散和连续

一个尝试是创建两个
geom_sina
图,并根据这两个变量对其进行过滤——我希望对每个变量应用不同的alpha值。我没法让它工作

 ggplot() +
  coord_flip() +
  ggforce::geom_sina(aes(
    x = variable,
    y = value,
    color = stdfvalue),
    method = "counts", maxwidth = 1, size = 1, data = d %>% filter(variable == "Protocol Active")
  ) +
  ggforce::geom_sina(aes(
    x = variable,
    y = value,
    color = stdfvalue,
    alpha = 0.1),
    method = "counts", maxwidth = 1, size = 1, data = d %>% 
      filter(variable == "Wind")
  ) +
  facet_wrap(~model) +
  scale_color_gradient(
    low = "yellow",
    high = "blue1",
    breaks = c(0, 1),
    labels = c("     Low", "     High"), 
    guide = guide_colorbar(barwidth = 12, barheight = 0.3))
我的问题是,如何设置离散变量的alpha值,以便更强调地显示某个类。知道我们是否可以在相同的图上设置
scale\u alpha\u continuous
scale\u alpha\u discrete
。我也愿意听到任何其他的解决办法

数据:


d如果我理解正确,那么您想要实现的目标可以通过
scale\u alpha\u identity
实现。例如,我在数据集中添加了一个变量
alpha
,它定义了从
value
到特定
alpha
级别的映射(或中断)。然后将此新变量映射到
alpha
绘图上。默认情况下,不会显示图例,因此您必须通过指定所需的分隔符和标签手动设置图例

关于你的第二个问题。据我所知,不可能混合使用scale\u xxx\u离散和scale\u xxx\u连续。你可以将不同的变量映射到同一个尺度,但每个唯美主义者只有一个尺度,要么是离散的,要么是连续的。如果您想更好地控制比例,则必须绘制两个图并将它们粘在一起,例如通过
拼凑

#包----------------------------------------------------------------
图书馆(GG2)
图书馆(警队)
图书馆(dplyr)
#资料--------------------------------------------------------------------
d%
变异(α=情况_)(
变量==“风”~ifelse(值>1,1,0.5),
TRUE~ifelse(值>0,1,2)
)) %>%
ggplot()+
coord_flip()+
ggforce::geom_sina(aes)(
x=变量,
y=值,
颜色=标准值,
阿尔法=阿尔法
),
method=“counts”,maxwidth=1,size=1
) +
面_包裹(~模型)+
比例\颜色\渐变(
low=“黄色”,
high=“darkred”,
断开=c(0,1),
标签=c(“低”、“高”),
导向装置=导向装置\彩色条(条宽=12,条高=0.3)
) +
比例α标识(分隔符=c(.2,5,1),标签=c(“一”、“二”、“三”),指南=“图例”)+
主题(legend.position=“底部”)

由(v0.3.0)于2020年3月15日创建