使用填充时 geom_point 中的点大小

问题描述

我想使用 ggplot 来显示点和线,但我希望有两个图例 - 一个用于点,一个用于线。

我使用下面的代码设法做到了这一点,但由于某种原因,'size' 选项不再在 geom_point 中响应,并且它们停留在您可以在图像中看到的相当丑陋的尺寸上

请注意,我选择了 stroke = NA 是因为我不希望这些点有边框。代码如下。

有什么想法吗?

ggplot(data = plot_data) +
    geom_point(aes(x = z.1,y = obs,fill = treatcat),alpha = 0.4,shape = 21,stroke = NA,size = 1) +
    geom_line(aes(x = z.1,y = under,colour = "True"),linetype = "dashed") +
    geom_line(aes(x = z.1,y = crude,colour = "Crude"),size = 1.5) +
    scale_fill_manual(name = "Treatment",values = c("0" = "#F8766D","1" = "#C77CFF"),breaks = c("0","1"),labels = c("Untreated","Treated")) + 
    scale_colour_manual(name = "Model",values = c("Crude" = "orange","True" = "black"),breaks = c("Crude","True"),labels = c("Crude","True")) + 
    ylim(-30,27.5) +
    theme(plot.title = element_text(size = "12")) +
    labs(title = "Fitted Values for Crude Model",x = "Z",y = "Y(1)")

解决方法

也许你想要两个色阶,这里有一个使用 ggnewscale 的解决方案。有几个具有类似功能的 github 包(中继器和 ggh4x)即将推出,但目前这是我所知的唯一 CRAN 选项。

根据评论 - 我正在使用 see::geom_point2 因为我也不喜欢这些笔画

library(ggplot2)
library(see)

ggplot(iris,aes(x = Sepal.Length,y = Sepal.Width)) +
    geom_point2(aes(color = Petal.Width),alpha = 0.4,size = 10) +
  ggnewscale::new_scale_color() +
    geom_smooth(aes(color = Species),linetype = "dashed",method = "lm")